getMerchantId(); $list = UserRoleModel::getRoleList($merchantId); return json_success([ 'list' => $list, 'total' => count($list) ]); } /** * 获取角色详情 */ public function detail() { $id = $this->request->param('id', 0, 'intval'); if (!$id) { return json_error([], '角色ID不能为空'); } $merchantId = $this->getMerchantId(); $role = UserRoleModel::getRoleById($id, $merchantId); if (!$role) { return json_error([], '角色不存在'); } return json_success($role); } /** * 创建角色 */ public function create() { $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([], '权限配置不能为空'); } $merchantId = $this->getMerchantId(); if (UserRoleModel::checkRoleExists($data['role_name'], $merchantId)) { return json_error([], '角色名称已存在'); } $data['merchant_id'] = $merchantId; try { $role = UserRoleModel::createRole($data); return json_success(['id' => $role->id], '创建角色成功'); } catch (\Exception $e) { return json_error([], '创建角色失败:' . $e->getMessage()); } } /** * 更新角色 */ public function update() { $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([], '权限配置不能为空'); } $merchantId = $this->getMerchantId(); $role = UserRoleModel::getRoleById($id, $merchantId); if (!$role) { return json_error([], '角色不存在'); } if (UserRoleModel::checkRoleExists($data['role_name'], $merchantId, $id)) { return json_error([], '角色名称已存在'); } try { UserRoleModel::updateRole($id, $merchantId, $data); return json_success([], '更新角色成功'); } catch (\Exception $e) { return json_error([], '更新角色失败:' . $e->getMessage()); } } /** * 删除角色 */ public function delete() { $id = Request::param('id', 0, 'intval'); if (!$id) { return json_error([], '角色ID不能为空'); } $merchantId = $this->getMerchantId(); $role = UserRoleModel::getRoleById($id, $merchantId); 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', $merchantId) ->count(); if ($userCount > 0) { return json_error([], '该角色下还有用户,不能删除'); } try { UserRoleModel::deleteRole($id, $merchantId); return json_success([], '删除角色成功'); } catch (\Exception $e) { return json_error([], '删除角色失败:' . $e->getMessage()); } } /** * 获取商户ID */ private function getMerchantId() { $token = Request::cookie('auth_token'); if (!$token) { throw new \Exception('未登录'); } $payload = $this->verifyToken($token); if (!$payload) { throw new \Exception('登录已过期'); } return $payload['merchant_id'] ?? 0; } /** * 验证Token */ private function verifyToken($token) { try { $parts = explode('.', $token); if (count($parts) != 3) { return false; } $payload = json_decode(base64_decode($parts[1]), true); if ($payload['exp'] < time()) { return false; } return $payload; } catch (\Exception $e) { return false; } } }