Printer.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\common\service\order;
  10. use app\common\model\Setting as SettingModel;
  11. use app\common\model\Printer as PrinterModel;
  12. use app\common\enum\DeliveryType as DeliveryTypeEnum;
  13. use app\common\library\printer\Driver as PrinterDriver;
  14. /**
  15. * 订单打印服务类
  16. * Class Printer
  17. * @package app\common\service\order
  18. */
  19. class Printer
  20. {
  21. /**
  22. * 执行订单打印
  23. * @param \app\common\model\BaseModel $order 订单信息
  24. * @param int $scene 场景
  25. * @return bool
  26. * @throws \app\common\exception\BaseException
  27. * @throws \think\exception\DbException
  28. */
  29. public function printTicket($order, $scene)
  30. {
  31. // 打印机设置
  32. $printerConfig = SettingModel::getItem('printer', $order['wxapp_id']);
  33. // 判断是否开启打印设置
  34. if (!$printerConfig['is_open']
  35. || !$printerConfig['printer_id']
  36. || !in_array($scene, $printerConfig['order_status'])) {
  37. return false;
  38. }
  39. // 获取当前的打印机
  40. $printer = PrinterModel::detail($printerConfig['printer_id']);
  41. if (empty($printer) || $printer['is_delete']) {
  42. return false;
  43. }
  44. // 实例化打印机驱动
  45. $PrinterDriver = new PrinterDriver($printer);
  46. // 获取订单打印内容
  47. $content = $this->getPrintContent($order);
  48. // 执行打印请求
  49. return $PrinterDriver->printTicket($content);
  50. }
  51. /**
  52. * 构建订单打印的内容
  53. * @param \app\common\model\BaseModel $order
  54. * @return string
  55. */
  56. private function getPrintContent($order)
  57. {
  58. // 商城名称
  59. $storeName = SettingModel::getItem('store', $order['wxapp_id'])['name'];
  60. // 收货地址
  61. /* @var \app\common\model\OrderAddress $address */
  62. $address = $order['address'];
  63. // 拼接模板内容
  64. $content = "<CB>{$storeName}</CB><BR>";
  65. $content .= '--------------------------------<BR>';
  66. $content .= "昵称:{$order['user']['nickName']} [{$order['user_id']}]<BR>";
  67. $content .= "订单号:{$order['order_no']}<BR>";
  68. $content .= '付款时间:' . date('Y-m-d H:i:s', $order['pay_time']) . '<BR>';
  69. // 收货人信息
  70. if ($order['delivery_type']['value'] == DeliveryTypeEnum::EXPRESS) {
  71. $content .= "--------------------------------<BR>";
  72. $content .= "收货人:{$address['name']}<BR>";
  73. $content .= "联系电话:{$address['phone']}<BR>";
  74. $content .= '收货地址:' . $address->getFullAddress() . '<BR>';
  75. }
  76. // 自提信息
  77. if ($order['delivery_type']['value'] == DeliveryTypeEnum::EXTRACT && !empty($order['extract'])) {
  78. $content .= "--------------------------------<BR>";
  79. $content .= "联系人:{$order['extract']['linkman']}<BR>";
  80. $content .= "联系电话:{$order['extract']['phone']}<BR>";
  81. $content .= "自提门店:{$order['extract_shop']['shop_name']}<BR>";
  82. }
  83. // 商品信息
  84. $content .= '=========== 商品信息 ===========<BR>';
  85. foreach ($order['goods'] as $key => $goods) {
  86. $content .= ($key + 1) . ".商品名称:{$goods['goods_name']}<BR>";
  87. !empty($goods['goods_attr']) && $content .= " 商品规格:{$goods['goods_attr']}<BR>";
  88. $content .= " 购买数量:{$goods['total_num']}<BR>";
  89. $content .= " 商品总价:{$goods['total_price']}元<BR>";
  90. $content .= '--------------------------------<BR>';
  91. }
  92. // 买家备注
  93. if (!empty($order['buyer_remark'])) {
  94. $content .= '============ 买家备注 ============<BR>';
  95. $content .= "<B>{$order['buyer_remark']}</B><BR>";
  96. $content .= '--------------------------------<BR>';
  97. }
  98. // 订单金额
  99. if ($order['coupon_money'] > 0) {
  100. $content .= "优惠券:-{$order['coupon_money']}元<BR>";
  101. }
  102. if ($order['points_num'] > 0) {
  103. $content .= "积分抵扣:-{$order['points_money']}元<BR>";
  104. }
  105. if ($order['update_price']['value'] != '0.00') {
  106. $content .= "后台改价:{$order['update_price']['symbol']}{$order['update_price']['value']}元<BR>";
  107. }
  108. // 运费
  109. if ($order['delivery_type']['value'] == DeliveryTypeEnum::EXPRESS) {
  110. $content .= "运费:{$order['express_price']}元<BR>";
  111. $content .= '------------------------------<BR>';
  112. }
  113. // 实付款
  114. $content .= "<RIGHT>实付款:<BOLD><B>{$order['pay_price']}</B></BOLD>元</RIGHT><BR>";
  115. return $content;
  116. }
  117. }