StorePrinter.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\merchant\store;
  12. use app\common\repositories\store\ExcelRepository;
  13. use app\common\repositories\store\StorePrinterRepository;
  14. use crmeb\exceptions\UploadException;
  15. use crmeb\services\ExcelService;
  16. use think\App;
  17. use crmeb\basic\BaseController;
  18. class StorePrinter extends BaseController
  19. {
  20. protected $repository;
  21. /**
  22. * 构造函数
  23. *
  24. * @param App $app 应用实例
  25. * @param StorePrinterRepository $repository 打印机仓库实例
  26. */
  27. public function __construct(App $app, StorePrinterRepository $repository)
  28. {
  29. // 调用父类构造函数
  30. parent::__construct($app);
  31. // 初始化仓库实例
  32. $this->repository = $repository;
  33. }
  34. /**
  35. * 获取打印机列表
  36. *
  37. * @return mixed
  38. */
  39. public function lst()
  40. {
  41. // 获取分页参数
  42. [$page, $limit] = $this->getPage();
  43. $where = $this->request->params(['status', 'keyword','type']);
  44. // 添加商家ID条件
  45. $where['mer_id'] = $this->request->merId();
  46. $data = $this->repository->merList($where, $page, $limit);
  47. // 返回成功响应
  48. return app('json')->success($data);
  49. }
  50. /**
  51. * 创建打印机表单
  52. *
  53. * @return mixed
  54. */
  55. public function createForm()
  56. {
  57. // 调用仓库方法获取表单数据并转换为数组
  58. return app('json')->success(formToData($this->repository->form(null)));
  59. }
  60. /**
  61. * 创建打印机信息
  62. *
  63. * @return \think\response\Json
  64. */
  65. public function create()
  66. {
  67. // 获取请求参数
  68. $params = $this->request->params([
  69. 'type',
  70. 'printer_name',
  71. 'printer_appkey',
  72. 'printer_appid',
  73. 'printer_secret',
  74. 'printer_terminal',
  75. 'status',
  76. 'times',
  77. 'print_type',
  78. ]);
  79. // 判断参数是否完整
  80. if (!$params['printer_name'] ||
  81. !$params['printer_appid'] ||
  82. !$params['printer_appkey'] ||
  83. !$params['printer_terminal']
  84. ) {
  85. return app('json')->fail('信息不完整');
  86. }
  87. if (!is_int($params['times'])) return app('json')->fail('打印联数必须为整数');
  88. // 设置商家ID
  89. $params['mer_id'] = $this->request->merId();
  90. // 调用仓库的创建方法
  91. $this->repository->create($params);
  92. // 返回成功信息
  93. return app('json')->success('添加成功');
  94. }
  95. /**
  96. * 获取打印机表单数据
  97. *
  98. * @param int $id 打印机ID
  99. * @return \think\response\Json
  100. */
  101. public function updateForm($id)
  102. {
  103. // 调用仓库的表单方法,并转换为数据格式
  104. return app('json')->success(formToData($this->repository->form($id)));
  105. }
  106. /**
  107. * 更新打印机信息
  108. * @param int $id 打印机ID
  109. * @return \think\response\Json
  110. */
  111. public function update($id)
  112. {
  113. // 获取请求参数
  114. $params = $this->request->params([
  115. 'type',
  116. 'printer_name',
  117. 'printer_appkey',
  118. 'printer_appid',
  119. 'printer_secret',
  120. 'printer_terminal',
  121. 'status',
  122. 'times',
  123. 'print_type',
  124. ]);
  125. // 判断参数是否完整
  126. if (!$params['printer_name'] ||
  127. !$params['printer_appid'] ||
  128. !$params['printer_appkey'] ||
  129. !$params['printer_terminal']
  130. ) {
  131. return app('json')->fail('信息不完整');
  132. }
  133. if (!is_int($params['times'])) return app('json')->fail('打印联数必须为整数');
  134. // 查询打印机信息
  135. $res = $this->repository->getWhere(['printer_id' => $id, 'mer_id' => $this->request->merId()]);
  136. if (!$res) return app('json')->fail('打印机信息不存在');
  137. // 更新打印机信息
  138. $this->repository->update($id, $params);
  139. return app('json')->success('编辑成功');
  140. }
  141. /**
  142. * 删除打印机信息
  143. * @param int $id 打印机ID
  144. * @return \think\response\Json
  145. */
  146. public function delete($id)
  147. {
  148. // 查询打印机信息
  149. $res = $this->repository->getWhere(['printer_id' => $id, 'mer_id' => $this->request->merId()]);
  150. if (!$res) return app('json')->fail('打印机信息不存在');
  151. // 删除打印机信息
  152. $this->repository->delete($id);
  153. return app('json')->success('删除成功');
  154. }
  155. /**
  156. * 切换打印机状态
  157. * @param int $id 打印机ID
  158. * @return \think\response\Json
  159. */
  160. public function switchWithStatus($id)
  161. {
  162. $status = $this->request->param('status') == 1 ? 1 : 0;
  163. // 更新打印机状态
  164. $this->repository->update($id, ['status' => $status]);
  165. return app('json')->success('修改成功');
  166. }
  167. /**
  168. * 根据打印机ID获取内容信息
  169. *
  170. * 本函数通过调用repository的getWhere方法,根据提供的打印机ID($id)和商户ID(mer_id),
  171. * 获取对应的记录如果找不到匹配的记录,则返回错误信息提示打印机信息不存在
  172. * 找到记录后,将记录的内容(content)从JSON字符串格式解码为PHP关联数组,
  173. * 并将内容信息返回成功响应
  174. *
  175. * @param int $id 打印机ID,用于查询特定的打印机信息
  176. * @return \Illuminate\Http\JsonResponse 返回JSON格式的成功或失败响应
  177. */
  178. public function getContent($id)
  179. {
  180. // 根据打印机ID和商户ID获取记录
  181. $res = $this->repository->getWhere(['printer_id' => $id, 'mer_id' => $this->request->merId()]);
  182. // 如果没有找到记录,返回失败响应
  183. if (!$res) return app('json')->fail('打印机信息不存在');
  184. // 将记录的内容从JSON字符串解码为关联数组
  185. $print_content = json_decode($res->print_content, true) == null ? [] : json_decode($res->print_content, true);
  186. // 返回成功响应,包含解码后的内容信息
  187. return app('json')->success(compact('print_content'));
  188. }
  189. /**
  190. * 设置内容
  191. *
  192. * 此方法用于更新特定项的内容信息它从请求参数中获取内容信息,
  193. * 然后更新到存储库中的指定项上,最后返回操作结果
  194. *
  195. * @param mixed $id 项的唯一标识符,用于定位需要更新的项
  196. * @return mixed 操作结果,通常为JSON格式的成功响应
  197. */
  198. public function setContent($id)
  199. {
  200. // 从请求中获取需要设置的内容
  201. $content = $this->request->param('print_content');
  202. // 将获取到的内容转换为JSON格式并存储到指定项的内容字段中
  203. $this->repository->update($id, ['print_content' => json_encode($content, true)]);
  204. // 返回成功消息,表示内容设置成功
  205. return app('json')->success('修改成功');
  206. }
  207. }