$list, 'total' => count($list) ], '获取角色列表成功'); } /** * 获取角色详情 */ public function detail() { $loginInfo = checkUserLogin(); if (!$loginInfo) { return json_error([], '请先登录'); } if (!checkPermission($loginInfo, 'role', 'detail')) { return json_error([], '没有查看角色详情的权限'); } $id = $this->request->param('id', 0, 'intval'); if (!$id) { return json_error([], '角色ID不能为空'); } $role = UserRoleModel::getRoleById($id, $loginInfo['merchant_id']); if (!$role) { return json_error([], '角色不存在'); } return json_success($role, '获取角色详情成功'); } /** * 创建角色 */ public function create() { $loginInfo = checkUserLogin(); if (!$loginInfo) { return json_error([], '请先登录'); } if (!checkPermission($loginInfo, 'role', 'create')) { return json_error([], '没有创建角色的权限'); } $data = Request::only(['role_name', 'privileges']); if (empty($data['role_name'])) { return json_error([], '角色名称不能为空'); } if (empty($data['privileges']) || !is_array($data['privileges'])) { return json_error([], '权限配置不能为空'); } if (UserRoleModel::checkRoleExists($data['role_name'], $loginInfo['merchant_id'])) { return json_error([], '角色名称已存在'); } $data['merchant_id'] = $loginInfo['merchant_id']; try { $role = UserRoleModel::createRole($data); return json_success(['id' => $role->id], '创建角色成功'); } catch (\Exception $e) { return json_error([], '创建角色失败:' . $e->getMessage()); } } /** * 更新角色 */ public function update() { $loginInfo = checkUserLogin(); if (!$loginInfo) { return json_error([], '请先登录'); } if (!checkPermission($loginInfo, 'role', 'update')) { return json_error([], '没有编辑角色的权限'); } $id = Request::param('id', 0, 'intval'); if (!$id) { return json_error([], '角色ID不能为空'); } $data = Request::only(['role_name', 'privileges']); if (empty($data['role_name'])) { return json_error([], '角色名称不能为空'); } if (empty($data['privileges']) || !is_array($data['privileges'])) { return json_error([], '权限配置不能为空'); } $role = UserRoleModel::getRoleById($id, $loginInfo['merchant_id']); if (!$role) { return json_error([], '角色不存在'); } if (UserRoleModel::checkRoleExists($data['role_name'], $loginInfo['merchant_id'], $id)) { return json_error([], '角色名称已存在'); } try { UserRoleModel::updateRole($id, $loginInfo['merchant_id'], $data); return json_success([], '更新角色成功'); } catch (\Exception $e) { return json_error([], '更新角色失败:' . $e->getMessage()); } } /** * 删除角色 */ public function delete() { $loginInfo = checkUserLogin(); if (!$loginInfo) { return json_error([], '请先登录'); } if (!checkPermission($loginInfo, 'role', 'delete')) { return json_error([], '没有删除角色的权限'); } $id = Request::param('id', 0, 'intval'); if (!$id) { return json_error([], '角色ID不能为空'); } $role = UserRoleModel::getRoleById($id, $loginInfo['merchant_id']); if (!$role) { return json_error([], '角色不存在'); } $superAdminRoleId = Config::get('permission.super_admin_role_id', 1); if ($id == $superAdminRoleId) { return json_error([], '不能删除超级管理员角色'); } $userCount = UserModel::where('user_role', $id) ->where('merchant_id', $loginInfo['merchant_id']) ->count(); if ($userCount > 0) { return json_error([], '该角色下还有用户,不能删除'); } try { UserRoleModel::deleteRole($id, $loginInfo['merchant_id']); return json_success([], '删除角色成功'); } catch (\Exception $e) { return json_error([], '删除角色失败:' . $e->getMessage()); } } }