Trim locale bundles and filtered menus
This commit is contained in:
@@ -13,6 +13,67 @@ import { useAppContextStore } from '#/store/app-context';
|
||||
|
||||
const forbiddenComponent = () => import('#/views/_core/fallback/forbidden.vue');
|
||||
|
||||
function normalizeViewPath(path: string) {
|
||||
const normalizedPath = path.replace(/^(\.\/|\.\.\/)+/, '');
|
||||
const viewPath = normalizedPath.startsWith('/')
|
||||
? normalizedPath
|
||||
: `/${normalizedPath}`;
|
||||
return viewPath.replace(/^\/views/, '');
|
||||
}
|
||||
|
||||
function hasPageComponent(
|
||||
component: string | undefined,
|
||||
pageMap: ComponentRecordType,
|
||||
layoutMap: ComponentRecordType,
|
||||
) {
|
||||
if (!component) return true;
|
||||
if (layoutMap[component]) return true;
|
||||
|
||||
const normalizedPath = normalizeViewPath(component);
|
||||
const pageKey = normalizedPath.endsWith('.vue')
|
||||
? normalizedPath
|
||||
: `${normalizedPath}.vue`;
|
||||
|
||||
return Boolean(pageMap[pageKey]);
|
||||
}
|
||||
|
||||
function normalizePageMap(pageMap: ComponentRecordType) {
|
||||
return Object.fromEntries(
|
||||
Object.entries(pageMap).map(([key, value]) => [
|
||||
normalizeViewPath(key),
|
||||
value,
|
||||
]),
|
||||
) as ComponentRecordType;
|
||||
}
|
||||
|
||||
function filterAvailableRoutes<T extends { children?: T[]; component?: string }>(
|
||||
routes: T[],
|
||||
pageMap: ComponentRecordType,
|
||||
layoutMap: ComponentRecordType,
|
||||
): T[] {
|
||||
return routes
|
||||
.map((route) => {
|
||||
const children = route.children
|
||||
? filterAvailableRoutes(route.children, pageMap, layoutMap)
|
||||
: undefined;
|
||||
|
||||
if (!route.component && route.children && !children?.length) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (!hasPageComponent(route.component, pageMap, layoutMap)) {
|
||||
if (!children?.length) return undefined;
|
||||
|
||||
const groupRoute = { ...route, children };
|
||||
delete groupRoute.component;
|
||||
return groupRoute;
|
||||
}
|
||||
|
||||
return children ? { ...route, children } : route;
|
||||
})
|
||||
.filter(Boolean) as T[];
|
||||
}
|
||||
|
||||
async function generateAccess(options: GenerateMenuAndRoutesOptions) {
|
||||
const pageMap: ComponentRecordType = import.meta.glob([
|
||||
'../views/_core/agent-chat/**/*.vue',
|
||||
@@ -54,7 +115,11 @@ async function generateAccess(options: GenerateMenuAndRoutesOptions) {
|
||||
const layoutMap: ComponentRecordType = {
|
||||
BasicLayout,
|
||||
IFrameView,
|
||||
Layout: BasicLayout,
|
||||
'Layout.vue': BasicLayout,
|
||||
'/Layout.vue': BasicLayout,
|
||||
};
|
||||
const routePageMap = normalizePageMap(pageMap);
|
||||
|
||||
return await generateAccessible(preferences.app.accessMode, {
|
||||
...options,
|
||||
@@ -72,11 +137,13 @@ async function generateAccess(options: GenerateMenuAndRoutesOptions) {
|
||||
|
||||
// 如果是子应用模式,传递 appCode 过滤应用专属菜单
|
||||
if (appContextStore.appCode) {
|
||||
return await getAllMenusApi(appContextStore.appCode);
|
||||
const menus = await getAllMenusApi(appContextStore.appCode);
|
||||
return filterAvailableRoutes(menus, routePageMap, layoutMap);
|
||||
}
|
||||
|
||||
// 主应用模式,不传参数
|
||||
return await getAllMenusApi();
|
||||
const menus = await getAllMenusApi();
|
||||
return filterAvailableRoutes(menus, routePageMap, layoutMap);
|
||||
},
|
||||
// 可以指定没有权限跳转403页面
|
||||
forbiddenComponent,
|
||||
|
||||
Reference in New Issue
Block a user