Printer.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\DbException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. */
  39. public function printEvent($order, int $printerId)
  40. {
  41. // 获取当前的打印机
  42. $printer = PrinterModel::detail($printerId);
  43. if (empty($printer) || $printer['is_delete']) {
  44. return false;
  45. }
  46. // 实例化打印机驱动
  47. $PrinterDriver = new PrinterDriver($printer);
  48. // 获取订单打印内容
  49. $content = $this->getPrintContent($order);
  50. // 执行打印请求
  51. $status = $PrinterDriver->printTicket($content);
  52. if ($status === false) {
  53. $this->error = $PrinterDriver->getError();
  54. }
  55. return $status;
  56. }
  57. /**
  58. * 执行订单打印 (自动)
  59. * @param mixed $order 订单信息
  60. * @param int $scene 场景值
  61. * @return bool
  62. * @throws BaseException
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. */
  67. public function printTicket($order, int $scene): bool
  68. {
  69. // 打印机设置
  70. $printerConfig = SettingModel::getItem('printer', $order['store_id']);
  71. // 判断是否开启打印设置
  72. if (!$printerConfig['is_open']
  73. || !$printerConfig['printer_id']
  74. || !in_array($scene, $printerConfig['order_status'])) {
  75. return false;
  76. }
  77. // 获取当前的打印机
  78. $printer = PrinterModel::detail($printerConfig['printer_id']);
  79. if (empty($printer) || $printer['is_delete']) {
  80. return false;
  81. }
  82. // 实例化打印机驱动
  83. $PrinterDriver = new PrinterDriver($printer);
  84. // 获取订单打印内容
  85. $content = $this->getPrintContent($order);
  86. // 执行打印请求
  87. return $PrinterDriver->printTicket($content);
  88. }
  89. /**
  90. * 构建订单打印的内容
  91. * @param $order
  92. * @return string
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\DbException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. */
  97. private function getPrintContent($order): string
  98. {
  99. // 商城信息
  100. $storeInfo = StoreModel::detail($order['store_id']);
  101. // 收货地址
  102. /* @var OrderAddress $address */
  103. $address = $order['address'];
  104. // 拼接模板内容
  105. $content = "<CB>{$storeInfo['store_name']}</CB><BR>";
  106. $content .= '--------------------------------<BR>';
  107. $content .= "昵称:{$order['user']['nick_name']} [{$order['user_id']}]<BR>";
  108. $content .= "订单号:{$order['order_no']}<BR>";
  109. $content .= "付款时间:{$order['pay_time']}<BR>";
  110. // 收货人信息
  111. if ($order['delivery_type'] == DeliveryTypeEnum::EXPRESS) {
  112. $content .= "--------------------------------<BR>";
  113. $content .= "收货人:{$address['name']}<BR>";
  114. $content .= "联系电话:{$address['phone']}<BR>";
  115. $content .= '收货地址:' . $address->getFullAddress() . '<BR>';
  116. }
  117. // 商品信息
  118. $content .= '=========== 商品信息 ===========<BR>';
  119. foreach ($order['goods'] as $key => $goods) {
  120. $content .= ($key + 1) . ".商品名称:{$goods['goods_name']}<BR>";
  121. if (!empty($goods['goods_props'])) {
  122. $content .= ' 商品规格:' . GoodsService::goodsPropsToAttr($goods['goods_props']) . '<BR>';
  123. }
  124. $content .= " 购买数量:{$goods['total_num']}<BR>";
  125. $content .= " 商品总价:{$goods['total_price']}元<BR>";
  126. $content .= '--------------------------------<BR>';
  127. }
  128. // 买家备注
  129. if (!empty($order['buyer_remark'])) {
  130. $content .= '============ 买家备注 ============<BR>';
  131. $content .= "<B>{$order['buyer_remark']}</B><BR>";
  132. $content .= '--------------------------------<BR>';
  133. }
  134. // 订单金额
  135. if ($order['coupon_money'] > 0) {
  136. $content .= "优惠券:-{$order['coupon_money']}元<BR>";
  137. }
  138. if ($order['points_num'] > 0) {
  139. $content .= "积分抵扣:-{$order['points_money']}元<BR>";
  140. }
  141. if ($order['update_price']['value'] != '0.00') {
  142. $content .= "后台改价:{$order['update_price']['symbol']}{$order['update_price']['value']}元<BR>";
  143. }
  144. // 运费
  145. if ($order['delivery_type'] == DeliveryTypeEnum::EXPRESS) {
  146. $content .= "运费:{$order['express_price']}元<BR>";
  147. $content .= '------------------------------<BR>';
  148. }
  149. // 实付款
  150. $content .= "<RIGHT>实付款:<BOLD><B>{$order['pay_price']}</B></BOLD>元</RIGHT><BR>";
  151. return $content;
  152. }
  153. }