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.

238 lines
9.1 KiB

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