UserRole.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\controller;
  4. use app\BaseController;
  5. use app\model\UserRoleModel;
  6. use app\model\UserModel;
  7. use app\validate\UserRoleValidate;
  8. use think\facade\Config;
  9. use think\facade\Request;
  10. use think\facade\Cookie;
  11. class UserRole extends BaseController
  12. {
  13. /**
  14. * 获取角色列表
  15. */
  16. public function list()
  17. {
  18. $userInfo = $this->request->userInfo;
  19. $list = UserRoleModel::getRoleList($userInfo['merchant_id']);
  20. return json_success([
  21. 'list' => $list,
  22. 'total' => count($list)
  23. ], '获取角色列表成功');
  24. }
  25. /**
  26. * 获取角色详情
  27. */
  28. public function detail()
  29. {
  30. $userInfo = $this->request->userInfo;
  31. $id = Request::param('id', 0, 'intval');
  32. if (!$id) {
  33. return json_error([], '角色ID不能为空');
  34. }
  35. $role = UserRoleModel::getRoleById($id, $userInfo['merchant_id']);
  36. if (!$role) {
  37. return json_error([], '角色不存在');
  38. }
  39. return json_success($role, '获取角色详情成功');
  40. }
  41. /**
  42. * 创建角色
  43. */
  44. public function create()
  45. {
  46. $userInfo = $this->request->userInfo;
  47. // 获取请求数据
  48. $data = Request::only(['role_name', 'privileges']);
  49. $data['merchant_id'] = $userInfo['merchant_id'];
  50. // 使用验证器进行字段验证
  51. try {
  52. $validate = validate(UserRoleValidate::class);
  53. if (!$validate->scene('create')->check($data)) {
  54. return json_error([], $validate->getError());
  55. }
  56. } catch (\think\exception\ValidateException $e) {
  57. return json_error([], $e->getMessage());
  58. }
  59. // 额外的业务逻辑验证
  60. // 检查角色名称是否已存在
  61. if (UserRoleModel::checkRoleExists($data['role_name'], $userInfo['merchant_id'])) {
  62. return json_error([], '角色名称已存在');
  63. }
  64. try {
  65. $role = UserRoleModel::createRole($data);
  66. return json_success(['id' => $role->id], '创建角色成功');
  67. } catch (\Exception $e) {
  68. return json_error([], '创建角色失败:' . $e->getMessage());
  69. }
  70. }
  71. /**
  72. * 更新角色
  73. */
  74. public function update()
  75. {
  76. $userInfo = $this->request->userInfo;
  77. $id = Request::post('id', 0, 'intval');
  78. if (!$id) {
  79. return json_error([], '角色ID不能为空');
  80. }
  81. $role = UserRoleModel::getRoleById($id, $userInfo['merchant_id']);
  82. if (!$role) {
  83. return json_error([], '角色不存在');
  84. }
  85. // 获取请求中提供的所有可更新字段
  86. $requestData = Request::post();
  87. $allowedFields = ['role_name', 'privileges'];
  88. $updateData = [];
  89. // 只处理请求中存在且允许更新的字段
  90. foreach ($allowedFields as $field) {
  91. if (array_key_exists($field, $requestData)) {
  92. $updateData[$field] = $requestData[$field];
  93. }
  94. }
  95. if (empty($updateData)) {
  96. return json_error([], '没有要更新的数据');
  97. }
  98. // 使用验证器进行字段验证
  99. $validate = new UserRoleValidate();
  100. // 只验证传入的字段
  101. if (!$validate->only(array_keys($updateData))->check($updateData)) {
  102. return json_error([], $validate->getError());
  103. }
  104. // 额外的业务逻辑验证
  105. // 检查角色名称是否重复
  106. if (isset($updateData['role_name'])) {
  107. if (UserRoleModel::checkRoleExists($updateData['role_name'], $userInfo['merchant_id'], $id)) {
  108. return json_error([], '角色名称已存在');
  109. }
  110. }
  111. try {
  112. UserRoleModel::updateRole($id, $userInfo['merchant_id'], $updateData);
  113. return json_success([], '更新角色成功');
  114. } catch (\Exception $e) {
  115. return json_error([], '更新角色失败:' . $e->getMessage());
  116. }
  117. }
  118. /**
  119. * 删除角色
  120. */
  121. public function delete()
  122. {
  123. $userInfo = $this->request->userInfo;
  124. $id = Request::param('id', 0, 'intval');
  125. if (!$id) {
  126. return json_error([], '角色ID不能为空');
  127. }
  128. $role = UserRoleModel::getRoleById($id, $userInfo['merchant_id']);
  129. if (!$role) {
  130. return json_error([], '角色不存在');
  131. }
  132. $userCount = UserModel::where('user_role', $id)
  133. ->where('merchant_id', $userInfo['merchant_id'])
  134. ->count();
  135. if ($userCount > 0) {
  136. return json_error([], '该角色下还有用户,不能删除');
  137. }
  138. try {
  139. UserRoleModel::deleteRole($id, $userInfo['merchant_id']);
  140. return json_success([], '删除角色成功');
  141. } catch (\Exception $e) {
  142. return json_error([], '删除角色失败:' . $e->getMessage());
  143. }
  144. }
  145. /**
  146. * 验证输入数据
  147. */
  148. protected function validateInput(array $data, $scene = '')
  149. {
  150. $validate = new UserRoleValidate();
  151. // 执行场景验证
  152. if (!$validate->scene($scene)->check($data)) {
  153. return $validate->getError();
  154. }
  155. return "";
  156. }
  157. }