PrintCloud.php 7.3 KB

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