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.

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