You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

245 lines
9.1 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Libs\FeiePrintClient;
  4. use App\Service\v3\Interfaces\FeiePrintServiceInterface;
  5. use Hyperf\DbConnection\Db;
  6. class FeiePrintService implements FeiePrintServiceInterface
  7. {
  8. // *必填*:飞鹅云后台注册账号
  9. const USER = '13161443713@163.com';
  10. // *必填*: 飞鹅云后台注册账号后生成的UKEY 【备注:这不是填打印机的KEY】
  11. const UKEY = 'XsaHzgePdyWTfcMX';
  12. // *必填*:打印机编号,必须要在管理后台里添加打印机或调用API接口添加之后,才能调用API
  13. const SN = '550510805';
  14. // 以下参数不需要修改
  15. // 接口IP或域名
  16. const IP = 'api.feieyun.cn';
  17. // 接口IP端口
  18. const PORT = 80;
  19. // 接口路径
  20. const PATH = '/Api/Open/';
  21. protected $feieUser = '';
  22. protected $feieUkey = '';
  23. protected $feieHost = '';
  24. protected $feiePort = '';
  25. protected $feieApiPath = '';
  26. public function __construct()
  27. {
  28. $this->feieUser = config('feie.user');
  29. $this->feieUkey = config('feie.ukey');
  30. $this->feieHost = config('feie.host');
  31. $this->feiePort = config('feie.port');
  32. $this->feieApiPath = config('feie.api_path');
  33. }
  34. public function feiePrint($orderMainId)
  35. {
  36. // TODO 对象数组=》二维数组
  37. $data = Db::table('lanzu_order_main as m')
  38. ->join('lanzu_order as o','o.order_main_id', '=', 'm.id','inner')
  39. ->join('lanzu_order_goods as g','o.id','=', 'g.order_id','inner')
  40. ->join('lanzu_feprint as f','m.market_id','=', 'f.market_id','inner')
  41. ->join('lanzu_store as s','s.id','=', 'o.store_id','inner')
  42. ->where('m.id', $orderMainId)
  43. ->selectRaw("o.note as o_note,g.name,g.number,g.price,g.goods_unit,m.delivery_time_note as ps_time,m.address,m.note,m.name as user_name,m.delivery_money,m.money as m_money,m.coupon_money,m.services_money,f.sn,m.tel,m.order_num,g.id,g.spec,s.name as shopname")
  44. ->orderBy('s.id')
  45. ->get()
  46. ->toArray();
  47. if (empty($data)) {
  48. return ;
  49. }
  50. foreach ($data as $key => &$item) {
  51. $item = (array)$item;
  52. }
  53. $content = $this->printFormat($data, 14, 6, 3, 6);
  54. $res = $this->printMsg($data[0]['sn'], $content, 1);
  55. return ($res);
  56. }
  57. /**
  58. * [打印订单接口 Open_printMsg]
  59. * @param [string] $sn [打印机编号sn]
  60. * @param [string] $content [打印内容]
  61. * @param [string] $times [打印联数]
  62. * @return [string] [接口返回值]
  63. */
  64. protected function printMsg($sn, $content, $times = 1)
  65. {
  66. $time = time(); //请求时间
  67. $msgInfo = array(
  68. 'user' => $this->feieUser,
  69. 'stime' => $time,
  70. 'sig' => sha1($this->feieUser . $this->feieUkey . $time),
  71. 'apiname' => 'Open_printMsg',
  72. 'sn' => $sn,
  73. 'content' => $content,
  74. 'times' => $times//打印次数
  75. );
  76. $client = new FeiePrintClient($this->feieHost, $this->feiePort);
  77. if (!$client->post($this->feieApiPath, $msgInfo)) {
  78. echo 'error';
  79. } else {
  80. // 服务器返回的JSON字符串,建议要当做日志记录起来
  81. $result = $client->getContent();
  82. return $result;
  83. }
  84. }
  85. protected function printFormat($arr, $A, $B, $C, $D)
  86. {
  87. $orderInfo = '<CB>懒族生活</CB><BR>';
  88. $orderInfo .= '名称 单价 数量 金额<BR>';
  89. $orderInfo .= '--------------------------------<BR>';
  90. $shopname = "";
  91. $shopnum = 0;
  92. foreach ($arr as $k5 => $v5) {
  93. if ($shopname != $v5['shopname']) {
  94. if ($shopname != "") {
  95. $orderInfo .= ' <BR>';
  96. }
  97. $shopnum++;
  98. $orderInfo .= "<C>(" . $shopnum . ")" .$v5['shopname'] . '</C><BR>';
  99. $shopname = $v5['shopname'];
  100. }
  101. $name = $v5['name'];
  102. $name .= "(规格:". $v5['goods_unit'].")";
  103. $price = $v5['m_money'];
  104. $num = $v5['number'];
  105. $prices = sprintf("%.2f",$v5['m_money']*$v5['number']);
  106. $kw3 = '';
  107. $kw1 = '';
  108. $kw2 = '';
  109. $kw4 = '';
  110. $str = $name;
  111. $blankNum = $A;//名称控制为14个字节
  112. $lan = mb_strlen($str,'utf-8');
  113. $m = 0;
  114. $j=1;
  115. $blankNum++;
  116. $result = array();
  117. if(strlen($price) < $B){
  118. $k1 = $B - strlen($price);
  119. for($q=0;$q<$k1;$q++){
  120. $kw1 .= ' ';
  121. }
  122. $price = $kw1.$price;
  123. }
  124. if(strlen($num) < $C){
  125. $k2 = $C - strlen($num);
  126. for($q=0;$q<$k2;$q++){
  127. $kw2 .= ' ';
  128. }
  129. $num = $kw2.$num;
  130. }
  131. if(strlen($prices) < $D){
  132. $k3 = $D - strlen($prices);
  133. for($q=0;$q<$k3;$q++){
  134. $kw4 .= ' ';
  135. }
  136. $prices = $kw4.$prices;
  137. }
  138. for ($i=0;$i<$lan;$i++){
  139. $new = mb_substr($str,$m,$j,'utf-8');
  140. $j++;
  141. if(mb_strwidth($new,'utf-8')<$blankNum) {
  142. if($m+$j>$lan) {
  143. $m = $m+$j;
  144. $tail = $new;
  145. // $lenght = iconv("UTF-8", "GBK//IGNORE", $new);
  146. $k = $A - mb_strwidth($new,'utf-8');
  147. for($q=0;$q<$k;$q++){
  148. $kw3 .= ' ';
  149. }
  150. if($m==$j){
  151. $tail .= $kw3.' '.$price.' '.$num.' '.$prices;
  152. }else{
  153. $tail .= $kw3.'<BR>';
  154. }
  155. break;
  156. }else{
  157. $next_new = mb_substr($str,$m,$j,'utf-8');
  158. if(mb_strwidth($next_new,'utf-8')<$blankNum) continue;
  159. else{
  160. $m = $i+1;
  161. $result[] = $new;
  162. $j=1;
  163. }
  164. }
  165. }
  166. }
  167. $head = '';
  168. foreach ($result as $key=>$value) {
  169. if($key < 1){
  170. // $v_lenght = iconv("UTF-8", "GBK//IGNORE", $value);
  171. $v_lenght = mb_strwidth($value,'utf-8');
  172. if($v_lenght == 13) $value = $value." ";
  173. $head .= $value.' '.$price.' '.$num.' '.$prices;
  174. }else{
  175. $head .= $value.'<BR>';
  176. }
  177. }
  178. $orderInfo .= $head.$tail;
  179. if(!empty($v5['o_note'])){
  180. $orderInfo .= '备注:'.$v5['o_note'].'<BR>';
  181. }
  182. }
  183. // $time = date('Y-m-d H:i:s', time());
  184. $orderInfo .= '--------------------------------<BR>';
  185. if ($arr[0]['services_money'] > 0) {
  186. $kw5 = '';
  187. $len = 24 - strlen($arr[0]['services_money']);
  188. for ($q = 0; $q < $len; $q++) {
  189. $kw5 .= ' ';
  190. }
  191. $orderInfo .= '服务费:' . $kw5 . $arr[0]['services_money'] . '<BR>';
  192. }
  193. if($arr[0]['delivery_money'] > 0){
  194. $kw5 = '';
  195. $len = 24 - strlen($arr[0]['delivery_money']);
  196. for ($q = 0; $q < $len; $q++) {
  197. $kw5 .= ' ';
  198. }
  199. $orderInfo .= '配送费:'.$kw5.$arr[0]['delivery_money'].'<BR>';
  200. }
  201. if($arr[0]['coupon_money'] > 0){
  202. $coupon_money = sprintf("%.2f",$arr[0]['coupon_money']);
  203. $kw6 = '';
  204. $len = 25 - strlen($coupon_money);
  205. for ($q = 0; $q < $len; $q++) {
  206. $kw6 .= ' ';
  207. }
  208. $orderInfo .= '红包:'.$kw6.'-'.$coupon_money.'<BR>';
  209. }
  210. $total = '合计:'.$arr[0]['m_money'];
  211. $user_name = $arr[0]['user_name'];
  212. if(strlen($user_name)>18){
  213. $user_name=substr($user_name,0,18).'...';
  214. }
  215. $str = $user_name . $total;
  216. $kw5 = '';
  217. // $lenght = iconv("UTF-8", "GBK//IGNORE", $str);
  218. $total_len = 32 - mb_strwidth($str,'utf-8');
  219. for ($q = 0; $q < $total_len; $q++) {
  220. $kw5 .= ' ';
  221. }
  222. $total_str = $user_name.$kw5.$total;
  223. $orderInfo .= $total_str.'<BR>';
  224. $orderInfo .= '送货地点:' . $arr[0]['address'] . '<BR>';
  225. $tel = substr_replace( $arr[0]['tel'], '****', 3, 4);
  226. $orderInfo .= '联系电话:' . $tel . '<BR>';
  227. $orderInfo .= '配送时间:' . $arr[0]['ps_time'] . '<BR>';
  228. if(!empty($arr[0]['note'])){
  229. $orderInfo .= '备注:'.$arr[0]['note'].'<BR><BR>';
  230. }
  231. //$orderInfo .= '<QR>http://www.feieyun.com</QR>';//把解析后的二维码生成的字符串用标签套上即可自动生成二维码
  232. return $orderInfo;
  233. }
  234. }