2022-08-02 18:03:12 +08:00
|
|
|
import uniq from 'lodash/uniq';
|
2023-04-05 15:40:41 +08:00
|
|
|
import { createRouter, createWebHistory, RouteRecordRaw, useRoute } from 'vue-router';
|
2021-08-26 11:25:15 +08:00
|
|
|
|
2022-12-28 15:12:28 +08:00
|
|
|
const env = import.meta.env.MODE || 'development';
|
|
|
|
|
2023-01-16 18:54:21 +08:00
|
|
|
// 导入homepage相关固定路由
|
2023-03-07 11:21:04 +08:00
|
|
|
const homepageModules = import.meta.glob('./modules/**/homepage.ts', { eager: true });
|
2022-07-12 14:14:44 +08:00
|
|
|
|
2023-01-16 18:54:21 +08:00
|
|
|
// 导入modules非homepage相关固定路由
|
2023-03-07 11:21:04 +08:00
|
|
|
const fixedModules = import.meta.glob('./modules/**/!(homepage).ts', { eager: true });
|
2022-07-12 14:14:44 +08:00
|
|
|
|
2023-01-16 18:54:21 +08:00
|
|
|
// 其他固定路由
|
2021-12-12 15:46:02 +08:00
|
|
|
const defaultRouterList: Array<RouteRecordRaw> = [
|
|
|
|
{
|
|
|
|
path: '/login',
|
|
|
|
name: 'login',
|
|
|
|
component: () => import('@/pages/login/index.vue'),
|
|
|
|
},
|
2021-08-26 11:25:15 +08:00
|
|
|
{
|
2021-12-12 15:46:02 +08:00
|
|
|
path: '/',
|
2021-12-13 20:12:32 +08:00
|
|
|
redirect: '/dashboard/base',
|
2021-08-26 11:25:15 +08:00
|
|
|
},
|
|
|
|
];
|
2023-01-16 18:54:21 +08:00
|
|
|
// 存放固定路由
|
|
|
|
export const homepageRouterList: Array<RouteRecordRaw> = mapModuleRouterList(homepageModules);
|
|
|
|
export const fixedRouterList: Array<RouteRecordRaw> = mapModuleRouterList(fixedModules);
|
2021-08-26 11:25:15 +08:00
|
|
|
|
2023-01-16 18:54:21 +08:00
|
|
|
export const allRoutes = [...homepageRouterList, ...fixedRouterList, ...defaultRouterList];
|
|
|
|
|
|
|
|
// 固定路由模块转换为路由
|
|
|
|
export function mapModuleRouterList(modules: Record<string, unknown>): Array<RouteRecordRaw> {
|
|
|
|
const routerList: Array<RouteRecordRaw> = [];
|
|
|
|
Object.keys(modules).forEach((key) => {
|
|
|
|
// @ts-ignore
|
|
|
|
const mod = modules[key].default || {};
|
|
|
|
const modList = Array.isArray(mod) ? [...mod] : [mod];
|
|
|
|
routerList.push(...modList);
|
|
|
|
});
|
|
|
|
return routerList;
|
|
|
|
}
|
2021-12-12 15:46:02 +08:00
|
|
|
|
2022-08-02 18:03:12 +08:00
|
|
|
export const getRoutesExpanded = () => {
|
2023-05-24 10:40:06 +08:00
|
|
|
const expandedRoutes: Array<string> = [];
|
2022-08-02 18:03:12 +08:00
|
|
|
|
2023-01-16 18:54:21 +08:00
|
|
|
fixedRouterList.forEach((item) => {
|
2022-08-02 18:03:12 +08:00
|
|
|
if (item.meta && item.meta.expanded) {
|
|
|
|
expandedRoutes.push(item.path);
|
|
|
|
}
|
|
|
|
if (item.children && item.children.length > 0) {
|
|
|
|
item.children
|
|
|
|
.filter((child) => child.meta && child.meta.expanded)
|
|
|
|
.forEach((child: RouteRecordRaw) => {
|
|
|
|
expandedRoutes.push(item.path);
|
|
|
|
expandedRoutes.push(`${item.path}/${child.path}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return uniq(expandedRoutes);
|
|
|
|
};
|
|
|
|
|
2022-06-08 17:05:39 +08:00
|
|
|
export const getActive = (maxLevel = 3): string => {
|
2022-04-28 11:14:13 +08:00
|
|
|
const route = useRoute();
|
|
|
|
if (!route.path) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return route.path
|
|
|
|
.split('/')
|
|
|
|
.filter((_item: string, index: number) => index <= maxLevel && index > 0)
|
|
|
|
.map((item: string) => `/${item}`)
|
|
|
|
.join('');
|
|
|
|
};
|
|
|
|
|
2021-08-26 11:25:15 +08:00
|
|
|
const router = createRouter({
|
2023-01-30 11:09:58 +08:00
|
|
|
history: createWebHistory(env === 'site' ? '/starter/vue-next/' : import.meta.env.VITE_BASE_URL),
|
2022-03-07 00:08:57 +08:00
|
|
|
routes: allRoutes,
|
2021-09-01 11:26:19 +08:00
|
|
|
scrollBehavior() {
|
2021-08-26 11:25:15 +08:00
|
|
|
return {
|
|
|
|
el: '#app',
|
|
|
|
top: 0,
|
|
|
|
behavior: 'smooth',
|
2021-09-01 11:26:19 +08:00
|
|
|
};
|
2021-08-26 11:25:15 +08:00
|
|
|
},
|
2021-09-01 11:26:19 +08:00
|
|
|
});
|
2021-12-12 15:46:02 +08:00
|
|
|
|
2021-09-01 11:26:19 +08:00
|
|
|
export default router;
|