User.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace app\controller\merchant;
  3. use app\extra\basic\Base;
  4. use app\extra\tools\CodeExtend;
  5. use app\middleware\AuthMiddleware;
  6. use app\model\saas\SaasStore;
  7. use app\model\system\SystemUser;
  8. use app\service\system\UserService;
  9. use DI\Attribute\Inject;
  10. use LinFly\Annotation\Attributes\Route\Controller;
  11. use LinFly\Annotation\Attributes\Route\GetMapping;
  12. use LinFly\Annotation\Attributes\Route\Middleware;
  13. use LinFly\Annotation\Attributes\Route\PostMapping;
  14. use support\Request;
  15. use support\Response;
  16. #[Controller("/api/merchant/user"),Middleware(AuthMiddleware::class)]
  17. class User extends Base
  18. {
  19. #[Inject]
  20. protected UserService $service;
  21. #[Inject]
  22. protected SystemUser $model;
  23. #[GetMapping('list')]
  24. public function getDataList(Request $request): Response
  25. {
  26. try {
  27. $param = $request->all();
  28. $param['type'] = 3;
  29. $param['store_id'] = $request->user['store_id'];
  30. $data = $this->service->setModel()->getList($param,"account");
  31. return successTrans(100010,pageFormat($data),200);
  32. } catch (\Throwable $th) {
  33. return error($th->getMessage());
  34. }
  35. }
  36. /**
  37. * 账户列表
  38. * @param Request $request
  39. * @return Response
  40. */
  41. #[PostMapping("save")]
  42. public function setUserData(Request $request): Response
  43. {
  44. try {
  45. [$state,$msg] = $this->checkDeposit($request->user['store_id']);
  46. if (!$state) return error($msg);
  47. $param = $request->post();
  48. if(!isset($param['id'])) // 新增
  49. {
  50. $param['salt'] = CodeExtend::random(6,3);
  51. $param['password'] = md5($param['password'].$param['salt']);
  52. $param['create_ip'] = $request->getRealIp();
  53. $param['store_id'] = $request->user['store_id'];
  54. $user = $this->model->where("username",$param['username'])->findOrEmpty();
  55. if (!$user->isEmpty()) return errorTrans(20011);
  56. }
  57. if (isset($param['app_id'])) {
  58. $param['app_id'] = json_encode($param['app_id']);
  59. }
  60. if(isset($param['role_path']) && is_array($param['role_path'])){
  61. $parent = $param['role_path'];
  62. $param['role_path'] = implode(",",$parent);
  63. $param['role_id'] = $parent[count($parent) - 1];
  64. }
  65. $state = $this->model->setAutoData($param);
  66. if (!$state) return errorTrans(100011);
  67. return successTrans(100010);
  68. } catch (\Throwable $throwable) {
  69. return error($throwable->getMessage());
  70. }
  71. }
  72. /**
  73. * 删除用户
  74. * @param Request $request
  75. * @return Response
  76. */
  77. #[PostMapping("del")]
  78. public function delUser(Request $request): Response
  79. {
  80. try {
  81. $param = $this->_valid([
  82. "id.require" => trans("empty.require")
  83. ],$request->method());
  84. if (!is_array($param)) return error($param);
  85. $user = $this->model->where("id",$param["id"])->findOrEmpty();
  86. if ($user->isEmpty()) return errorTrans("error.data");
  87. if ($user['store_id'] <> $request->user['store_id']) return errorTrans("error.data");
  88. $state = $user->delete();
  89. if (!$state) return errorTrans("error.data");
  90. return successTrans("success.data");
  91. } catch (\Throwable $throwable) {
  92. return error($throwable->getMessage());
  93. }
  94. }
  95. }