Browse Source
feat: support `menuVisibleWithForbidden` in generate-routes-backend (#7526)
当后端菜单项 `meta.menuVisibleWithForbidden` 为 true 时,将其路由组件替换为 403 页,菜单仍展示该项,访问时展示 403,便于用户知悉功能并申请权限。
pull/7528/head
MistyMoon
1 month ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with
24 additions and
2 deletions
-
packages/utils/src/helpers/generate-routes-backend.ts
|
|
|
@ -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) { |
|
|
|
|