2024-07-28 06:29:05 +00:00
|
|
|
|
import type {
|
|
|
|
|
AccessModeType,
|
|
|
|
|
GenerateMenuAndRoutesOptions,
|
|
|
|
|
RouteRecordRaw,
|
|
|
|
|
} from '@vben/types';
|
2024-06-30 07:03:37 +00:00
|
|
|
|
|
2024-07-13 08:35:47 +00:00
|
|
|
|
import {
|
2024-08-09 10:24:52 +00:00
|
|
|
|
cloneDeep,
|
2024-07-13 08:35:47 +00:00
|
|
|
|
generateMenus,
|
|
|
|
|
generateRoutesByBackend,
|
|
|
|
|
generateRoutesByFrontend,
|
2024-07-28 06:29:05 +00:00
|
|
|
|
mapTree,
|
2024-07-22 16:03:59 +00:00
|
|
|
|
} from '@vben/utils';
|
2024-07-05 15:15:46 +00:00
|
|
|
|
|
2024-07-13 08:35:47 +00:00
|
|
|
|
async function generateAccessible(
|
2024-07-06 05:28:08 +00:00
|
|
|
|
mode: AccessModeType,
|
2024-07-13 08:35:47 +00:00
|
|
|
|
options: GenerateMenuAndRoutesOptions,
|
2024-06-30 07:03:37 +00:00
|
|
|
|
) {
|
|
|
|
|
const { router } = options;
|
2024-07-05 15:15:46 +00:00
|
|
|
|
|
2024-08-09 10:24:52 +00:00
|
|
|
|
options.routes = cloneDeep(options.routes);
|
2024-06-30 07:03:37 +00:00
|
|
|
|
// 生成路由
|
|
|
|
|
const accessibleRoutes = await generateRoutes(mode, options);
|
|
|
|
|
|
|
|
|
|
// 动态添加到router实例内
|
2024-07-06 05:28:08 +00:00
|
|
|
|
accessibleRoutes.forEach((route) => {
|
|
|
|
|
router.addRoute(route);
|
|
|
|
|
});
|
2024-06-30 07:03:37 +00:00
|
|
|
|
|
|
|
|
|
// 生成菜单
|
2024-07-13 08:35:47 +00:00
|
|
|
|
const accessibleMenus = await generateMenus(accessibleRoutes, options.router);
|
2024-06-30 07:03:37 +00:00
|
|
|
|
|
|
|
|
|
return { accessibleMenus, accessibleRoutes };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate routes
|
|
|
|
|
* @param mode
|
2024-08-22 13:52:44 +00:00
|
|
|
|
* @param options
|
2024-06-30 07:03:37 +00:00
|
|
|
|
*/
|
|
|
|
|
async function generateRoutes(
|
2024-07-06 05:28:08 +00:00
|
|
|
|
mode: AccessModeType,
|
2024-07-13 08:35:47 +00:00
|
|
|
|
options: GenerateMenuAndRoutesOptions,
|
2024-06-30 07:03:37 +00:00
|
|
|
|
) {
|
|
|
|
|
const { forbiddenComponent, roles, routes } = options;
|
|
|
|
|
|
2024-07-28 06:29:05 +00:00
|
|
|
|
let resultRoutes: RouteRecordRaw[] = routes;
|
2024-06-30 07:03:37 +00:00
|
|
|
|
switch (mode) {
|
|
|
|
|
case 'frontend': {
|
2024-07-28 06:29:05 +00:00
|
|
|
|
resultRoutes = await generateRoutesByFrontend(
|
2024-06-30 07:03:37 +00:00
|
|
|
|
routes,
|
|
|
|
|
roles || [],
|
|
|
|
|
forbiddenComponent,
|
|
|
|
|
);
|
2024-07-28 06:29:05 +00:00
|
|
|
|
break;
|
2024-06-30 07:03:37 +00:00
|
|
|
|
}
|
|
|
|
|
case 'backend': {
|
2024-07-28 06:29:05 +00:00
|
|
|
|
resultRoutes = await generateRoutesByBackend(options);
|
|
|
|
|
break;
|
2024-06-30 07:03:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-28 06:29:05 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 调整路由树,做以下处理:
|
|
|
|
|
* 1. 对未添加redirect的路由添加redirect
|
|
|
|
|
*/
|
|
|
|
|
resultRoutes = mapTree(resultRoutes, (route) => {
|
|
|
|
|
// 如果有redirect或者没有子路由,则直接返回
|
|
|
|
|
if (route.redirect || !route.children || route.children.length === 0) {
|
|
|
|
|
return route;
|
|
|
|
|
}
|
|
|
|
|
const firstChild = route.children[0];
|
|
|
|
|
|
|
|
|
|
// 如果子路由不是以/开头,则直接返回,这种情况需要计算全部父级的path才能得出正确的path,这里不做处理
|
2024-08-05 13:12:22 +00:00
|
|
|
|
if (!firstChild?.path || !firstChild.path.startsWith('/')) {
|
2024-07-28 06:29:05 +00:00
|
|
|
|
return route;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
route.redirect = firstChild.path;
|
|
|
|
|
return route;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return resultRoutes;
|
2024-06-30 07:03:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-13 08:35:47 +00:00
|
|
|
|
export { generateAccessible };
|