|
@@ -8,13 +8,26 @@ import type {
|
|
|
|
|
|
|
|
import { mapTree } from '@vben-core/shared/utils';
|
|
import { mapTree } from '@vben-core/shared/utils';
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * 判断路由是否在菜单中显示但访问时展示 403(让用户知悉功能并申请权限)
|
|
|
|
|
+ */
|
|
|
|
|
+function menuHasVisibleWithForbidden(route: RouteRecordRaw): boolean {
|
|
|
|
|
+ return !!route.meta?.menuVisibleWithForbidden;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 动态生成路由 - 后端方式
|
|
* 动态生成路由 - 后端方式
|
|
|
|
|
+ * 对 meta.menuVisibleWithForbidden 为 true 的项直接替换为 403 组件,让用户知悉功能并申请权限。
|
|
|
*/
|
|
*/
|
|
|
async function generateRoutesByBackend(
|
|
async function generateRoutesByBackend(
|
|
|
options: GenerateMenuAndRoutesOptions,
|
|
options: GenerateMenuAndRoutesOptions,
|
|
|
): Promise<RouteRecordRaw[]> {
|
|
): Promise<RouteRecordRaw[]> {
|
|
|
- const { fetchMenuListAsync, layoutMap = {}, pageMap = {} } = options;
|
|
|
|
|
|
|
+ const {
|
|
|
|
|
+ fetchMenuListAsync,
|
|
|
|
|
+ layoutMap = {},
|
|
|
|
|
+ pageMap = {},
|
|
|
|
|
+ forbiddenComponent,
|
|
|
|
|
+ } = options;
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
const menuRoutes = await fetchMenuListAsync?.();
|
|
const menuRoutes = await fetchMenuListAsync?.();
|
|
@@ -28,7 +41,16 @@ async function generateRoutesByBackend(
|
|
|
normalizePageMap[normalizeViewPath(key)] = value;
|
|
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;
|
|
return routes;
|
|
|
} catch (error) {
|
|
} catch (error) {
|