Store.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. "val.default" => 2,
  51. "type.default" => 1, // 1 设置path 2 关闭二维码
  52. ],"post");
  53. if (!is_array($param)) return error($param);
  54. $store = $this->model->where("id",$param['id'])->findOrEmpty();
  55. if ($store->isEmpty()) return errorTrans("empty.data");
  56. if ($param['type'] == 1) {
  57. // 设置path
  58. $resp = (new Client)->config($this->getDyConfig())->token()->setMiniPath($store['store_id']);
  59. if ($resp['err_no'] <> 0) return error($resp['err_msg']);
  60. $resp = (new Account)->config($this->getDyConfig())->token()->setWhiteSetting($store['store_id'],1,true);
  61. if ($resp['err_no'] <> 0) return error($resp['err_msg']);
  62. } else {
  63. if ($store['is_hide'] == 1) {
  64. $store->is_hide = 0;
  65. } else {
  66. $store->is_hide = 1;
  67. }
  68. $store->save();
  69. $resp = (new Client)->config($this->getDyConfig())->token()->setBtnText($store['store_id'],$param['val']);
  70. if ($resp['err_no'] <> 0) return error($resp['err_msg']);
  71. }
  72. return successTrans("success.data");
  73. } catch (\Throwable $throwable) {
  74. return error($throwable->getMessage());
  75. }
  76. }
  77. /**
  78. * @param Request $request
  79. * @return Response
  80. */
  81. #[Route(path: "hook",methods: "get")]
  82. public function getHookUri(Request $request): Response
  83. {
  84. try {
  85. $param = $request->get();
  86. return successTrans(100010,['url' => "https://miniapi.jsshuita.com.cn/"]);
  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 (!isset($param['id'])) {
  102. $this->scene = "add";
  103. $param['user_id'] = CodeExtend::random(16,1,date("md"));
  104. if (!empty($param['username'])) {
  105. $userName = (new SystemUser)->where("username",$param['username'])->findOrEmpty();
  106. if (!$userName->isEmpty()) return errorTrans("error.user-exist");
  107. }
  108. }
  109. if (!$this->validate->check($param)) return error($this->validate->getError());
  110. $state = $this->model->setAutoData($param);
  111. if (!$state) return errorTrans("error.data");
  112. // $this->sceneUser($param,3,"id");
  113. return successTrans("success.data");
  114. } catch (\Throwable $throwable) {
  115. echo $throwable->getMessage()."\n";
  116. echo $throwable->getFile()."\n";
  117. echo $throwable->getLine()."\n";
  118. return error($throwable->getMessage());
  119. }
  120. }
  121. /**
  122. * 新增/编辑代理
  123. * @param Request $request
  124. * @return Response
  125. */
  126. #[Route(path: "edit",methods: "post")]
  127. public function edit(Request $request): Response
  128. {
  129. try {
  130. $param = $request->post();
  131. if (!$this->validate->check($request->post())) return error($this->validate->getError());
  132. $state = $this->model->setAutoData($param);
  133. if (!$state) return errorTrans("error.data");
  134. return successTrans("success.data");
  135. } catch (\Throwable $throwable) {
  136. echo $throwable->getMessage()."\n";
  137. echo $throwable->getFile()."\n";
  138. echo $throwable->getLine()."\n";
  139. return error($throwable->getMessage());
  140. }
  141. }
  142. /**
  143. * @param Request $request
  144. * @return Response
  145. */
  146. #[Route(path: "batch",methods: "post")]
  147. public function setBatchData(Request $request): Response
  148. {
  149. try {
  150. $param = $this->_valid([
  151. "id.require" => trans("empty.require"),
  152. "value.require" => trans("empty.require"),
  153. "field.require" => trans("empty.require"),
  154. "type.require" => trans("empty.require"),
  155. ],"post");
  156. if (!is_array($param)) return error($param);
  157. if ($param['type'] == "batch") {
  158. $state = $this->model->where("id","in",$param['id'])->save([$param['field'] => $param['value']]);
  159. } else {
  160. $state = $this->model->where("id",$param['id'])->save([$param['field'] => $param['value']]);
  161. }
  162. if (!$state) return errorTrans("error.data");
  163. return successTrans("success.data");
  164. } catch (\Throwable $throwable) {
  165. return error($throwable->getMessage());
  166. }
  167. }
  168. /**
  169. * 删除
  170. * @param Request $request
  171. * @return Response
  172. */
  173. #[Route(path: "del",methods: "post")]
  174. public function delUser(Request $request): Response
  175. {
  176. try {
  177. $param = $this->_valid([
  178. "id.require" => trans("empty.require"),
  179. "type.default" => "one",
  180. ],"post");
  181. if (!is_array($param)) return error($param);
  182. if ($param['type'] == "batch") {
  183. $state = $this->model->where("id","in",$param['id'])->delete();
  184. } else {
  185. $data = $this->model->where("id",$param['id'])->findOrEmpty();
  186. if ($data->isEmpty()) return errorTrans("empty.data");
  187. // 删除其他相关数据
  188. $state = $data->delete();
  189. }
  190. if (!$state) return errorTrans("error.data");
  191. return successTrans("success.data");
  192. } catch (\Throwable $throwable) {
  193. return error($throwable->getMessage());
  194. }
  195. }
  196. }