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.

216 lines
8.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
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. if (env('APP_ENV') === 'dev') {
  52. $client = new FeiePrintClient($this->feieHost, $this->feiePort);
  53. $msgInfo = array(
  54. 'user' => $this->feieUser,
  55. 'stime' => time(),
  56. 'sig' => sha1($this->feieUser . $this->feieUkey . time()),
  57. 'apiname' => 'Open_queryPrinterStatus',
  58. 'sn' => $printSn
  59. );
  60. $client->post($this->feieApiPath, $msgInfo);
  61. $result = json_decode($client->getContent(),true);
  62. if($result['data'] != '在线,工作状态正常。'){
  63. return [
  64. 'msg' => 'ok',
  65. 'ret' => 0,
  66. 'data' => '920527381_20200927151404_1155818771',
  67. 'serverExecutedTime' => 4
  68. ];
  69. }
  70. }
  71. $content = $this->printFormat($data, 4, 14, 7, 7);
  72. $res = $this->printMsg($printSn, $content, 1);
  73. return $res;
  74. }
  75. /**
  76. * [打印订单接口 Open_printMsg]
  77. * @param [string] $sn [打印机编号sn]
  78. * @param [string] $content [打印内容]
  79. * @param [string] $times [打印联数]
  80. * @return [string] [接口返回值]
  81. */
  82. protected function printMsg($sn, $content, $times = 1)
  83. {
  84. $time = time(); //请求时间
  85. $msgInfo = array(
  86. 'user' => $this->feieUser,
  87. 'stime' => $time,
  88. 'sig' => sha1($this->feieUser . $this->feieUkey . $time),
  89. 'apiname' => 'Open_printMsg',
  90. 'sn' => $sn,
  91. 'content' => $content,
  92. 'times' => $times//打印次数
  93. );
  94. $client = new FeiePrintClient($this->feieHost, $this->feiePort);
  95. if (!$client->post($this->feieApiPath, $msgInfo)) {
  96. echo 'error';
  97. } else {
  98. // 服务器返回的JSON字符串,建议要当做日志记录起来
  99. $result = $client->getContent();
  100. return $result;
  101. }
  102. }
  103. protected function printFormat($arr, $A, $B, $C, $D)
  104. {
  105. $orderInfo = '<CB>懒族生活</CB><BR>';
  106. $orderInfo .= '数量 名称 单价 金额<BR>';
  107. $orderInfo .= '--------------------------------<BR>';
  108. //$shopnum 当前为第几个店铺
  109. $shopnum = 0;
  110. //循环处理子订单
  111. foreach ($arr->orders as $k5 => $order) {
  112. $orderInfo .= ' <BR>';
  113. $shopnum++;
  114. $orderInfo .= "<C>(" . $shopnum . ")" .$order->store->name . '</C><BR>';
  115. $subNum = 0;
  116. //循环处理子订单下商品
  117. foreach ($order['orderGoods'] as $goods){
  118. //店铺商品数量小计
  119. $subNum += $goods->number;
  120. $orderInfo .= str_pad($goods->number,$A,' ',STR_PAD_RIGHT);
  121. //商品名处理
  122. $nameLength = mb_strwidth($goods->name);
  123. $nameArr = mb_str_split($goods->name);
  124. $length = $A;
  125. foreach ($nameArr as $name){
  126. $len = mb_strwidth($name);
  127. $length += $len;
  128. if($length >= ($A + $B +$C + $D)){
  129. $orderInfo .= '<BR> ';
  130. $length = $A;
  131. }
  132. $orderInfo .= $name;
  133. }
  134. //商品名长度是否超过一行
  135. $goodsTotal = bcmul($goods->number,$goods->price,2);
  136. $priceLen = mb_strwidth($goods->price);
  137. $totalLen = mb_strwidth($goodsTotal);
  138. if($nameLength >= ($B+$C+$D-$priceLen-$totalLen)){
  139. $orderInfo .= '<BR>';
  140. $orderInfo .= str_pad($goods->price,$A+$B+$C,' ',STR_PAD_LEFT);
  141. }else{
  142. $orderInfo .= str_pad($goods->price,$C+$B+$A-$length,' ',STR_PAD_LEFT);
  143. }
  144. $orderInfo .= str_pad($goodsTotal,$D,' ',STR_PAD_LEFT);
  145. }
  146. /**
  147. * 订单商品处理结束
  148. */
  149. //处理订单备注
  150. $orderInfo .= '--------------------------------<BR>';
  151. //订单小计
  152. $orderInfo .= $this->space($subNum,$order->money);
  153. if(!empty($order->note)){
  154. $orderInfo .='<BR>';
  155. $orderInfo .='<BR>';
  156. $note = '备注:'.$order->note;
  157. $orderInfo .= $note.'<BR>';
  158. }
  159. }
  160. $orderInfo .= '--------------------------------<BR>';
  161. if ($arr->services_money > 0) {
  162. $orderInfo .= $this->space('服务费:',$arr->services_money);
  163. }
  164. if($arr->shipping_type != 3 && $arr->delivery_money > 0){
  165. $orderInfo .= $this->space('配送费:',$arr->delivery_money);
  166. }
  167. if($arr->coupon_money > 0){
  168. $orderInfo .= $this->space('红包:',$arr->coupon_money);
  169. }
  170. $total = '合计:'.$arr->money;
  171. $userName = $arr->name;
  172. if(strlen($userName)>18){
  173. $userName=substr($userName,0,18).'...';
  174. }
  175. $userLength = preg_replace('/[^\x{4e00}-\x{9fa5}]/u', '', $userName);
  176. $totalLength = preg_replace('/[^\x{4e00}-\x{9fa5}]/u', '', $total);
  177. $userLength = mb_strlen($userLength);
  178. $totalLength = mb_strlen($totalLength);
  179. $orderInfo .= str_pad($userName,$A+$B+$userLength,' ',STR_PAD_RIGHT);
  180. $orderInfo .= str_pad($total,$C+$D+$totalLength+1,' ',STR_PAD_LEFT);
  181. $orderInfo .= '<BR>';
  182. $orderInfo .= '配送方式:' . $arr->shipping_type_text . '<BR>';
  183. if($arr->shipping_type != Shipping::TYPE_SELF_TAKE){
  184. $orderInfo .= '送货地点:' . $arr->address . '<BR>';
  185. }
  186. if(!empty($arr->tel)){
  187. $tel = substr_replace( $arr->tel, '****', 3, 4);
  188. $orderInfo .= '联系电话:' . $tel . '<BR>';
  189. }
  190. $orderInfo .= '下单时间:' . $arr->pay_time_text . '<BR>';
  191. if($arr->shipping_type != 3 && $arr->delivery_money > 0) {
  192. $orderInfo .= '送达时间:' . $arr->delivery_time_note . '<BR>';
  193. }else{
  194. $orderInfo .= '自提时间:' . $arr->delivery_time_note . '<BR>';
  195. }
  196. //$orderInfo .= '<QR>http://www.feieyun.com</QR>';//把解析后的二维码生成的字符串用标签套上即可自动生成二维码
  197. return $orderInfo;
  198. }
  199. function space($name,$price,$len = 32)
  200. {
  201. $length = mb_strwidth($name);
  202. $str = $name;
  203. $str .= str_pad($price ,$len-$length,' ',STR_PAD_LEFT);
  204. return $str;
  205. }
  206. }