UserRole.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. class UserRole extends BaseController
  10. {
  11. /**
  12. * 获取所有权限配置
  13. */
  14. public function getPermissions()
  15. {
  16. $permissions = Config::get('permission.permissions', []);
  17. return json_success($permissions, '获取权限配置成功');
  18. }
  19. /**
  20. * 获取角色列表
  21. */
  22. public function list()
  23. {
  24. $merchantId = $this->getMerchantId();
  25. $list = UserRoleModel::getRoleList($merchantId);
  26. return json_success([
  27. 'list' => $list,
  28. 'total' => count($list)
  29. ]);
  30. }
  31. /**
  32. * 获取角色详情
  33. */
  34. public function detail()
  35. {
  36. $id = $this->request->param('id', 0, 'intval');
  37. if (!$id) {
  38. return json_error([], '角色ID不能为空');
  39. }
  40. $merchantId = $this->getMerchantId();
  41. $role = UserRoleModel::getRoleById($id, $merchantId);
  42. if (!$role) {
  43. return json_error([], '角色不存在');
  44. }
  45. return json_success($role);
  46. }
  47. /**
  48. * 创建角色
  49. */
  50. public function create()
  51. {
  52. $data = Request::only(['role_name', 'privileges']);
  53. if (empty($data['role_name'])) {
  54. return json_error([], '角色名称不能为空');
  55. }
  56. if (empty($data['privileges']) || !is_array($data['privileges'])) {
  57. return json_error([], '权限配置不能为空');
  58. }
  59. $merchantId = $this->getMerchantId();
  60. if (UserRoleModel::checkRoleExists($data['role_name'], $merchantId)) {
  61. return json_error([], '角色名称已存在');
  62. }
  63. $data['merchant_id'] = $merchantId;
  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. $id = Request::param('id', 0, 'intval');
  77. if (!$id) {
  78. return json_error([], '角色ID不能为空');
  79. }
  80. $data = Request::only(['role_name', 'privileges']);
  81. if (empty($data['role_name'])) {
  82. return json_error([], '角色名称不能为空');
  83. }
  84. if (empty($data['privileges']) || !is_array($data['privileges'])) {
  85. return json_error([], '权限配置不能为空');
  86. }
  87. $merchantId = $this->getMerchantId();
  88. $role = UserRoleModel::getRoleById($id, $merchantId);
  89. if (!$role) {
  90. return json_error([], '角色不存在');
  91. }
  92. if (UserRoleModel::checkRoleExists($data['role_name'], $merchantId, $id)) {
  93. return json_error([], '角色名称已存在');
  94. }
  95. try {
  96. UserRoleModel::updateRole($id, $merchantId, $data);
  97. return json_success([], '更新角色成功');
  98. } catch (\Exception $e) {
  99. return json_error([], '更新角色失败:' . $e->getMessage());
  100. }
  101. }
  102. /**
  103. * 删除角色
  104. */
  105. public function delete()
  106. {
  107. $id = Request::param('id', 0, 'intval');
  108. if (!$id) {
  109. return json_error([], '角色ID不能为空');
  110. }
  111. $merchantId = $this->getMerchantId();
  112. $role = UserRoleModel::getRoleById($id, $merchantId);
  113. if (!$role) {
  114. return json_error([], '角色不存在');
  115. }
  116. $superAdminRoleId = Config::get('permission.super_admin_role_id', 1);
  117. if ($id == $superAdminRoleId) {
  118. return json_error([], '不能删除超级管理员角色');
  119. }
  120. $userCount = UserModel::where('user_role', $id)
  121. ->where('merchant_id', $merchantId)
  122. ->count();
  123. if ($userCount > 0) {
  124. return json_error([], '该角色下还有用户,不能删除');
  125. }
  126. try {
  127. UserRoleModel::deleteRole($id, $merchantId);
  128. return json_success([], '删除角色成功');
  129. } catch (\Exception $e) {
  130. return json_error([], '删除角色失败:' . $e->getMessage());
  131. }
  132. }
  133. /**
  134. * 获取商户ID
  135. */
  136. private function getMerchantId()
  137. {
  138. $token = Request::cookie('auth_token');
  139. if (!$token) {
  140. throw new \Exception('未登录');
  141. }
  142. $payload = $this->verifyToken($token);
  143. if (!$payload) {
  144. throw new \Exception('登录已过期');
  145. }
  146. return $payload['merchant_id'] ?? 0;
  147. }
  148. /**
  149. * 验证Token
  150. */
  151. private function verifyToken($token)
  152. {
  153. try {
  154. $parts = explode('.', $token);
  155. if (count($parts) != 3) {
  156. return false;
  157. }
  158. $payload = json_decode(base64_decode($parts[1]), true);
  159. if ($payload['exp'] < time()) {
  160. return false;
  161. }
  162. return $payload;
  163. } catch (\Exception $e) {
  164. return false;
  165. }
  166. }
  167. }