Printer.php 4.6 KB

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