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.

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