| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- /**
- * Copyright (c) 2013-Now https://jeesite.com All rights reserved.
- * No deletion without permission, or be held responsible to law.
- * @author Vben、ThinkGem
- */
- import type { AppRouteModule, AppRouteRecordRaw } from '@jeesite/core/router/types';
- import type { Router, RouteRecordNormalized } from 'vue-router';
- import { cloneDeep, omit } from 'lodash-es';
- import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router';
- import { LAYOUT, IFRAME_BLANK, IFRAME_SIMPLE, EXCEPTION_COMPONENT } from '@jeesite/core/router/constant';
- import { warn, env } from '@jeesite/core/utils/log';
- // Dynamic introduction
- function asyncImportRoute(
- routes: AppRouteRecordRaw[] | undefined,
- parent: AppRouteRecordRaw | undefined,
- root: AppRouteRecordRaw | undefined,
- ) {
- if (!routes) return;
- routes.forEach((item) => {
- item.meta = item.meta || {};
- if (!item.meta.icon) {
- item.meta.icon = 'bx:bx-circle';
- }
- if (parent && item.meta.hideMenu) {
- item.meta.currentActiveMenu = parent.path;
- }
- const rawComponent = item.component;
- const component = typeof rawComponent === 'string' ? rawComponent.toUpperCase() : '';
- if (rawComponent && typeof rawComponent !== 'string') {
- item.children && asyncImportRoute(item.children, item, root);
- return;
- }
- if (!component || component === 'LAYOUT') {
- item.component = LAYOUT;
- } else if (component === 'IFRAME') {
- item.component = root?.component ? IFRAME_BLANK : IFRAME_SIMPLE;
- } else {
- item.component = dynamicImport(item.component as string);
- }
- if (!item.component) {
- item.component = EXCEPTION_COMPONENT;
- item.props = item.props || {};
- item.props.status = 404;
- }
- item.children && asyncImportRoute(item.children, item, root);
- });
- }
- let dynamicViewsModules: Record<string, () => Promise<Recordable>>;
- const explicitDynamicViewMap: Record<string, () => Promise<Recordable>> = {
- '/graph/index': () => import('@jeesite/graph/views/graph/index.vue'),
- '/graph/g6-editor': () => import('@jeesite/graph/views/g6Editor/index.vue'),
- '/graph/relationGraphTest': () => import('@jeesite/graph/views/relationGraphTest/index.vue'),
- '/aiBaseStation/index': () => import('@jeesite/trans/views/aiBaseStation/index.vue'),
- };
- function normalizeViewModuleKey(key: string) {
- return key.replace(/\\/g, '/');
- }
- export function dynamicImport(component: string) {
- const explicitImport = explicitDynamicViewMap[component];
- if (explicitImport) {
- return explicitImport;
- }
- if (!dynamicViewsModules) {
- dynamicViewsModules = import.meta.glob('../../../../**/views/**/*.{vue,tsx}');
- }
- const keys = Object.keys(dynamicViewsModules);
- const matchKeys = keys.filter((key) => {
- const viewsPath = '/views',
- l = viewsPath.length,
- index = key.indexOf(viewsPath);
- let k = key.substring(index + l);
- const lastIndex = k.lastIndexOf('.');
- k = k.substring(0, lastIndex);
- return k === component;
- });
- if (matchKeys?.length === 1) {
- const matchKey = matchKeys[0];
- return dynamicViewsModules[matchKey];
- }
- if (matchKeys?.length > 1) {
- const preferCaseDmPortrait =
- component === '/dm/portrait' &&
- matchKeys.find((key) => normalizeViewModuleKey(key).endsWith('/case/views/dm/portrait.vue'));
- if (preferCaseDmPortrait) {
- return dynamicViewsModules[preferCaseDmPortrait];
- }
- const workspaceMatchKeys = matchKeys.filter((key) => !normalizeViewModuleKey(key).includes('/node_modules/'));
- const preferWorkspaceView = workspaceMatchKeys.length === 1 ? workspaceMatchKeys[0] : undefined;
- if (preferWorkspaceView) {
- warn(`Found duplicate view components for route "${component}", prefer workspace file: ${preferWorkspaceView}`);
- return dynamicViewsModules[preferWorkspaceView];
- }
- warn(`Found duplicate view components for route "${component}", fallback to first matched file: ${matchKeys[0]}`);
- return dynamicViewsModules[matchKeys[0]];
- }
- }
- // Turn background objects into routing objects
- export function transformObjToRoute<T = AppRouteModule>(routeList: AppRouteModule[]): T[] {
- routeList.forEach((item) => {
- const rawComponent = item.component;
- const component = typeof rawComponent === 'string' ? rawComponent.toUpperCase() : '';
- if (component === 'BLANK') {
- item.component = item.path;
- item.children = [cloneDeep(item)];
- item.component = undefined;
- } else {
- item.children = [cloneDeep(item)];
- item.component = LAYOUT;
- }
- item.path = '';
- item.name = `${item.name}Parent`;
- item.meta = item.meta || {};
- item.meta.single = true;
- item.meta.affix = false;
- item.children && asyncImportRoute(item.children, item, item);
- });
- return routeList as unknown as T[];
- }
- /**
- * Convert multi-level routing to level 2 routing
- */
- export function flatMultiLevelRoutes(routeModules: AppRouteModule[]) {
- const modules: AppRouteModule[] = cloneDeep(routeModules);
- for (let index = 0; index < modules.length; index++) {
- const routeModule = modules[index];
- if (!isMultipleRoute(routeModule)) {
- continue;
- }
- promoteRouteLevel(routeModule);
- }
- return modules;
- }
- export function createRouteHistory() {
- if (env.VITE_ROUTE_WEB_HISTORY == 'true') {
- return createWebHistory(env.VITE_PUBLIC_PATH);
- } else {
- return createWebHashHistory(env.VITE_PUBLIC_PATH);
- }
- }
- // Routing level upgrade
- function promoteRouteLevel(routeModule: AppRouteModule) {
- // Use vue-router to splice menus
- let router: Router | null = createRouter({
- routes: [routeModule as unknown as RouteRecordNormalized],
- history: createRouteHistory(),
- });
- const routes = router.getRoutes();
- addToChildren(routes, routeModule.children || [], routeModule);
- router = null;
- routeModule.children = routeModule.children?.map((item) => omit(item, 'children'));
- }
- // Add all sub-routes to the secondary route
- function addToChildren(routes: RouteRecordNormalized[], children: AppRouteRecordRaw[], routeModule: AppRouteModule) {
- for (let index = 0; index < children.length; index++) {
- const child = children[index];
- const route = routes.find((item) => item.name === child.name);
- if (!route) {
- continue;
- }
- routeModule.children = routeModule.children || [];
- if (!routeModule.children.find((item) => item.name === route.name)) {
- routeModule.children?.push(route as unknown as AppRouteModule);
- }
- if (child.children?.length) {
- addToChildren(routes, child.children, routeModule);
- }
- }
- }
- // Determine whether the level exceeds 2 levels
- function isMultipleRoute(routeModule: AppRouteModule) {
- if (!routeModule || !Reflect.has(routeModule, 'children') || !routeModule.children?.length) {
- return false;
- }
- const children = routeModule.children;
- let flag = false;
- for (let index = 0; index < children.length; index++) {
- const child = children[index];
- if (child.children?.length) {
- flag = true;
- break;
- }
- }
- return flag;
- }
|