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.

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