Files
Yi.Framework/Yi.Ai.Vue3/src/config/permission.ts
2026-01-24 22:08:54 +08:00

121 lines
3.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 权限配置文件
* 用于配置特定页面的访问权限
*/
/**
* 权限配置接口
*/
export interface PermissionConfig {
/** 路由路径 */
path: string;
/** 允许访问的用户名列表 */
allowedUsers: string[];
/** 权限描述 */
description?: string;
}
/**
* 页面权限配置列表
* 在这里配置需要特殊权限控制的页面
*/
export const PAGE_PERMISSIONS: PermissionConfig[] = [
{
path: '/console/channel',
allowedUsers: ['cc', 'Guo'],
description: '号池管理页面 - 仅限cc和Guo用户访问',
},
{
path: '/console/system-statistics',
allowedUsers: ['cc', 'Guo'],
description: '系统统计页面 - 仅限cc和Guo用户访问',
},
{
path: '/console/announcement',
allowedUsers: ['cc', 'Guo'],
description: '公告管理页面 - 仅限cc和Guo用户访问',
},
// 可以在这里继续添加其他需要权限控制的页面
// {
// path: '/console/admin',
// allowedUsers: ['admin', 'superadmin'],
// description: '管理员页面',
// },
];
/**
* 检查用户是否有权限访问指定路径
* @param path 路由路径
* @param userName 用户名
* @returns 是否有权限
*/
export function checkPagePermission(path: string, userName: string | undefined): boolean {
// 如果没有用户名返回false
if (!userName) {
return false;
}
// 查找该路径的权限配置
const permissionConfig = PAGE_PERMISSIONS.find(config => config.path === path);
// 如果没有配置权限说明该页面不需要特殊权限返回true
if (!permissionConfig) {
return true;
}
// 检查用户名是否在允许列表中(不区分大小写)
return permissionConfig.allowedUsers.some(
allowedUser => allowedUser.toLowerCase() === userName.toLowerCase(),
);
}
/**
* 获取用户无权访问的路由列表
* @param userName 用户名
* @returns 无权访问的路由路径数组
*/
export function getRestrictedRoutes(userName: string | undefined): string[] {
if (!userName) {
return PAGE_PERMISSIONS.map(config => config.path);
}
return PAGE_PERMISSIONS.filter(
config => !config.allowedUsers.some(
allowedUser => allowedUser.toLowerCase() === userName.toLowerCase(),
),
).map(config => config.path);
}
/**
* 检查路由是否需要权限控制
* @param path 路由路径
* @returns 是否需要权限控制
*/
export function isRestrictedRoute(path: string): boolean {
return PAGE_PERMISSIONS.some(config => config.path === path);
}
/**
* 过滤菜单路由,移除用户无权访问的菜单项
* @param routes 路由配置数组
* @param userName 用户名
* @returns 过滤后的路由配置数组
*/
export function filterMenuRoutes(routes: any[], userName: string | undefined): any[] {
return routes.filter((route) => {
// 检查当前路由是否有权限
const hasPermission = checkPagePermission(route.path, userName);
if (!hasPermission) {
return false;
}
// 如果有子路由,递归过滤
if (route.children && route.children.length > 0) {
route.children = filterMenuRoutes(route.children, userName);
}
return true;
});
}