YlyPrinterService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\service\printer;
  20. use App\Api\PrinterService;
  21. use App\Api\PrintService;
  22. use app\common\cache\YlyPrinterCache;
  23. use app\common\enum\DeliveryEnum;
  24. use app\common\enum\OrderEnum;
  25. use app\common\enum\PrinterEnum;
  26. use app\common\enum\YesNoEnum;
  27. use app\common\model\Printer;
  28. use app\common\model\PrinterTemplate;
  29. use app\common\service\ConfigService;
  30. use App\Config\YlyConfig;
  31. use App\Oauth\YlyOauthClient;
  32. /**
  33. * 易联云打印服务
  34. */
  35. class YlyPrinterService
  36. {
  37. /**
  38. * @notes 获取access_token
  39. * @param $printer
  40. * @param $config
  41. * @return mixed
  42. * @author Tab
  43. * @date 2021/11/16 19:37
  44. */
  45. public function getAccessToken($printer, $config)
  46. {
  47. $token = (new YlyPrinterCache())->getAccessToken($printer['client_id']);
  48. if (!empty($token)) {
  49. return $token;
  50. }
  51. // 没有则调用接口去获取
  52. $client = new YlyOauthClient($config);
  53. $token = $client->getToken();
  54. $accessToken = $token->access_token;
  55. // 自有应用模式access_token无失效时间,接口访问频次:10次/日
  56. (new YlyPrinterCache())->setAccessToken($printer['client_id'], $accessToken, 20 * 24 * 3600);
  57. return $accessToken;
  58. }
  59. /**
  60. * @notes 获取打印模板
  61. * @param $printer
  62. * @return array
  63. * @author Tab
  64. * @date 2021/11/16 19:37
  65. */
  66. public function tempalte($printer)
  67. {
  68. $withoutField = 'create_time, update_time, delete_time';
  69. return PrinterTemplate::withoutField($withoutField)->findOrEmpty($printer['template_id'])->toArray();
  70. }
  71. /**
  72. * @notes 开始打印
  73. * @param $order
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\DbException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. * @author Tab
  78. * @date 2021/11/16 19:37
  79. */
  80. public function startPrint($order, $scene = PrinterEnum::ORDER_PRIINT)
  81. {
  82. // 打印机状态
  83. $status = false;
  84. // 打印机列表
  85. $printerLists = Printer::where('status', YesNoEnum::YES)->select()->toArray();
  86. foreach ($printerLists as $printer) {
  87. $response = (new YlyPrinterService())->getPrintStatus($printer);
  88. if (! in_array($response->body->state, [ 1, 2 ])) {
  89. continue;
  90. }
  91. $status = true;
  92. if ($scene == PrinterEnum::ORDER_PAY && !$printer['auto_print']) {
  93. // 订单支付但未开启打印跳过
  94. continue;
  95. }
  96. $template = $this->tempalte($printer);
  97. $this->singlePrint($printer, $order, $template);
  98. }
  99. return $status;
  100. }
  101. /**
  102. * @notes 添加打印机
  103. * @param $params
  104. * @return mixed
  105. * @author Tab
  106. * @date 2021/11/16 19:37
  107. */
  108. public function addPrinter($params)
  109. {
  110. $print = $this->getPrinter($params);
  111. $response = $print->addPrinter($params['machine_code'],$params['private_key'],$params['name']);
  112. return $response;
  113. }
  114. /**
  115. * @notes 删除打印机
  116. * @param $params
  117. * @author Tab
  118. * @date 2021/11/16 17:32
  119. */
  120. public function deletePrinter($params)
  121. {
  122. $print = $this->getPrinter($params);
  123. $print->deletePrinter($params['machine_code']);
  124. }
  125. /**
  126. * @notes 获取打印机状态
  127. * @param $params
  128. * @return mixed
  129. * @author Tab
  130. * @date 2021/11/16 18:04
  131. */
  132. public function getPrintStatus($params){
  133. $print = $this->getPrinter($params);
  134. $response = $print->getPrintStatus($params['machine_code']);
  135. return $response;
  136. }
  137. /**
  138. * @notes 获取打印机接口
  139. * @param $params
  140. * @return PrinterService
  141. * @author Tab
  142. * @date 2021/11/16 18:52
  143. */
  144. public function getPrinter($params)
  145. {
  146. $config = new YlyConfig($params['client_id'], $params['client_secret']);
  147. $accessToken = $this->getAccessToken($params, $config);
  148. return new PrinterService($accessToken, $config);
  149. }
  150. /**
  151. * @notes 获取打印接口
  152. * @param $params
  153. * @return PrintService
  154. * @author Tab
  155. * @date 2021/11/16 18:53
  156. */
  157. public function getPrint($params)
  158. {
  159. $config = new YlyConfig($params['client_id'], $params['client_secret']);
  160. $accessToken = $this->getAccessToken($params, $config);
  161. return new PrintService($accessToken, $config);
  162. }
  163. /**
  164. * @notes 单次打印
  165. * @param $printer
  166. * @param $order
  167. * @param $template
  168. * @author Tab
  169. * @date 2021/11/16 18:53
  170. */
  171. public function singlePrint($printer, $order, $template)
  172. {
  173. $shopName = ConfigService::get('shop', 'name');
  174. $print = $this->getPrint($printer);
  175. $content = "<MN>".$printer['print_number']."</MN>";
  176. $content .= "<center>".$template['ticket_name']."</center>" . PHP_EOL;
  177. $content .= str_repeat('-', 32).PHP_EOL;
  178. if ($template['show_shop_name'] && $shopName) {
  179. $content .= "<FS><center>".$shopName."</center></FS>";
  180. $content .= str_repeat('-', 32).PHP_EOL;
  181. }
  182. $content .= "订单编号:".$order['sn'].PHP_EOL;
  183. $content .= "支付方式:".$order['pay_way_desc'].PHP_EOL;
  184. $content .= "配送方式:".$order['delivery_type_desc'].PHP_EOL;
  185. $content .= str_repeat('-', 32).PHP_EOL;
  186. // 商品信息
  187. $all_original_money = 0;
  188. $content .= "<table><tr><td>商品信息</td><td>数量</td><td>单价</td></tr>";
  189. foreach ($order['orderGoods'] as $goods){
  190. $content .= "<tr><td>".mb_substr($goods['goods_snap']->goods_name, 0, 6)."</td>";
  191. $content .= "<td>".$goods['goods_num']."</td>";
  192. switch ($order['order_type']) {
  193. // 拼团 秒杀 砍价 使用实际价格
  194. case OrderEnum::TEAM_ORDER:
  195. case OrderEnum::SECKILL_ORDER:
  196. case OrderEnum::BARGAIN_ORDER:
  197. $content .= "<td>¥".$goods['goods_price']."</td></tr>";
  198. $all_original_money += $goods['goods_price'] * $goods['goods_num'];
  199. break;
  200. // 普通 虚拟 使用原价
  201. default:
  202. $content .= "<td>¥".$goods['original_price']."</td></tr>";
  203. $all_original_money += $goods['original_price'] * $goods['goods_num'];
  204. break;
  205. }
  206. }
  207. $content .= '</table>';
  208. $content .= str_repeat('-', 32).PHP_EOL;
  209. // 合计信息
  210. switch ($order['order_type']) {
  211. // 拼团 秒杀 砍价 使用实际价格
  212. case OrderEnum::TEAM_ORDER:
  213. case OrderEnum::SECKILL_ORDER:
  214. case OrderEnum::BARGAIN_ORDER:
  215. $content .= "<LR>订单原价:,¥". bcadd($order['goods_price'], 0, 2) . "</LR>";
  216. break;
  217. // 普通 虚拟 使用原价
  218. default:
  219. $content .= "<LR>订单原价:,¥". bcadd($all_original_money, 0, 2) . "</LR>";
  220. break;
  221. }
  222. $content .= "<LR>会员折扣:,-¥".$order['member_amount']."</LR>";
  223. $content .= "<LR>优惠券:,-¥".$order['discount_amount']."</LR>";
  224. $content .= "<LR>商品改价:," . ($order['change_price'] >= 0 ? "+" : "-") . "¥" . abs($order['change_price']) . "</LR>";
  225. $content .= "<LR>运费:,¥".$order['express_price']."</LR>";
  226. $content .= "<LR>实付金额:,¥".$order['order_amount']."</LR>";
  227. // 收货信息
  228. if ($order['delivery_type'] == DeliveryEnum::EXPRESS) {
  229. if($template['consignee_info']->name && $template['consignee_info']->mobile && $template['consignee_info']->address){
  230. $content .= str_repeat('-', 32).PHP_EOL;
  231. $content .= "收货信息".PHP_EOL;
  232. }
  233. if($template['consignee_info']->name){
  234. $content .= "收货人:".$order['address']->contact.PHP_EOL;
  235. }
  236. if($template['consignee_info']->mobile){
  237. $content .= "联系方式:".$order['address']->mobile.PHP_EOL;
  238. }
  239. if($template['consignee_info']->address){
  240. $content .= "收货地址:".$order['address']->address.PHP_EOL;
  241. }
  242. }
  243. // 自提订单
  244. if ($order['delivery_type'] == DeliveryEnum::SELF_DELIVERY) {
  245. //门店信息
  246. if($template['selffetch_shop']->shop_name || $template['selffetch_shop']->contacts || $template['selffetch_shop']->shop_address){
  247. $content .= str_repeat('-', 32).PHP_EOL;
  248. $content .= "门店信息".PHP_EOL;
  249. }
  250. if($template['selffetch_shop']->shop_name){
  251. $content .= "自提门店:".$order['selffetch_shop']['name'].PHP_EOL;
  252. }
  253. if($template['selffetch_shop']->contacts){
  254. $content .= "联系人:".$order['selffetch_shop']['contact'].PHP_EOL;
  255. }
  256. if($template['selffetch_shop']->shop_address){
  257. $content .= "门店地址:".$order['selffetch_shop']['detailed_address'].PHP_EOL;
  258. }
  259. //提货人信息
  260. if($template['verification_info']->contacts || $template['verification_info']->mobile || $template['verification_info']->pickup_code){
  261. $content .= str_repeat('-', 32).PHP_EOL;
  262. $content .= "提货人信息".PHP_EOL;
  263. }
  264. if($template['verification_info']->contacts){
  265. $content .= "提货人:".$order['address']->contact.PHP_EOL;
  266. }
  267. if($template['verification_info']->mobile){
  268. $content .= "联系方式:".$order['address']->mobile.PHP_EOL;
  269. }
  270. if($template['verification_info']->pickup_code){
  271. $content .= "提货码:".$order['pickup_code'].PHP_EOL;
  272. }
  273. }
  274. $content .= str_repeat('-', 32).PHP_EOL;
  275. // 买家留言
  276. if($template['show_buyer_message'] && $order['user_remark']){
  277. $content .= '买家留言:'.$order['user_remark'].PHP_EOL;
  278. $content .= str_repeat('-', 32).PHP_EOL;
  279. }
  280. // 二维码
  281. if($template['show_qrcode'] && $template['qrcode_name']){
  282. $content .= "<center>".$template['qrcode_name']."</center>".PHP_EOL;
  283. }
  284. if($template['show_qrcode'] && $template['qrcode_content']){
  285. $content .= "<QR>".$template['qrcode_content']."</QR>".PHP_EOL;
  286. }
  287. $content .= str_repeat('-', 32).PHP_EOL;
  288. // 底部信息
  289. $content .= "<center>".$template['bottom']."</center>".PHP_EOL;
  290. $print->index($printer['machine_code'], $content, $order['sn']);
  291. }
  292. }