Printer.php 2.7 KB

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