Store.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace app\controller\admin;
  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/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. $list = $this->service->getList($param);
  34. return successTrans("success.data",pageFormat($list),200);
  35. } catch (\Throwable $throwable) {
  36. return error($throwable->getMessage());
  37. }
  38. }
  39. /**
  40. * 设置path
  41. * @param Request $request
  42. * @return Response
  43. */
  44. #[Route(path: "auth",methods: "post")]
  45. public function setAuth(Request $request): Response
  46. {
  47. try {
  48. $param = $this->_valid([
  49. "id.require" => trans("empty.require"),
  50. ],"post");
  51. if (!is_array($param)) return error($param);
  52. $store = $this->model->where("id",$param['id'])->findOrEmpty();
  53. if ($store->isEmpty()) return errorTrans("empty.data");
  54. // 设置path
  55. $resp = (new Client)->config($this->getDyConfig())->token()->setMiniPath($store['store_id']);
  56. if ($resp['err_no'] <> 0) return error($resp['err_msg']);
  57. $resp = (new Account)->config($this->getDyConfig())->token()->setWhiteSetting($store['store_id'],1,true);
  58. if ($resp['err_no'] <> 0) return error($resp['err_msg']);
  59. return successTrans("success.data");
  60. } catch (\Throwable $throwable) {
  61. return error($throwable->getMessage());
  62. }
  63. }
  64. /**
  65. * @param Request $request
  66. * @return Response
  67. */
  68. #[Route(path: "hook",methods: "get")]
  69. public function getHookUri(Request $request): Response
  70. {
  71. try {
  72. $param = $request->get();
  73. return successTrans(100010,['url' => "https://miniapi.jsshuita.com.cn/"]);
  74. } catch (\Throwable $throwable) {
  75. return error($throwable->getMessage());
  76. }
  77. }
  78. /**
  79. * 新增/编辑代理
  80. * @param Request $request
  81. * @return Response
  82. */
  83. #[Route(path: "save",methods: "post")]
  84. public function save(Request $request): Response
  85. {
  86. try {
  87. $param = $request->post();
  88. if (!isset($param['id'])) {
  89. $this->scene = "add";
  90. $param['user_id'] = CodeExtend::random(16,1,date("md"));
  91. if (!empty($param['username'])) {
  92. $userName = (new SystemUser)->where("username",$param['username'])->findOrEmpty();
  93. if (!$userName->isEmpty()) return errorTrans("error.user-exist");
  94. }
  95. }
  96. if (!$this->validate->check($param)) return error($this->validate->getError());
  97. $state = $this->model->setAutoData($param);
  98. if (!$state) return errorTrans("error.data");
  99. $this->sceneUser($param,3,"id");
  100. return successTrans("success.data");
  101. } catch (\Throwable $throwable) {
  102. echo $throwable->getMessage()."\n";
  103. echo $throwable->getFile()."\n";
  104. echo $throwable->getLine()."\n";
  105. return error($throwable->getMessage());
  106. }
  107. }
  108. /**
  109. * 新增/编辑代理
  110. * @param Request $request
  111. * @return Response
  112. */
  113. #[Route(path: "edit",methods: "post")]
  114. public function edit(Request $request): Response
  115. {
  116. try {
  117. $param = $request->post();
  118. if (!$this->validate->check($request->post())) return error($this->validate->getError());
  119. $state = $this->model->setAutoData($param);
  120. if (!$state) return errorTrans("error.data");
  121. return successTrans("success.data");
  122. } catch (\Throwable $throwable) {
  123. echo $throwable->getMessage()."\n";
  124. echo $throwable->getFile()."\n";
  125. echo $throwable->getLine()."\n";
  126. return error($throwable->getMessage());
  127. }
  128. }
  129. /**
  130. * @param Request $request
  131. * @return Response
  132. */
  133. #[Route(path: "batch",methods: "post")]
  134. public function setBatchData(Request $request): Response
  135. {
  136. try {
  137. $param = $this->_valid([
  138. "id.require" => trans("empty.require"),
  139. "value.require" => trans("empty.require"),
  140. "field.require" => trans("empty.require"),
  141. "type.require" => trans("empty.require"),
  142. ],"post");
  143. if (!is_array($param)) return error($param);
  144. if ($param['type'] == "batch") {
  145. $state = $this->model->where("id","in",$param['id'])->save([$param['field'] => $param['value']]);
  146. } else {
  147. $state = $this->model->where("id",$param['id'])->save([$param['field'] => $param['value']]);
  148. }
  149. if (!$state) return errorTrans("error.data");
  150. return successTrans("success.data");
  151. } catch (\Throwable $throwable) {
  152. return error($throwable->getMessage());
  153. }
  154. }
  155. /**
  156. * 删除
  157. * @param Request $request
  158. * @return Response
  159. */
  160. #[Route(path: "del",methods: "post")]
  161. public function delUser(Request $request): Response
  162. {
  163. try {
  164. $param = $this->_valid([
  165. "id.require" => trans("empty.require"),
  166. "type.default" => "one",
  167. ],"post");
  168. if (!is_array($param)) return error($param);
  169. if ($param['type'] == "batch") {
  170. $state = $this->model->where("id","in",$param['id'])->delete();
  171. } else {
  172. $data = $this->model->where("id",$param['id'])->findOrEmpty();
  173. if ($data->isEmpty()) return errorTrans("empty.data");
  174. // 删除其他相关数据
  175. $state = $data->delete();
  176. }
  177. if (!$state) return errorTrans("error.data");
  178. return successTrans("success.data");
  179. } catch (\Throwable $throwable) {
  180. return error($throwable->getMessage());
  181. }
  182. }
  183. }