PrintCloud.php 7.2 KB

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