Store.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. if (!isset($param['id'])) {
  76. $this->scene = "add";
  77. $param['user_id'] = CodeExtend::random(16,1,date("md"));
  78. if (!empty($param['username'])) {
  79. $userName = (new SystemUser)->where("username",$param['username'])->findOrEmpty();
  80. if (!$userName->isEmpty()) return errorTrans("error.user-exist");
  81. }
  82. }
  83. if (!$this->validate->check($param)) return error($this->validate->getError());
  84. $state = $this->model->setAutoData($param);
  85. if (!$state) return errorTrans("error.data");
  86. $this->sceneUser($param,3,"id");
  87. return successTrans("success.data");
  88. } catch (\Throwable $throwable) {
  89. echo $throwable->getMessage()."\n";
  90. echo $throwable->getFile()."\n";
  91. echo $throwable->getLine()."\n";
  92. return error($throwable->getMessage());
  93. }
  94. }
  95. /**
  96. * 新增/编辑代理
  97. * @param Request $request
  98. * @return Response
  99. */
  100. #[Route(path: "edit",methods: "post")]
  101. public function edit(Request $request): Response
  102. {
  103. try {
  104. $param = $request->post();
  105. if (!$this->validate->check($request->post())) return error($this->validate->getError());
  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. * @param Request $request
  118. * @return Response
  119. */
  120. #[Route(path: "batch",methods: "post")]
  121. public function setBatchData(Request $request): Response
  122. {
  123. try {
  124. $param = $this->_valid([
  125. "id.require" => trans("empty.require"),
  126. "value.require" => trans("empty.require"),
  127. "field.require" => trans("empty.require"),
  128. "type.require" => trans("empty.require"),
  129. ],"post");
  130. if (!is_array($param)) return error($param);
  131. if ($param['type'] == "batch") {
  132. $state = $this->model->where("id","in",$param['id'])->save([$param['field'] => $param['value']]);
  133. } else {
  134. $state = $this->model->where("id",$param['id'])->save([$param['field'] => $param['value']]);
  135. }
  136. if (!$state) return errorTrans("error.data");
  137. return successTrans("success.data");
  138. } catch (\Throwable $throwable) {
  139. return error($throwable->getMessage());
  140. }
  141. }
  142. /**
  143. * 删除
  144. * @param Request $request
  145. * @return Response
  146. */
  147. #[Route(path: "del",methods: "post")]
  148. public function delUser(Request $request): Response
  149. {
  150. try {
  151. $param = $this->_valid([
  152. "id.require" => trans("empty.require"),
  153. "type.default" => "one",
  154. ],"post");
  155. if (!is_array($param)) return error($param);
  156. if ($param['type'] == "batch") {
  157. $state = $this->model->where("id","in",$param['id'])->delete();
  158. } else {
  159. $data = $this->model->where("id",$param['id'])->findOrEmpty();
  160. if ($data->isEmpty()) return errorTrans("empty.data");
  161. // 删除其他相关数据
  162. $state = $data->delete();
  163. }
  164. if (!$state) return errorTrans("error.data");
  165. return successTrans("success.data");
  166. } catch (\Throwable $throwable) {
  167. return error($throwable->getMessage());
  168. }
  169. }
  170. }