Printer.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的多应用管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\sys;
  12. use app\service\admin\sys\PrinterService;
  13. use app\service\admin\sys\PrinterTemplateService;
  14. use core\base\BaseAdminController;
  15. /**
  16. * 小票打印机控制器
  17. * Class Printer
  18. * @package app\adminapi\controller\sys
  19. */
  20. class Printer extends BaseAdminController
  21. {
  22. /**
  23. * 获取小票打印机分页列表
  24. * @return \think\Response
  25. */
  26. public function pages()
  27. {
  28. $data = $this->request->params([
  29. [ "printer_name", "" ],
  30. ]);
  31. return success(( new PrinterService() )->getPage($data));
  32. }
  33. /**
  34. * 获取小票打印机列表
  35. * @return \think\Response
  36. */
  37. public function lists()
  38. {
  39. $data = $this->request->params([
  40. [ "printer_name", "" ],
  41. ]);
  42. return success(( new PrinterService() )->getList($data));
  43. }
  44. /**
  45. * 小票打印机详情
  46. * @param int $id
  47. * @return \think\Response
  48. */
  49. public function info(int $id)
  50. {
  51. return success(( new PrinterService() )->getInfo($id));
  52. }
  53. /**
  54. * 添加小票打印机
  55. * @return \think\Response
  56. */
  57. public function add()
  58. {
  59. $data = $this->request->params([
  60. [ "brand", "" ],
  61. [ "printer_name", "" ],
  62. [ "printer_code", "" ],
  63. [ "printer_key", "" ],
  64. [ "open_id", "" ],
  65. [ "apikey", "" ],
  66. [ 'template_type', '' ],
  67. [ 'trigger', '' ],
  68. [ "value", "" ],
  69. [ "print_width", "" ],
  70. [ "status", 0 ],
  71. ]);
  72. $this->validate($data, 'app\validate\sys\Printer.add');
  73. $id = ( new PrinterService() )->add($data);
  74. return success('ADD_SUCCESS', [ 'id' => $id ]);
  75. }
  76. /**
  77. * 小票打印机编辑
  78. * @param $id 小票打印机id
  79. * @return \think\Response
  80. */
  81. public function edit(int $id)
  82. {
  83. $data = $this->request->params([
  84. [ "brand", "" ],
  85. [ "printer_name", "" ],
  86. [ "printer_code", "" ],
  87. [ "printer_key", "" ],
  88. [ "open_id", "" ],
  89. [ "apikey", "" ],
  90. [ 'template_type', '' ],
  91. [ 'trigger', '' ],
  92. [ "value", "" ],
  93. [ "print_width", "" ],
  94. [ "status", 0 ],
  95. ]);
  96. $this->validate($data, 'app\validate\sys\Printer.edit');
  97. ( new PrinterService() )->edit($id, $data);
  98. return success('EDIT_SUCCESS');
  99. }
  100. /**
  101. * 修改小票打印机状态
  102. * @return \think\Response
  103. */
  104. public function modifyStatus()
  105. {
  106. $data = $this->request->params([
  107. [ "printer_id", 0 ],
  108. [ "status", 0 ],
  109. ]);
  110. ( new PrinterService() )->modifyStatus($data);
  111. return success('SUCCESS');
  112. }
  113. /**
  114. * 小票打印机删除
  115. * @param $id 小票打印机id
  116. * @return \think\Response
  117. */
  118. public function del(int $id)
  119. {
  120. ( new PrinterService() )->del($id);
  121. return success('DELETE_SUCCESS');
  122. }
  123. /**
  124. * 获取小票打印模板分页列表
  125. * @return \think\Response
  126. */
  127. public function templatePageLists()
  128. {
  129. $data = $this->request->params([
  130. [ "template_type", "" ],
  131. [ "template_name", "" ],
  132. ]);
  133. return success(( new PrinterTemplateService() )->getPage($data));
  134. }
  135. /**
  136. * 获取小票打印模板列表
  137. * @return \think\Response
  138. * @throws \think\db\exception\DbException
  139. */
  140. public function templateLists()
  141. {
  142. $data = $this->request->params([
  143. [ "template_type", "" ],
  144. [ "template_name", "" ],
  145. ]);
  146. return success(( new PrinterTemplateService() )->getList($data));
  147. }
  148. /**
  149. * 小票打印模板详情
  150. * @param int $id
  151. * @return \think\Response
  152. */
  153. public function templateInfo(int $id)
  154. {
  155. return success(( new PrinterTemplateService() )->getInfo($id));
  156. }
  157. /**
  158. * 添加小票打印模板
  159. * @return \think\Response
  160. */
  161. public function templateAdd()
  162. {
  163. $data = $this->request->params([
  164. [ "template_type", "" ],
  165. [ "template_name", "" ],
  166. [ "value", "" ],
  167. ]);
  168. $this->validate($data, 'app\validate\sys\PrinterTemplate.add');
  169. $id = ( new PrinterTemplateService() )->add($data);
  170. return success('ADD_SUCCESS', [ 'id' => $id ]);
  171. }
  172. /**
  173. * 小票打印模板编辑
  174. * @param $id 小票打印模板id
  175. * @return \think\Response
  176. */
  177. public function templateEdit(int $id)
  178. {
  179. $data = $this->request->params([
  180. [ "template_type", "" ],
  181. [ "template_name", "" ],
  182. [ "value", "" ],
  183. ]);
  184. $this->validate($data, 'app\validate\sys\PrinterTemplate.edit');
  185. ( new PrinterTemplateService() )->edit($id, $data);
  186. return success('EDIT_SUCCESS');
  187. }
  188. /**
  189. * 小票打印模板删除
  190. * @param $id 小票打印模板id
  191. * @return \think\Response
  192. */
  193. public function templateDel(int $id)
  194. {
  195. ( new PrinterTemplateService() )->del($id);
  196. return success('DELETE_SUCCESS');
  197. }
  198. /**
  199. * 获取小票打印模板类型
  200. * @return array|\think\Response
  201. */
  202. public function getType()
  203. {
  204. return success(( new PrinterService() )->getType());
  205. }
  206. /**
  207. * 获取小票打印机设备品牌
  208. * @return array|\think\Response
  209. */
  210. public function getBrand()
  211. {
  212. $data = $this->request->params([
  213. [ "brand", "" ],
  214. ]);
  215. return success(( new PrinterService() )->getBrand($data[ 'brand' ]));
  216. }
  217. /**
  218. * 测试打印
  219. * @param int $id
  220. * @return \think\Response
  221. */
  222. public function testPrint(int $id)
  223. {
  224. ( new PrinterService() )->testPrint($id);
  225. return success('SUCCESS');
  226. }
  227. /**
  228. * 刷新打印机token
  229. * @param int $id
  230. * @return \think\Response
  231. */
  232. public function refreshToken(int $id)
  233. {
  234. ( new PrinterService() )->refreshToken($id);
  235. return success('SUCCESS');
  236. }
  237. /**
  238. * 打印小票内容
  239. * @return \think\Response
  240. */
  241. public function printTicket()
  242. {
  243. $data = $this->request->params([
  244. [ "type", "" ], // 小票模板类型
  245. [ "trigger", "" ], // 触发时机
  246. [ 'business', [] ] // 业务参数,根据自身业务传值
  247. ]);
  248. $res = ( new PrinterService() )->printTicket($data);
  249. if ($res[ 'code' ] == 0) {
  250. return success('SUCCESS');
  251. } else {
  252. return fail($res[ 'message' ]);
  253. }
  254. }
  255. }