Printer.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\store\controller\setting;
  10. use app\store\controller\Controller;
  11. use app\store\model\Printer as PrinterModel;
  12. /**
  13. * 小票打印机管理
  14. * Class Printer
  15. * @package app\store\controller\setting
  16. */
  17. class Printer extends Controller
  18. {
  19. /**
  20. * 打印机列表
  21. * @return mixed
  22. * @throws \think\exception\DbException
  23. */
  24. public function index()
  25. {
  26. $model = new PrinterModel;
  27. $list = $model->getList();
  28. return $this->fetch('index', compact('list'));
  29. }
  30. /**
  31. * 添加打印机
  32. * @return array|mixed
  33. */
  34. public function add()
  35. {
  36. $model = new PrinterModel;
  37. if (!$this->request->isAjax()) {
  38. // 打印机类型列表
  39. $printerType = $model::getPrinterTypeList();
  40. return $this->fetch('add', compact('printerType'));
  41. }
  42. // 新增记录
  43. if ($model->add($this->postData('printer'))) {
  44. return $this->renderSuccess('添加成功', url('setting.printer/index'));
  45. }
  46. return $this->renderError($model->getError() ?: '添加失败');
  47. }
  48. /**
  49. * 编辑打印机
  50. * @param $printer_id
  51. * @return array|mixed
  52. * @throws \think\exception\DbException
  53. */
  54. public function edit($printer_id)
  55. {
  56. // 模板详情
  57. $model = PrinterModel::detail($printer_id);
  58. if (!$this->request->isAjax()) {
  59. // 打印机类型列表
  60. $printerType = $model::getPrinterTypeList();
  61. return $this->fetch('edit', compact('model', 'printerType'));
  62. }
  63. // 更新记录
  64. if ($model->edit($this->postData('printer'))) {
  65. return $this->renderSuccess('更新成功', url('setting.printer/index'));
  66. }
  67. return $this->renderError($model->getError() ?: '更新失败');
  68. }
  69. /**
  70. * 删除打印机
  71. * @param $printer_id
  72. * @return array
  73. * @throws \think\exception\DbException
  74. */
  75. public function delete($printer_id)
  76. {
  77. $model = PrinterModel::detail($printer_id);
  78. if (!$model->setDelete()) {
  79. return $this->renderError($model->getError() ?: '删除失败');
  80. }
  81. return $this->renderSuccess('删除成功');
  82. }
  83. /**
  84. * 测试打印接口
  85. * @param int $order_id
  86. * @throws \app\common\exception\BaseException
  87. * @throws \think\exception\DbException
  88. */
  89. public function test($order_id = 180)
  90. {
  91. // 订单信息
  92. $order = \app\store\model\Order::detail($order_id);
  93. // 实例化打印机驱动
  94. $Printer = new \app\common\service\order\Printer();
  95. $Printer->printTicket($order, \app\common\enum\OrderStatus::ORDER_PAYMENT);
  96. }
  97. }