UserRole.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 think\facade\Config;
  8. use think\facade\Request;
  9. use think\facade\Cookie;
  10. class UserRole extends BaseController
  11. {
  12. /**
  13. * 获取角色列表
  14. */
  15. public function list()
  16. {
  17. $userInfo = $this->request->userInfo;
  18. $list = UserRoleModel::getRoleList($userInfo['merchant_id']);
  19. return json_success([
  20. 'list' => $list,
  21. 'total' => count($list)
  22. ], '获取角色列表成功');
  23. }
  24. /**
  25. * 获取角色详情
  26. */
  27. public function detail()
  28. {
  29. $userInfo = $this->request->userInfo;
  30. $id = Request::param('id', 0, 'intval');
  31. if (!$id) {
  32. return json_error([], '角色ID不能为空');
  33. }
  34. $role = UserRoleModel::getRoleById($id, $userInfo['merchant_id']);
  35. if (!$role) {
  36. return json_error([], '角色不存在');
  37. }
  38. return json_success($role, '获取角色详情成功');
  39. }
  40. /**
  41. * 创建角色
  42. */
  43. public function create()
  44. {
  45. $userInfo = $this->request->userInfo;
  46. $data = Request::only(['role_name', 'privileges']);
  47. if (empty($data['role_name'])) {
  48. return json_error([], '角色名称不能为空');
  49. }
  50. if (empty($data['privileges']) || !is_array($data['privileges'])) {
  51. return json_error([], '权限配置不能为空');
  52. }
  53. if (UserRoleModel::checkRoleExists($data['role_name'], $userInfo['merchant_id'])) {
  54. return json_error([], '角色名称已存在');
  55. }
  56. $data['merchant_id'] = $userInfo['merchant_id'];
  57. try {
  58. $role = UserRoleModel::createRole($data);
  59. return json_success(['id' => $role->id], '创建角色成功');
  60. } catch (\Exception $e) {
  61. return json_error([], '创建角色失败:' . $e->getMessage());
  62. }
  63. }
  64. /**
  65. * 更新角色
  66. */
  67. public function update()
  68. {
  69. $userInfo = $this->request->userInfo;
  70. $id = Request::param('id', 0, 'intval');
  71. if (!$id) {
  72. return json_error([], '角色ID不能为空');
  73. }
  74. $data = Request::only(['role_name', 'privileges']);
  75. if (empty($data['role_name'])) {
  76. return json_error([], '角色名称不能为空');
  77. }
  78. if (empty($data['privileges']) || !is_array($data['privileges'])) {
  79. return json_error([], '权限配置不能为空');
  80. }
  81. $role = UserRoleModel::getRoleById($id, $userInfo['merchant_id']);
  82. if (!$role) {
  83. return json_error([], '角色不存在');
  84. }
  85. if (UserRoleModel::checkRoleExists($data['role_name'], $userInfo['merchant_id'], $id)) {
  86. return json_error([], '角色名称已存在');
  87. }
  88. try {
  89. UserRoleModel::updateRole($id, $userInfo['merchant_id'], $data);
  90. return json_success([], '更新角色成功');
  91. } catch (\Exception $e) {
  92. return json_error([], '更新角色失败:' . $e->getMessage());
  93. }
  94. }
  95. /**
  96. * 删除角色
  97. */
  98. public function delete()
  99. {
  100. $userInfo = $this->request->userInfo;
  101. $id = Request::param('id', 0, 'intval');
  102. if (!$id) {
  103. return json_error([], '角色ID不能为空');
  104. }
  105. $role = UserRoleModel::getRoleById($id, $userInfo['merchant_id']);
  106. if (!$role) {
  107. return json_error([], '角色不存在');
  108. }
  109. $superAdminRoleId = Config::get('permission.super_admin_role_id', 1);
  110. if ($id == $superAdminRoleId) {
  111. return json_error([], '不能删除超级管理员角色');
  112. }
  113. $userCount = UserModel::where('user_role', $id)
  114. ->where('merchant_id', $userInfo['merchant_id'])
  115. ->count();
  116. if ($userCount > 0) {
  117. return json_error([], '该角色下还有用户,不能删除');
  118. }
  119. try {
  120. UserRoleModel::deleteRole($id, $userInfo['merchant_id']);
  121. return json_success([], '删除角色成功');
  122. } catch (\Exception $e) {
  123. return json_error([], '删除角色失败:' . $e->getMessage());
  124. }
  125. }
  126. }