UserRole.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. $data = Request::only(['role_name', 'privileges']);
  48. if (empty($data['role_name'])) {
  49. return json_error([], '角色名称不能为空');
  50. }
  51. if (empty($data['privileges']) || !is_array($data['privileges'])) {
  52. return json_error([], '权限配置不能为空');
  53. }
  54. if (UserRoleModel::checkRoleExists($data['role_name'], $userInfo['merchant_id'])) {
  55. return json_error([], '角色名称已存在');
  56. }
  57. $data['merchant_id'] = $userInfo['merchant_id'];
  58. try {
  59. $role = UserRoleModel::createRole($data);
  60. return json_success(['id' => $role->id], '创建角色成功');
  61. } catch (\Exception $e) {
  62. return json_error([], '创建角色失败:' . $e->getMessage());
  63. }
  64. }
  65. /**
  66. * 更新角色
  67. */
  68. public function update()
  69. {
  70. $userInfo = $this->request->userInfo;
  71. $id = Request::param('id', 0, 'intval');
  72. if (!$id) {
  73. return json_error([], '角色ID不能为空');
  74. }
  75. $data = Request::only(['role_name', 'privileges']);
  76. if (empty($data['role_name'])) {
  77. return json_error([], '角色名称不能为空');
  78. }
  79. if (empty($data['privileges']) || !is_array($data['privileges'])) {
  80. return json_error([], '权限配置不能为空');
  81. }
  82. $role = UserRoleModel::getRoleById($id, $userInfo['merchant_id']);
  83. if (!$role) {
  84. return json_error([], '角色不存在');
  85. }
  86. if (UserRoleModel::checkRoleExists($data['role_name'], $userInfo['merchant_id'], $id)) {
  87. return json_error([], '角色名称已存在');
  88. }
  89. try {
  90. UserRoleModel::updateRole($id, $userInfo['merchant_id'], $data);
  91. return json_success([], '更新角色成功');
  92. } catch (\Exception $e) {
  93. return json_error([], '更新角色失败:' . $e->getMessage());
  94. }
  95. }
  96. /**
  97. * 删除角色
  98. */
  99. public function delete()
  100. {
  101. $userInfo = $this->request->userInfo;
  102. $id = Request::param('id', 0, 'intval');
  103. if (!$id) {
  104. return json_error([], '角色ID不能为空');
  105. }
  106. $role = UserRoleModel::getRoleById($id, $userInfo['merchant_id']);
  107. if (!$role) {
  108. return json_error([], '角色不存在');
  109. }
  110. $userCount = UserModel::where('user_role', $id)
  111. ->where('merchant_id', $userInfo['merchant_id'])
  112. ->count();
  113. if ($userCount > 0) {
  114. return json_error([], '该角色下还有用户,不能删除');
  115. }
  116. try {
  117. UserRoleModel::deleteRole($id, $userInfo['merchant_id']);
  118. return json_success([], '删除角色成功');
  119. } catch (\Exception $e) {
  120. return json_error([], '删除角色失败:' . $e->getMessage());
  121. }
  122. }
  123. /**
  124. * 验证输入数据
  125. */
  126. protected function validateInput(array $data, $scene = '')
  127. {
  128. $validate = new UserRoleValidate();
  129. // 执行场景验证
  130. if (!$validate->scene($scene)->check($data)) {
  131. return $validate->getError();
  132. }
  133. return "";
  134. }
  135. }