Shop.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace app\controller\admin;
  3. use app\extra\basic\Base;
  4. use app\extra\service\saas\ShopService;
  5. use app\extra\tools\CodeExtend;
  6. use app\middleware\AuthMiddleware;
  7. use app\model\saas\SaasAgent;
  8. use app\model\saas\SaasStore;
  9. use app\model\saas\SaasStoreShop;
  10. use app\model\system\SystemUser;
  11. use app\validate\saas\ShopValidate;
  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/shop"),Middleware(AuthMiddleware::class)]
  19. class Shop extends Base
  20. {
  21. #[Inject]
  22. protected ShopValidate $validate;
  23. #[Inject]
  24. protected ShopService $service;
  25. #[Inject]
  26. protected SaasAgent $model;
  27. #[Route(path: "list",methods: "get")]
  28. public function getStoreList(Request $request): Response
  29. {
  30. try {
  31. $param = $request->get();
  32. $list = $this->service->getList($param);
  33. return successTrans("success.data",pageFormat($list),200);
  34. } catch (\Throwable $throwable) {
  35. return error($throwable->getMessage());
  36. }
  37. }
  38. /**
  39. * 新增/编辑代理
  40. * @param Request $request
  41. * @return Response
  42. */
  43. #[Route(path: "save",methods: "post")]
  44. public function save(Request $request): Response
  45. {
  46. try {
  47. $param = $request->post();
  48. if (!isset($param['id'])) {
  49. $param['agent_id'] = CodeExtend::random(16,1,date("md"));
  50. if (!empty($param['username'])) {
  51. $userName = (new SystemUser)->where("username",$param['username'])->findOrEmpty();
  52. if (!$userName->isEmpty()) return errorTrans("error.user-exist");
  53. }
  54. }
  55. if (!$this->validate->check($param)) return error($this->validate->getError());
  56. $state = $this->model->setAutoData($param);
  57. if (!$state) return errorTrans("error.data");
  58. $this->sceneUser($param,2,"id");
  59. return successTrans("success.data");
  60. } catch (\Throwable $throwable) {
  61. echo $throwable->getMessage()."\n";
  62. echo $throwable->getFile()."\n";
  63. echo $throwable->getLine()."\n";
  64. return error($throwable->getMessage());
  65. }
  66. }
  67. /**
  68. * 新增/编辑代理
  69. * @param Request $request
  70. * @return Response
  71. */
  72. #[Route(path: "edit",methods: "post")]
  73. public function edit(Request $request): Response
  74. {
  75. try {
  76. $param = $request->post();
  77. if (!$this->validate->check($request->post())) return error($this->validate->getError());
  78. $state = $this->model->setAutoData($param);
  79. if (!$state) return errorTrans("error.data");
  80. return successTrans("success.data");
  81. } catch (\Throwable $throwable) {
  82. echo $throwable->getMessage()."\n";
  83. echo $throwable->getFile()."\n";
  84. echo $throwable->getLine()."\n";
  85. return error($throwable->getMessage());
  86. }
  87. }
  88. /**
  89. * @param Request $request
  90. * @return Response
  91. */
  92. #[Route(path: "batch",methods: "post")]
  93. public function setBatchData(Request $request): Response
  94. {
  95. try {
  96. $param = $this->_valid([
  97. "id.require" => trans("empty.require"),
  98. "value.require" => trans("empty.require"),
  99. "field.require" => trans("empty.require"),
  100. "type.require" => trans("empty.require"),
  101. ],"post");
  102. if (!is_array($param)) return error($param);
  103. if ($param['type'] == "batch") {
  104. $state = $this->model->where("id","in",$param['id'])->save([$param['field'] => $param['value']]);
  105. } else {
  106. $state = $this->model->where("id",$param['id'])->save([$param['field'] => $param['value']]);
  107. }
  108. if (!$state) return errorTrans("error.data");
  109. return successTrans("success.data");
  110. } catch (\Throwable $throwable) {
  111. return error($throwable->getMessage());
  112. }
  113. }
  114. /**
  115. * 删除
  116. * @param Request $request
  117. * @return Response
  118. */
  119. #[Route(path: "del",methods: "post")]
  120. public function delUser(Request $request): Response
  121. {
  122. try {
  123. $param = $this->_valid([
  124. "id.require" => trans("empty.require"),
  125. "type.default" => "one",
  126. ],"post");
  127. if (!is_array($param)) return error($param);
  128. if ($param['type'] == "batch") {
  129. $state = $this->model->where("id","in",$param['id'])->delete();
  130. } else {
  131. $data = $this->model->where("id",$param['id'])->findOrEmpty();
  132. if ($data->isEmpty()) return errorTrans("empty.data");
  133. // 删除其他相关数据
  134. $state = $data->delete();
  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. }