226 lines
5.8 KiB
TypeScript
226 lines
5.8 KiB
TypeScript
/* eslint-disable */
|
|
/* prettier-ignore */
|
|
// Generated by elegant-router
|
|
// Read more: https://github.com/soybeanjs/elegant-router
|
|
|
|
import type { RouteRecordRaw } from 'vue-router';
|
|
import type { ElegantConstRoute } from '@elegant-router/vue';
|
|
import type { RouteKey, RouteMap, RoutePath } from '@elegant-router/types';
|
|
import BaseLayout from '@/layouts/base-layout/index.vue';
|
|
import BlankLayout from '@/layouts/blank-layout/index.vue';
|
|
|
|
/**
|
|
* transform elegant const routes to vue routes
|
|
* @param routes elegant const routes
|
|
*/
|
|
export function transformElegantRoutesToVueRoutes(
|
|
routes: ElegantConstRoute[]
|
|
) {
|
|
return routes.flatMap(route => transformElegantRouteToVueRoute(route));
|
|
}
|
|
|
|
/**
|
|
* transform elegant route to vue route
|
|
* @param route elegant const route
|
|
*/
|
|
function transformElegantRouteToVueRoute(
|
|
route: ElegantConstRoute
|
|
) {
|
|
const LAYOUT_PREFIX = 'layout.';
|
|
const VIEW_PREFIX = 'view.';
|
|
const ROUTE_DEGREE_SPLITTER = '_';
|
|
const FIRST_LEVEL_ROUTE_COMPONENT_SPLIT = '$';
|
|
|
|
function isLayout(component: string) {
|
|
return component.startsWith(LAYOUT_PREFIX);
|
|
}
|
|
|
|
function getLayoutName(component: string) {
|
|
const layout = component.replace(LAYOUT_PREFIX, '');
|
|
if (layout === 'base') {
|
|
return BaseLayout;
|
|
}
|
|
if (layout === 'blank') {
|
|
return BlankLayout;
|
|
}
|
|
throw new Error(`Layout component "${layout}" not found`);
|
|
}
|
|
|
|
function isView(component: string) {
|
|
return component.startsWith(VIEW_PREFIX);
|
|
}
|
|
|
|
function getViewName(component: string) {
|
|
const view = component.replace(VIEW_PREFIX, '');
|
|
const v = findView(view);
|
|
if (!v) {
|
|
throw new Error(`View component "${view}" not found`);
|
|
}
|
|
|
|
return v;
|
|
}
|
|
|
|
function isFirstLevelRoute(item: ElegantConstRoute) {
|
|
return !item.name.includes(ROUTE_DEGREE_SPLITTER);
|
|
}
|
|
|
|
function isSingleLevelRoute(item: ElegantConstRoute) {
|
|
return isFirstLevelRoute(item) && !item.children?.length;
|
|
}
|
|
|
|
function getSingleLevelRouteComponent(component: string) {
|
|
const [layout, view] = component.split(FIRST_LEVEL_ROUTE_COMPONENT_SPLIT);
|
|
|
|
return {
|
|
layout: getLayoutName(layout),
|
|
view: getViewName(view)
|
|
};
|
|
}
|
|
|
|
const vueRoutes: RouteRecordRaw[] = [];
|
|
|
|
// add props: true to route
|
|
if (route.path.includes(':') && !route.props) {
|
|
route.props = true;
|
|
}
|
|
|
|
const { name, path, component, children, ...rest } = route;
|
|
|
|
const vueRoute = { name, path, ...rest } as RouteRecordRaw;
|
|
|
|
try {
|
|
if (component) {
|
|
if (isSingleLevelRoute(route)) {
|
|
const { layout, view } = getSingleLevelRouteComponent(component);
|
|
|
|
const singleLevelRoute: RouteRecordRaw = {
|
|
path,
|
|
component: layout,
|
|
children: [
|
|
{
|
|
name,
|
|
path: '',
|
|
component: view,
|
|
...rest
|
|
} as RouteRecordRaw
|
|
]
|
|
};
|
|
|
|
return [singleLevelRoute];
|
|
}
|
|
|
|
if (isLayout(component)) {
|
|
vueRoute.component = getLayoutName(component);
|
|
}
|
|
|
|
if (isView(component)) {
|
|
vueRoute.component = getViewName(component);
|
|
}
|
|
|
|
}
|
|
} catch (error: any) {
|
|
console.error(`Error transforming route "${route.name}": ${error.toString()}`);
|
|
return [];
|
|
}
|
|
|
|
|
|
// add redirect to child
|
|
if (children?.length && !vueRoute.redirect) {
|
|
vueRoute.redirect = {
|
|
name: children[0].name
|
|
};
|
|
}
|
|
|
|
if (children?.length) {
|
|
const childRoutes = children.flatMap(child => transformElegantRouteToVueRoute(child));
|
|
|
|
if (isFirstLevelRoute(route)) {
|
|
vueRoute.children = childRoutes;
|
|
} else {
|
|
vueRoutes.push(...childRoutes);
|
|
}
|
|
}
|
|
|
|
vueRoutes.unshift(vueRoute);
|
|
|
|
return vueRoutes;
|
|
}
|
|
|
|
/**匹配views里面所有的.vue或.tsx文件 */
|
|
const views = import.meta.glob('./../../views/**/*.{vue,tsx}');
|
|
|
|
/**
|
|
* 查找页面模块
|
|
*
|
|
* 查找 `/views/system/menu/index.vue` 或 `/views/system/menu/index.tsx`
|
|
*
|
|
* 参数值为 `system/menu/index`
|
|
*
|
|
* @param dirName 组件路径
|
|
* @returns 路由懒加载函数
|
|
*/
|
|
function findView(dirName: string) {
|
|
for (const dir in views) {
|
|
let viewDirName = '';
|
|
const component = dir.match(/views\/(.+)\.(vue|tsx)/);
|
|
if (component && component.length === 3) {
|
|
viewDirName = component[1];
|
|
}
|
|
viewDirName = viewDirName.replaceAll('/', '_').replace('_index', '');
|
|
if (viewDirName === dirName) {
|
|
return () => views[dir]();
|
|
}
|
|
}
|
|
return () => import('@/views/_builtin/404/index.vue');
|
|
}
|
|
|
|
/**
|
|
* map of route name and route path
|
|
*/
|
|
const routeMap: RouteMap = {
|
|
'root': '/',
|
|
'not-found': '/:pathMatch(.*)*',
|
|
'exception': '/exception',
|
|
'exception_403': '/exception/403',
|
|
'exception_404': '/exception/404',
|
|
'exception_500': '/exception/500',
|
|
'403': '/403',
|
|
'404': '/404',
|
|
'500': '/500',
|
|
'about': '/about',
|
|
'function': '/function',
|
|
'function_hide-child': '/function/hide-child',
|
|
'function_hide-child_one': '/function/hide-child/one',
|
|
'function_hide-child_three': '/function/hide-child/three',
|
|
'function_hide-child_two': '/function/hide-child/two',
|
|
'function_multi-tab': '/function/multi-tab',
|
|
'function_request': '/function/request',
|
|
'function_super-page': '/function/super-page',
|
|
'function_tab': '/function/tab',
|
|
'function_toggle-auth': '/function/toggle-auth',
|
|
'home': '/home',
|
|
'login': '/login/:module(pwd-login|code-login|register|reset-pwd|bind-wechat)?',
|
|
'user-center': '/user-center'
|
|
};
|
|
|
|
/**
|
|
* get route path by route name
|
|
* @param name route name
|
|
*/
|
|
export function getRoutePath<T extends RouteKey>(name: T) {
|
|
console.log(name);
|
|
return routeMap[name];
|
|
}
|
|
|
|
/**
|
|
* get route name by route path
|
|
* @param path route path
|
|
*/
|
|
export function getRouteName(path: RoutePath) {
|
|
const routeEntries = Object.entries(routeMap) as [RouteKey, RoutePath][];
|
|
|
|
const routeName: RouteKey | null = routeEntries.find(([, routePath]) => routePath === path)?.[0] || null;
|
|
|
|
return routeName;
|
|
}
|