feat: Dynamically get the menu from the back end
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import type { accessModeType } from '@vben-core/preferences';
|
||||
import type { RouteRecordRaw } from 'vue-router';
|
||||
|
||||
import type { GeneratorMenuAndRoutesOptions } from '../types';
|
||||
|
||||
import { generateMenus } from './generate-menus';
|
||||
import { generateRoutesByBackend } from './generate-routes-backend';
|
||||
import { generateRoutesByFrontend } from './generate-routes-frontend';
|
||||
|
||||
async function generateMenusAndRoutes(
|
||||
mode: accessModeType,
|
||||
options: GeneratorMenuAndRoutesOptions,
|
||||
) {
|
||||
const { router } = options;
|
||||
// 生成路由
|
||||
const accessibleRoutes = await generateRoutes(mode, options);
|
||||
|
||||
// 动态添加到router实例内
|
||||
accessibleRoutes.forEach((route) => router.addRoute(route));
|
||||
|
||||
// 生成菜单
|
||||
const accessibleMenus = await generateMenus1(mode, accessibleRoutes, options);
|
||||
|
||||
return { accessibleMenus, accessibleRoutes };
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate routes
|
||||
* @param mode
|
||||
*/
|
||||
async function generateRoutes(
|
||||
mode: accessModeType,
|
||||
options: GeneratorMenuAndRoutesOptions,
|
||||
) {
|
||||
const { forbiddenComponent, roles, routes } = options;
|
||||
|
||||
switch (mode) {
|
||||
// 允许所有路由访问,不做任何过滤处理
|
||||
case 'allow-all': {
|
||||
return routes;
|
||||
}
|
||||
case 'frontend': {
|
||||
return await generateRoutesByFrontend(
|
||||
routes,
|
||||
roles || [],
|
||||
forbiddenComponent,
|
||||
);
|
||||
}
|
||||
case 'backend': {
|
||||
return await generateRoutesByBackend(options);
|
||||
}
|
||||
default: {
|
||||
return routes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function generateMenus1(
|
||||
mode: accessModeType,
|
||||
routes: RouteRecordRaw[],
|
||||
options: GeneratorMenuAndRoutesOptions,
|
||||
) {
|
||||
const { router } = options;
|
||||
switch (mode) {
|
||||
case 'allow-all':
|
||||
case 'frontend':
|
||||
case 'backend': {
|
||||
return await generateMenus(routes, router);
|
||||
}
|
||||
default: {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export { generateMenusAndRoutes };
|
Reference in New Issue
Block a user