chore: format code
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import type { ExRouteRecordRaw, MenuRecordRaw } from '@vben-core/typings';
|
||||
import type { RouteRecordRaw, Router } from 'vue-router';
|
||||
|
||||
import { mapTree } from '@vben-core/toolkit';
|
||||
import type { RouteRecordRaw, Router } from 'vue-router';
|
||||
|
||||
/**
|
||||
* 根据 routes 生成菜单列表
|
||||
|
@@ -1,16 +1,30 @@
|
||||
import { filterTree } from '@vben-core/toolkit';
|
||||
import type { RouteRecordRaw } from 'vue-router';
|
||||
|
||||
import { filterTree, mapTree } from '@vben-core/toolkit';
|
||||
/**
|
||||
* 动态生成路由
|
||||
*/
|
||||
async function generatorRoutes(
|
||||
routes: RouteRecordRaw[],
|
||||
roles: string[],
|
||||
forbiddenPage?: RouteRecordRaw['component'],
|
||||
): Promise<RouteRecordRaw[]> {
|
||||
// 根据角色标识过滤路由表,判断当前用户是否拥有指定权限
|
||||
return filterTree(routes, (route) => {
|
||||
const finalRoutes = filterTree(routes, (route) => {
|
||||
return hasVisible(route) && hasAuthority(route, roles);
|
||||
});
|
||||
|
||||
if (!forbiddenPage) {
|
||||
return finalRoutes;
|
||||
}
|
||||
|
||||
// 如果有禁止访问的页面,将禁止访问的页面替换为403页面
|
||||
return mapTree(finalRoutes, (route) => {
|
||||
if (menuHasVisibleWithForbidden(route)) {
|
||||
route.component = forbiddenPage;
|
||||
}
|
||||
return route;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -24,9 +38,10 @@ function hasAuthority(route: RouteRecordRaw, access: string[]) {
|
||||
if (!authority) {
|
||||
return true;
|
||||
}
|
||||
return access.some((value) => {
|
||||
return authority.includes(value);
|
||||
});
|
||||
return (
|
||||
access.some((value) => authority.includes(value)) ||
|
||||
menuHasVisibleWithForbidden(route)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,4 +52,12 @@ function hasVisible(route?: RouteRecordRaw) {
|
||||
return !route?.meta?.hideInMenu;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断路由是否在菜单中显示,但是访问会被重定向到403
|
||||
* @param route
|
||||
*/
|
||||
function menuHasVisibleWithForbidden(route: RouteRecordRaw) {
|
||||
return !!route.meta?.menuVisibleWithForbidden;
|
||||
}
|
||||
|
||||
export { generatorRoutes, hasAuthority, hasVisible };
|
||||
|
@@ -1,11 +1,11 @@
|
||||
import type { RouteRecordRaw } from 'vue-router';
|
||||
|
||||
import type { RouteModuleType } from './merge-route-modules';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { mergeRouteModules } from './merge-route-modules';
|
||||
|
||||
import type { RouteModuleType } from './merge-route-modules';
|
||||
|
||||
describe('mergeRouteModules', () => {
|
||||
it('should merge route modules correctly', () => {
|
||||
const routeModules: Record<string, RouteModuleType> = {
|
||||
|
Reference in New Issue
Block a user