Prints.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace app\controller\admin;
  3. use app\extra\basic\Base;
  4. use app\extra\tools\PrintExtend;
  5. use app\middleware\AuthMiddleware;
  6. use app\model\saas\SaasPrint;
  7. use app\service\saas\PrintService;
  8. use app\validate\saas\PrintValidate;
  9. use DI\Attribute\Inject;
  10. use LinFly\Annotation\Attributes\Route\Controller;
  11. use LinFly\Annotation\Attributes\Route\GetMapping;
  12. use LinFly\Annotation\Attributes\Route\Middleware;
  13. use LinFly\Annotation\Attributes\Route\PostMapping;
  14. use support\Request;
  15. use support\Response;
  16. use Webman\RedisQueue\Redis;
  17. #[Controller("/api/prints"),Middleware(AuthMiddleware::class)]
  18. class Prints extends Base
  19. {
  20. #[Inject]
  21. protected PrintService $service;
  22. #[Inject]
  23. protected PrintValidate $validate;
  24. #[Inject]
  25. protected SaasPrint $model;
  26. #[GetMapping('list')]
  27. public function getStoreList(Request $request): Response
  28. {
  29. try {
  30. $param = $request->all();
  31. $list = $this->service->setModel()->getList($param,['poi']);
  32. return successTrans("success.data",pageFormat($list),200);
  33. } catch (\Throwable $throwable) {
  34. echo $throwable->getFile()."\n";
  35. echo $throwable->getLine()."\n";
  36. return error($throwable->getMessage());
  37. }
  38. }
  39. /**
  40. * 查询打印机状态
  41. * @return Response
  42. */
  43. #[PostMapping('refresh')]
  44. public function refreshState(): Response
  45. {
  46. try {
  47. Redis::send("print-state",[]);
  48. return successTrans("success.print-state");
  49. } catch (\Throwable $throwable) {
  50. return error($throwable->getMessage());
  51. }
  52. }
  53. /**
  54. * 打印测试
  55. * @param Request $request
  56. * @return Response
  57. */
  58. #[PostMapping('test')]
  59. public function testPrint(Request $request): Response
  60. {
  61. try {
  62. $param = $this->_valid([
  63. "sn.require" => trans("empty.require")
  64. ],"post");
  65. if (!is_array($param)) return error($param);
  66. $print = $this->model->where("sn",$param["sn"])->findOrEmpty();
  67. if ($print->isEmpty()) return errorTrans("empty.data");
  68. $resp = (new PrintExtend)->config(['appid' => sConf('wechat.ex_appid'),'secret' => sConf('wechat.ex_secret')])->printTemplate(sConf('wechat.ex_print'),$param['sn'],[
  69. [
  70. "express" => [
  71. [
  72. "j_mobile" => "180****9980",
  73. "j_address" =>"安徽省阜阳市颍州区xxx镇xxx村xxx",
  74. "express_num" => "SF3270714680386",
  75. "name" => "测试的商品标题名称测试的商品标题名称测试的商品标题名称测试的商品标题名称测试的商品标题名称",
  76. "time" => getDateFull(),
  77. "type" => "特快",
  78. "s_name" => "王*二",
  79. "s_address" => "安徽省阜阳市颍州区xxx镇xxx村xxx",
  80. "s_mobile" => "******00982",
  81. "j_name" => "王麻子",
  82. "express_type" => "饰品",
  83. "order" => "1033999288123123"
  84. ]
  85. ]
  86. ]
  87. ]);
  88. if (!$resp['status']) return error($resp['message']);
  89. return successTrans("success.print");
  90. } catch (\Throwable $throwable) {
  91. return error($throwable->getMessage());
  92. }
  93. }
  94. /**
  95. * 新增/编辑打印机
  96. * @param Request $request
  97. * @return Response
  98. */
  99. #[PostMapping('save')]
  100. public function save(Request $request): Response
  101. {
  102. try {
  103. $param = $request->post();
  104. if (!$this->validate->check($param)) return error($this->validate->getError());
  105. if (!isset($param['id'])) {
  106. $resp = (new PrintExtend)->config(['appid' => sConf('wechat.ex_appid'),'secret' => sConf('wechat.ex_secret')])->bindDevice($param['sn'],$param['name']);
  107. if (!$resp['status']) return error($resp['message']);
  108. }
  109. $state = $this->model->setAutoData($param);
  110. if (!$state) return errorTrans("error.data");
  111. return successTrans("success.data");
  112. } catch (\Throwable $throwable) {
  113. echo $throwable->getMessage()."\n";
  114. echo $throwable->getFile()."\n";
  115. echo $throwable->getLine()."\n";
  116. return error($throwable->getMessage());
  117. }
  118. }
  119. /**
  120. * 新增/编辑打印机
  121. * @param Request $request
  122. * @return Response
  123. */
  124. #[PostMapping('edit')]
  125. public function edit(Request $request): Response
  126. {
  127. try {
  128. $param = $request->post();
  129. if (!$this->validate->scene('edit')->check($request->post())) return error($this->validate->getError());
  130. $state = $this->model->setAutoData($param);
  131. if (!$state) return errorTrans("error.data");
  132. return successTrans("success.data");
  133. } catch (\Throwable $throwable) {
  134. echo $throwable->getMessage()."\n";
  135. echo $throwable->getFile()."\n";
  136. echo $throwable->getLine()."\n";
  137. return error($throwable->getMessage());
  138. }
  139. }
  140. /**
  141. * @param Request $request
  142. * @return Response
  143. */
  144. #[PostMapping('batch')]
  145. public function setBatchData(Request $request): Response
  146. {
  147. try {
  148. $param = $this->_valid([
  149. "id.require" => trans("empty.require"),
  150. "value.require" => trans("empty.require"),
  151. "field.require" => trans("empty.require"),
  152. "type.require" => trans("empty.require"),
  153. ],"post");
  154. if (!is_array($param)) return error($param);
  155. if ($param['type'] == "batch") {
  156. $state = $this->model->where("id","in",$param['id'])->save([$param['field'] => $param['value']]);
  157. } else {
  158. $state = $this->model->where("id",$param['id'])->save([$param['field'] => $param['value']]);
  159. }
  160. if (!$state) return errorTrans("error.data");
  161. return successTrans("success.data");
  162. } catch (\Throwable $throwable) {
  163. return error($throwable->getMessage());
  164. }
  165. }
  166. /**
  167. * 删除
  168. * @param Request $request
  169. * @return Response
  170. */
  171. #[PostMapping('del')]
  172. public function delUser(Request $request): Response
  173. {
  174. try {
  175. $param = $this->_valid([
  176. "id.require" => trans("empty.require"),
  177. "type.default" => "one",
  178. ],"post");
  179. if (!is_array($param)) return error($param);
  180. if ($param['type'] == "batch") {
  181. $state = $this->model->where("id","in",$param['id'])->delete();
  182. } else {
  183. $data = $this->model->where("id",$param['id'])->findOrEmpty();
  184. if ($data->isEmpty()) return errorTrans("empty.data");
  185. // 删除其他相关数据
  186. $state = $data->delete();
  187. }
  188. if (!$state) return errorTrans("error.data");
  189. return successTrans("success.data");
  190. } catch (\Throwable $throwable) {
  191. return error($throwable->getMessage());
  192. }
  193. }
  194. }