feat: 路由动态权限控制、图片广场优化
This commit is contained in:
@@ -12,4 +12,4 @@ export const COLLAPSE_THRESHOLD: number = 600;
|
||||
export const SIDE_BAR_WIDTH: number = 280;
|
||||
|
||||
// 路由白名单地址[本地存在的路由 staticRouter.ts 中]
|
||||
export const ROUTER_WHITE_LIST: string[] = ['/chat', '/chat/not_login', '/products', '/model-library', '/403', '/404'];
|
||||
export const ROUTER_WHITE_LIST: string[] = ['/chat', '/chat/conversation', '/chat/image', '/chat/video', '/model-library', '/403', '/404'];
|
||||
|
||||
110
Yi.Ai.Vue3/src/config/permission.ts
Normal file
110
Yi.Ai.Vue3/src/config/permission.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* 权限配置文件
|
||||
* 用于配置特定页面的访问权限
|
||||
*/
|
||||
|
||||
/**
|
||||
* 权限配置接口
|
||||
*/
|
||||
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/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;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user