161 lines
4.4 KiB
TypeScript
161 lines
4.4 KiB
TypeScript
import type {
|
|
ComponentRecordType,
|
|
GenerateMenuAndRoutesOptions,
|
|
} from '@vben/types';
|
|
|
|
import { generateAccessible } from '@vben/access';
|
|
import { preferences } from '@vben/preferences';
|
|
|
|
import { getAllMenusApi } from '#/api/core/menu';
|
|
import { BasicLayout, IFrameView } from '#/layouts';
|
|
import { $t } from '#/locales';
|
|
import { useAppContextStore } from '#/store/app-context';
|
|
|
|
import { isLightMenuName } from './light-menu';
|
|
|
|
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 isLightMenuRoute(route: { name?: string }) {
|
|
return isLightMenuName(route.name);
|
|
}
|
|
|
|
function filterAvailableRoutes<
|
|
T extends {
|
|
children?: T[];
|
|
component?: string;
|
|
name?: string;
|
|
path?: string;
|
|
query?: unknown;
|
|
},
|
|
>(
|
|
routes: T[],
|
|
pageMap: ComponentRecordType,
|
|
layoutMap: ComponentRecordType,
|
|
): T[] {
|
|
return routes
|
|
.map((route) => {
|
|
const children = route.children
|
|
? filterAvailableRoutes(route.children, pageMap, layoutMap)
|
|
: undefined;
|
|
const routeAllowed = isLightMenuRoute(route);
|
|
|
|
if (!routeAllowed && !children?.length) {
|
|
return undefined;
|
|
}
|
|
|
|
if (
|
|
!route.component &&
|
|
route.children &&
|
|
!children?.length
|
|
) {
|
|
return undefined;
|
|
}
|
|
|
|
if (!routeAllowed && children?.length) {
|
|
const groupRoute = { ...route, children };
|
|
delete groupRoute.component;
|
|
return groupRoute;
|
|
}
|
|
|
|
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',
|
|
'../views/_core/announcement/index.vue',
|
|
'../views/_core/announcement/list.vue',
|
|
'../views/_core/authentication/login.vue',
|
|
'../views/_core/fallback/**/*.vue',
|
|
'../views/_core/file-preview/index.vue',
|
|
'../views/_core/menu/index.vue',
|
|
'../views/_core/permission/index.vue',
|
|
'../views/_core/role/index.vue',
|
|
'../views/_core/user/index.vue',
|
|
'../views/ai-platform/agent/editor/index.vue',
|
|
'../views/ai-platform/agent/index.vue',
|
|
'../views/ai-platform/knowledge/detail/index.vue',
|
|
'../views/ai-platform/knowledge/index.vue',
|
|
'../views/ai-platform/model/index.vue',
|
|
'../views/ai-platform/workflow/editor/index.vue',
|
|
'../views/ai-platform/workflow/index.vue',
|
|
'../views/ai-platform/workflow-runs/index.vue',
|
|
]);
|
|
|
|
const layoutMap: ComponentRecordType = {
|
|
BasicLayout,
|
|
IFrameView,
|
|
Layout: BasicLayout,
|
|
'Layout.vue': BasicLayout,
|
|
'/Layout.vue': BasicLayout,
|
|
};
|
|
const routePageMap = normalizePageMap(pageMap);
|
|
|
|
return await generateAccessible(preferences.app.accessMode, {
|
|
...options,
|
|
fetchMenuListAsync: async () => {
|
|
const { ElMessage } =
|
|
await import('element-plus/es/components/message/index');
|
|
ElMessage({
|
|
duration: 1500,
|
|
message: `${$t('common.loadingMenu')}...`,
|
|
});
|
|
|
|
const appContextStore = useAppContextStore();
|
|
const menus = appContextStore.appCode
|
|
? await getAllMenusApi(appContextStore.appCode)
|
|
: await getAllMenusApi();
|
|
|
|
return filterAvailableRoutes(menus, routePageMap, layoutMap);
|
|
},
|
|
forbiddenComponent,
|
|
layoutMap,
|
|
pageMap,
|
|
});
|
|
}
|
|
|
|
export { generateAccess };
|