Store.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace app\controller\merchant;
  3. use app\extra\basic\Base;
  4. use app\extra\douyin\Account;
  5. use app\extra\douyin\Client;
  6. use app\extra\service\saas\StoreService;
  7. use app\extra\tools\CodeExtend;
  8. use app\middleware\AuthMiddleware;
  9. use app\model\saas\SaasStore;
  10. use app\model\system\SystemUser;
  11. use app\validate\saas\StoreValidate;
  12. use DI\Attribute\Inject;
  13. use LinFly\Annotation\Route\Controller;
  14. use LinFly\Annotation\Route\Route;
  15. use support\Request;
  16. use support\Response;
  17. use Webman\Annotation\Middleware;
  18. #[Controller(prefix: "/api/merchant/store"),Middleware(AuthMiddleware::class)]
  19. class Store extends Base
  20. {
  21. #[Inject]
  22. protected StoreService $service;
  23. #[Inject]
  24. protected SaasStore $model;
  25. #[Inject]
  26. protected StoreValidate $validate;
  27. protected string $scene = "edit";
  28. #[Route(path: "list",methods: "get")]
  29. public function getStoreList(Request $request): Response
  30. {
  31. try {
  32. $param = $request->get();
  33. $param['agent'] = $request->user['agent_id'];
  34. $list = $this->service->getList($param);
  35. return successTrans("success.data",pageFormat($list),200);
  36. } catch (\Throwable $throwable) {
  37. return error($throwable->getMessage());
  38. }
  39. }
  40. /**
  41. * 设置path
  42. * @param Request $request
  43. * @return Response
  44. */
  45. #[Route(path: "auth",methods: "post")]
  46. public function setAuth(Request $request): Response
  47. {
  48. try {
  49. $param = $this->_valid([
  50. "id.require" => trans("empty.require"),
  51. ],"post");
  52. if (!is_array($param)) return error($param);
  53. $store = $this->model->where("id",$param['id'])->findOrEmpty();
  54. if ($store->isEmpty()) return errorTrans("empty.data");
  55. // 设置path
  56. $resp = (new Client)->config($this->getDyConfig())->token()->setMiniPath($store['store_id']);
  57. if ($resp['err_no'] <> 0) return error($resp['err_msg']);
  58. $resp = (new Account)->config($this->getDyConfig())->token()->setWhiteSetting($store['store_id'],1,true);
  59. if ($resp['err_no'] <> 0) return error($resp['err_msg']);
  60. return successTrans("success.data");
  61. } catch (\Throwable $throwable) {
  62. return error($throwable->getMessage());
  63. }
  64. }
  65. /**
  66. * 新增/编辑代理
  67. * @param Request $request
  68. * @return Response
  69. */
  70. #[Route(path: "save",methods: "post")]
  71. public function save(Request $request): Response
  72. {
  73. try {
  74. $param = $request->post();
  75. $param['agent_id'] = $request->user['agent_id'];
  76. if (!isset($param['id'])) {
  77. $this->scene = "add";
  78. $param['user_id'] = CodeExtend::random(16,1,date("md"));
  79. if (!empty($param['username'])) {
  80. $userName = (new SystemUser)->where("username",$param['username'])->findOrEmpty();
  81. if (!$userName->isEmpty()) return errorTrans("error.user-exist");
  82. }
  83. }
  84. if (!$this->validate->check($param)) return error($this->validate->getError());
  85. $state = $this->model->setAutoData($param,"store_id");
  86. if (!$state) return errorTrans("error.data");
  87. $this->sceneUser($param,3,"id");
  88. return successTrans("success.data");
  89. } catch (\Throwable $throwable) {
  90. echo $throwable->getMessage()."\n";
  91. echo $throwable->getFile()."\n";
  92. echo $throwable->getLine()."\n";
  93. return error($throwable->getMessage());
  94. }
  95. }
  96. /**
  97. * 新增/编辑代理
  98. * @param Request $request
  99. * @return Response
  100. */
  101. #[Route(path: "edit",methods: "post")]
  102. public function edit(Request $request): Response
  103. {
  104. try {
  105. $param = $request->post();
  106. if (!$this->validate->scene("edit")->check($request->post())) return error($this->validate->getError());
  107. $state = $this->model->setAutoData($param,"store_id");
  108. if (!$state) return errorTrans("error.data");
  109. return successTrans("success.data");
  110. } catch (\Throwable $throwable) {
  111. echo $throwable->getMessage()."\n";
  112. echo $throwable->getFile()."\n";
  113. echo $throwable->getLine()."\n";
  114. return error($throwable->getMessage());
  115. }
  116. }
  117. /**
  118. * @param Request $request
  119. * @return Response
  120. */
  121. #[Route(path: "batch",methods: "post")]
  122. public function setBatchData(Request $request): Response
  123. {
  124. try {
  125. $param = $this->_valid([
  126. "id.require" => trans("empty.require"),
  127. "value.require" => trans("empty.require"),
  128. "field.require" => trans("empty.require"),
  129. "type.require" => trans("empty.require"),
  130. ],"post");
  131. if (!is_array($param)) return error($param);
  132. if ($param['type'] == "batch") {
  133. $state = $this->model->where("id","in",$param['id'])->save([$param['field'] => $param['value']]);
  134. } else {
  135. $state = $this->model->where("id",$param['id'])->save([$param['field'] => $param['value']]);
  136. }
  137. if (!$state) return errorTrans("error.data");
  138. return successTrans("success.data");
  139. } catch (\Throwable $throwable) {
  140. return error($throwable->getMessage());
  141. }
  142. }
  143. /**
  144. * 删除
  145. * @param Request $request
  146. * @return Response
  147. */
  148. #[Route(path: "del",methods: "post")]
  149. public function delUser(Request $request): Response
  150. {
  151. try {
  152. $param = $this->_valid([
  153. "id.require" => trans("empty.require"),
  154. "type.default" => "one",
  155. ],"post");
  156. if (!is_array($param)) return error($param);
  157. if ($param['type'] == "batch") {
  158. $state = $this->model->where("id","in",$param['id'])->delete();
  159. } else {
  160. $data = $this->model->where("id",$param['id'])->findOrEmpty();
  161. if ($data->isEmpty()) return errorTrans("empty.data");
  162. // 删除其他相关数据
  163. $state = $data->delete();
  164. }
  165. if (!$state) return errorTrans("error.data");
  166. return successTrans("success.data");
  167. } catch (\Throwable $throwable) {
  168. return error($throwable->getMessage());
  169. }
  170. }
  171. }