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.

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