Printer.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\service\order;
  13. use app\common\model\OrderAddress;
  14. use app\common\model\Store as StoreModel;
  15. use app\common\model\Printer as PrinterModel;
  16. use app\common\model\store\Setting as SettingModel;
  17. use app\common\enum\order\DeliveryType as DeliveryTypeEnum;
  18. use app\common\library\printer\Driver as PrinterDriver;
  19. use app\common\service\BaseService;
  20. use app\common\service\Goods as GoodsService;
  21. use cores\exception\BaseException;
  22. /**
  23. * 订单打印服务类
  24. * Class Printer
  25. * @package app\common\service\order
  26. */
  27. class Printer extends BaseService
  28. {
  29. /**
  30. * 执行订单打印 (手动)
  31. * @param mixed $order 订单信息
  32. * @param int $printerId 打印机ID
  33. * @return bool|mixed
  34. * @throws BaseException
  35. */
  36. public function printEvent($order, int $printerId)
  37. {
  38. // 获取当前的打印机
  39. $printer = PrinterModel::detail($printerId);
  40. if (empty($printer) || $printer['is_delete']) {
  41. return false;
  42. }
  43. // 实例化打印机驱动
  44. $PrinterDriver = new PrinterDriver($printer);
  45. // 获取订单打印内容
  46. $content = $this->getPrintContent($order);
  47. // 执行打印请求
  48. $status = $PrinterDriver->printTicket($content);
  49. if ($status === false) {
  50. $this->error = $PrinterDriver->getError();
  51. }
  52. return $status;
  53. }
  54. /**
  55. * 执行订单打印 (自动)
  56. * @param mixed $order 订单信息
  57. * @param int $scene 场景值
  58. * @return bool
  59. * @throws BaseException
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. */
  64. public function printTicket($order, int $scene): bool
  65. {
  66. // 打印机设置
  67. $printerConfig = SettingModel::getItem('printer', $order['store_id']);
  68. // 判断是否开启打印设置
  69. if (!$printerConfig['is_open']
  70. || !$printerConfig['printer_id']
  71. || !in_array($scene, $printerConfig['order_status'])) {
  72. return false;
  73. }
  74. // 获取当前的打印机
  75. $printer = PrinterModel::detail($printerConfig['printer_id']);
  76. if (empty($printer) || $printer['is_delete']) {
  77. return false;
  78. }
  79. // 实例化打印机驱动
  80. $PrinterDriver = new PrinterDriver($printer);
  81. // 获取订单打印内容
  82. $content = $this->getPrintContent($order);
  83. // 执行打印请求
  84. return $PrinterDriver->printTicket($content);
  85. }
  86. /**
  87. * 构建订单打印的内容
  88. * @param mixed $order
  89. * @return string
  90. */
  91. private function getPrintContent($order): string
  92. {
  93. // 商城信息
  94. $storeInfo = StoreModel::detail($order['store_id']);
  95. // 收货地址
  96. /* @var OrderAddress $address */
  97. $address = $order['address'];
  98. // 拼接模板内容
  99. $content = "<CB>{$storeInfo['store_name']}</CB><BR>";
  100. $content .= '--------------------------------<BR>';
  101. $content .= "昵称:{$order['user']['nick_name']} [{$order['user_id']}]<BR>";
  102. $content .= "订单号:{$order['order_no']}<BR>";
  103. $content .= "付款时间:{$order['pay_time']}<BR>";
  104. // 收货人信息
  105. if ($order['delivery_type'] == DeliveryTypeEnum::EXPRESS) {
  106. $content .= "--------------------------------<BR>";
  107. $content .= "收货人:{$address['name']}<BR>";
  108. $content .= "联系电话:{$address['phone']}<BR>";
  109. $content .= '收货地址:' . $address->getFullAddress() . '<BR>';
  110. }
  111. // 商品信息
  112. $content .= '=========== 商品信息 ===========<BR>';
  113. foreach ($order['goods'] as $key => $goods) {
  114. $content .= ($key + 1) . ".商品名称:{$goods['goods_name']}<BR>";
  115. if (!empty($goods['goods_props'])) {
  116. $content .= ' 商品规格:' . GoodsService::goodsPropsToAttr($goods['goods_props']) . '<BR>';
  117. }
  118. $content .= " 购买数量:{$goods['total_num']}<BR>";
  119. $content .= " 商品总价:{$goods['total_price']}元<BR>";
  120. $content .= '--------------------------------<BR>';
  121. }
  122. // 买家备注
  123. if (!empty($order['buyer_remark'])) {
  124. $content .= '============ 买家备注 ============<BR>';
  125. $content .= "<B>{$order['buyer_remark']}</B><BR>";
  126. $content .= '--------------------------------<BR>';
  127. }
  128. // 订单金额
  129. if ($order['coupon_money'] > 0) {
  130. $content .= "优惠券:-{$order['coupon_money']}元<BR>";
  131. }
  132. if ($order['points_num'] > 0) {
  133. $content .= "积分抵扣:-{$order['points_money']}元<BR>";
  134. }
  135. if ($order['update_price']['value'] != '0.00') {
  136. $content .= "后台改价:{$order['update_price']['symbol']}{$order['update_price']['value']}元<BR>";
  137. }
  138. // 运费
  139. if ($order['delivery_type'] == DeliveryTypeEnum::EXPRESS) {
  140. $content .= "运费:{$order['express_price']}元<BR>";
  141. $content .= '------------------------------<BR>';
  142. }
  143. // 实付款
  144. $content .= "<RIGHT>实付款:<BOLD><B>{$order['pay_price']}</B></BOLD>元</RIGHT><BR>";
  145. return $content;
  146. }
  147. }