mirror of
https://gitee.com/dapppp/ruoyi-plus-vben5.git
synced 2026-03-08 07:31:09 +08:00
feat: support menuVisibleWithForbidden in generate-routes-backend (#7526)
当后端菜单项 `meta.menuVisibleWithForbidden` 为 true 时,将其路由组件替换为 403 页,菜单仍展示该项,访问时展示 403,便于用户知悉功能并申请权限。
This commit is contained in:
@@ -8,13 +8,26 @@ import type {
|
||||
|
||||
import { mapTree } from '@vben-core/shared/utils';
|
||||
|
||||
/**
|
||||
* 判断路由是否在菜单中显示但访问时展示 403(让用户知悉功能并申请权限)
|
||||
*/
|
||||
function menuHasVisibleWithForbidden(route: RouteRecordRaw): boolean {
|
||||
return !!route.meta?.menuVisibleWithForbidden;
|
||||
}
|
||||
|
||||
/**
|
||||
* 动态生成路由 - 后端方式
|
||||
* 对 meta.menuVisibleWithForbidden 为 true 的项直接替换为 403 组件,让用户知悉功能并申请权限。
|
||||
*/
|
||||
async function generateRoutesByBackend(
|
||||
options: GenerateMenuAndRoutesOptions,
|
||||
): Promise<RouteRecordRaw[]> {
|
||||
const { fetchMenuListAsync, layoutMap = {}, pageMap = {} } = options;
|
||||
const {
|
||||
fetchMenuListAsync,
|
||||
layoutMap = {},
|
||||
pageMap = {},
|
||||
forbiddenComponent,
|
||||
} = options;
|
||||
|
||||
try {
|
||||
const menuRoutes = await fetchMenuListAsync?.();
|
||||
@@ -28,7 +41,16 @@ async function generateRoutesByBackend(
|
||||
normalizePageMap[normalizeViewPath(key)] = value;
|
||||
}
|
||||
|
||||
const routes = convertRoutes(menuRoutes, layoutMap, normalizePageMap);
|
||||
let routes = convertRoutes(menuRoutes, layoutMap, normalizePageMap);
|
||||
|
||||
if (forbiddenComponent) {
|
||||
routes = mapTree(routes, (route) => {
|
||||
if (menuHasVisibleWithForbidden(route)) {
|
||||
route.component = forbiddenComponent;
|
||||
}
|
||||
return route;
|
||||
});
|
||||
}
|
||||
|
||||
return routes;
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user