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.

186 lines
7.2 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
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
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\Model\v3\Feprint;
  5. use App\Model\v3\OrderMain;
  6. use App\Service\v3\Interfaces\FeiePrintServiceInterface;
  7. use Hyperf\DbConnection\Db;
  8. class FeiePrintService implements FeiePrintServiceInterface
  9. {
  10. // *必填*:飞鹅云后台注册账号
  11. const USER = '13161443713@163.com';
  12. // *必填*: 飞鹅云后台注册账号后生成的UKEY 【备注:这不是填打印机的KEY】
  13. const UKEY = 'XsaHzgePdyWTfcMX';
  14. // *必填*:打印机编号,必须要在管理后台里添加打印机或调用API接口添加之后,才能调用API
  15. const SN = '550510805';
  16. // 以下参数不需要修改
  17. // 接口IP或域名
  18. const IP = 'api.feieyun.cn';
  19. // 接口IP端口
  20. const PORT = 80;
  21. // 接口路径
  22. const PATH = '/Api/Open/';
  23. protected $feieUser = '';
  24. protected $feieUkey = '';
  25. protected $feieHost = '';
  26. protected $feiePort = '';
  27. protected $feieApiPath = '';
  28. public function __construct()
  29. {
  30. $this->feieUser = config('feie.user');
  31. $this->feieUkey = config('feie.ukey');
  32. $this->feieHost = config('feie.host');
  33. $this->feiePort = config('feie.port');
  34. $this->feieApiPath = config('feie.api_path');
  35. }
  36. public function feiePrint($globalOrderId)
  37. {
  38. // TODO 对象数组=》二维数组
  39. $data = OrderMain::query()->with(['orders' => function($query){
  40. $query->with('store','orderGoods');
  41. }
  42. ])->where('global_order_id',$globalOrderId)->first();
  43. if (empty($data)) {
  44. return ;
  45. }
  46. // foreach ($data['order'] as $key => &$item) {
  47. // $item = (array)$item;
  48. // }
  49. $printSn = Feprint::query()->where('market_id',$data->market_id)->value('sn');
  50. $content = $this->printFormat($data, 4, 14, 7, 7);
  51. $res = $this->printMsg($printSn, $content, 1);
  52. return $res;
  53. }
  54. /**
  55. * [打印订单接口 Open_printMsg]
  56. * @param [string] $sn [打印机编号sn]
  57. * @param [string] $content [打印内容]
  58. * @param [string] $times [打印联数]
  59. * @return [string] [接口返回值]
  60. */
  61. protected function printMsg($sn, $content, $times = 1)
  62. {
  63. $time = time(); //请求时间
  64. $msgInfo = array(
  65. 'user' => $this->feieUser,
  66. 'stime' => $time,
  67. 'sig' => sha1($this->feieUser . $this->feieUkey . $time),
  68. 'apiname' => 'Open_printMsg',
  69. 'sn' => $sn,
  70. 'content' => $content,
  71. 'times' => $times//打印次数
  72. );
  73. $client = new FeiePrintClient($this->feieHost, $this->feiePort);
  74. if (!$client->post($this->feieApiPath, $msgInfo)) {
  75. echo 'error';
  76. } else {
  77. // 服务器返回的JSON字符串,建议要当做日志记录起来
  78. $result = $client->getContent();
  79. return $result;
  80. }
  81. }
  82. protected function printFormat($arr, $A, $B, $C, $D)
  83. {
  84. $orderInfo = '<CB>懒族生活</CB><BR>';
  85. $orderInfo .= '数量 名称 单价 金额<BR>';
  86. $orderInfo .= '--------------------------------<BR>';
  87. //$shopnum 当前为第几个店铺
  88. $shopnum = 0;
  89. //循环处理子订单
  90. foreach ($arr->orders as $k5 => $order) {
  91. $orderInfo .= ' <BR>';
  92. $shopnum++;
  93. $orderInfo .= "<C>(" . $shopnum . ")" .$order->store->name . '</C><BR>';
  94. $subNum = 0;
  95. //循环处理子订单下商品
  96. foreach ($order['orderGoods'] as $goods){
  97. //店铺商品数量小计
  98. $subNum += $goods->number;
  99. $orderInfo .= str_pad($goods->number,$A,' ',STR_PAD_RIGHT);
  100. //商品名处理
  101. $nameLength = mb_strwidth($goods->name);
  102. $nameArr = mb_str_split($goods->name);
  103. $length = $A;
  104. foreach ($nameArr as $name){
  105. $len = mb_strwidth($name);
  106. $length += $len;
  107. if($length >= ($A + $B +$C + $D)){
  108. $orderInfo .= '<BR> ';
  109. $length = $A;
  110. }
  111. $orderInfo .= $name;
  112. }
  113. //商品名长度是否超过一行
  114. $goodsTotal = bcmul($goods->number,$goods->price,2);
  115. $priceLen = mb_strwidth($goods->price);
  116. $totalLen = mb_strwidth($goodsTotal);
  117. if($nameLength > ($B+$C+$D-$priceLen-$totalLen)){
  118. $orderInfo .= '<BR>';
  119. $orderInfo .= str_pad($goods->price,$A+$B+$C,' ',STR_PAD_LEFT);
  120. }else{
  121. $orderInfo .= str_pad($goods->price,$C+$B+$A-$length,' ',STR_PAD_LEFT);
  122. }
  123. $orderInfo .= str_pad($goodsTotal,$D,' ',STR_PAD_LEFT);
  124. }
  125. /**
  126. * 订单商品处理结束
  127. */
  128. //处理订单备注
  129. $orderInfo .= '--------------------------------<BR>';
  130. //订单小计
  131. $orderInfo .= $this->space($subNum,$order->money);
  132. if(!empty($order->note)){
  133. $orderInfo .='<BR>';
  134. $orderInfo .='<BR>';
  135. $note = '备注:'.$order->note;
  136. $orderInfo .= $note.'<BR>';
  137. }
  138. }
  139. $orderInfo .= '--------------------------------<BR>';
  140. if ($arr->services_money > 0) {
  141. $orderInfo .= $this->space('服务费:',$arr->services_money);
  142. }
  143. if($arr->delivery_money > 0){
  144. $orderInfo .= $this->space('配送费:',$arr->delivery_money);
  145. }
  146. if($arr->coupon_money > 0){
  147. $orderInfo .= $this->space('红包:',$arr->coupon_money);
  148. }
  149. $total = '合计:'.$arr->money;
  150. $userName = $arr->name;
  151. if(strlen($userName)>18){
  152. $userName=substr($userName,0,18).'...';
  153. }
  154. $userLength = preg_replace('/[^\x{4e00}-\x{9fa5}]/u', '', $userName);
  155. $totalLength = preg_replace('/[^\x{4e00}-\x{9fa5}]/u', '', $total);
  156. $userLength = mb_strlen($userLength);
  157. $totalLength = mb_strlen($totalLength);
  158. $orderInfo .= str_pad($userName,$A+$B+$userLength,' ',STR_PAD_RIGHT);
  159. $orderInfo .= str_pad($total,$C+$D+$totalLength+1,' ',STR_PAD_LEFT);
  160. $orderInfo .= '<BR>';
  161. $orderInfo .= '送货地点:' . $arr->address . '<BR>';
  162. $tel = substr_replace( $arr->tel, '****', 3, 4);
  163. $orderInfo .= '联系电话:' . $tel . '<BR>';
  164. $orderInfo .= '下单时间:' . $arr->pay_time_text . '<BR>';
  165. $orderInfo .= '送达时间:' . $arr->delivery_time_note . '<BR>';
  166. //$orderInfo .= '<QR>http://www.feieyun.com</QR>';//把解析后的二维码生成的字符串用标签套上即可自动生成二维码
  167. return $orderInfo;
  168. }
  169. function space($name,$price,$len = 32)
  170. {
  171. $length = mb_strwidth($name);
  172. $str = $name;
  173. $str .= str_pad($price ,$len-$length,' ',STR_PAD_LEFT);
  174. return $str;
  175. }
  176. }