You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.1 KiB

7 months ago
import type { RouteRecordRaw } from 'vue-router';
import { isArray } from 'lodash-es';
import { appConfig } from '/@/config/app.config.ts';
const modules = import.meta.globEager('/src/router/**/*.ts');
// const jsonModules = import.meta.globEager('/src/data/router/*.json');
const routers: any[] = [];
// Object.keys(modules).forEach((key) => {
// const mod = modules[key].default || {};
// const modList = Array.isArray(mod) ? [...mod] : [mod];
// modList[0].path && routers.push(...modList);
// });
// Object.keys(jsonModules).forEach((key) => {
// const mod = jsonModules[key].default || {};
// const modList = Array.isArray(mod) ? [...mod] : [mod];
// modList.forEach((item, index) => {
// modList[index] = setComponent(item);
// });
// routers.push(...modList);
// });
/**
* name为root的路由root是默认路由
* @param routers
*/
function hasRoot(routers: RouteRecordRaw[]): boolean {
let root = undefined;
function checkRoot(route: RouteRecordRaw | RouteRecordRaw[]) {
if (isArray(route)) {
for (let i = 0, l = route.length; i < l; i++) {
checkRoot(route[i]);
}
} else {
if (route.name === 'root') {
return (root = route);
}
if (route.children) {
for (let i = 0, l = route.children.length; i < l; i++) {
checkRoot(route.children[i]);
}
}
}
}
checkRoot(routers);
return !!root;
}
// if (!hasRoot(routers)) console.error('Route named root is required as the default route');
function setComponent(route: any) {
/* @vite-ignore */
const component = route.component;
if (component) {
route.component = () =>
import(/* @vite-ignore */ `/nerv-lib/paas/view/service/${component}.vue`);
} else {
route.component = () =>
import(/* @vite-ignore */ '/nerv-lib/paas/view/system/layout/content.vue');
}
if (route.children) {
route.children.forEach((item: Object, index: number) => {
route.children[index] = setComponent(item);
});
}
return route;
}
/**
*
*/
export const LoginRoute = {
path: '/login',
name: 'login',
component: appConfig.customLogin
? appConfig.customLogin
: () => import('/nerv-lib/paas/view/system/login.vue'),
meta: {
title: '登录',
},
};
/**
*
*/
export const Error404Route = {
path: '/:pathMatch(.*)*',
name: 'error404',
component: () => import('/nerv-lib/paas/view/service/error-404.vue'),
};
/**
*
*/
export const Error403Route = {
path: '/403',
name: 'error403',
component: () => import('/nerv-lib/paas/view/service/error-403.vue'),
};
/**
* root
*/
export const RootRoute = {
path: '/',
name: 'rootRoute',
redirect: 'root',
};
const outControlPageName: any[] = [];
Object.keys(modules).forEach((key) => {
const mod = modules[key].default || {};
if (mod.name && mod.outContrl) {
outControlPageName.push(mod.name);
}
});
export const WHITE_NAME_LIST = [
Error404Route.name,
Error403Route.name,
LoginRoute.name,
...outControlPageName,
];
export const ALLRoute = [...routers, LoginRoute, Error403Route, Error404Route, RootRoute];