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.

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