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.

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