2022-01-03 22:20:10 +08:00
|
|
|
|
import type { RouteRecordRaw } from 'vue-router';
|
|
|
|
|
import { consoleError } from '../common';
|
2022-01-21 23:59:14 +08:00
|
|
|
|
import { getLayoutComponent, getViewComponent } from './component';
|
2021-11-28 12:17:48 +08:00
|
|
|
|
|
2022-01-21 23:59:14 +08:00
|
|
|
|
type ComponentAction = Record<AuthRoute.RouteComponent, () => void>;
|
2021-11-28 12:17:48 +08:00
|
|
|
|
|
2022-01-06 11:22:14 +08:00
|
|
|
|
/**
|
|
|
|
|
* 将权限路由转换成vue路由
|
|
|
|
|
* @param routes - 权限路由
|
2022-01-21 23:59:14 +08:00
|
|
|
|
* @description 所有多级路由都会被转换成二级路由
|
2022-01-06 11:22:14 +08:00
|
|
|
|
*/
|
|
|
|
|
export function transformAuthRoutesToVueRoutes(routes: AuthRoute.Route[]) {
|
|
|
|
|
return routes.map(route => transformAuthRouteToVueRoute(route)).flat(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将单个权限路由转换成vue路由
|
|
|
|
|
* @param route - 权限路由
|
|
|
|
|
*/
|
|
|
|
|
function transformAuthRouteToVueRoute(item: AuthRoute.Route) {
|
|
|
|
|
const resultRoute: RouteRecordRaw[] = [];
|
|
|
|
|
|
2022-01-06 02:00:42 +08:00
|
|
|
|
const itemRoute = { ...item } as RouteRecordRaw;
|
|
|
|
|
|
2022-01-20 21:24:01 +08:00
|
|
|
|
// 动态path
|
2022-01-06 02:00:42 +08:00
|
|
|
|
if (hasDynamicPath(item)) {
|
|
|
|
|
Object.assign(itemRoute, { path: item.meta.dynamicPath });
|
2022-01-03 22:20:10 +08:00
|
|
|
|
}
|
2022-01-06 02:00:42 +08:00
|
|
|
|
|
2022-01-20 21:24:01 +08:00
|
|
|
|
// 外链路由
|
|
|
|
|
if (hasHref(item)) {
|
|
|
|
|
Object.assign(itemRoute, { component: getViewComponent('not-found-page') });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 路由组件
|
2022-01-03 22:20:10 +08:00
|
|
|
|
if (hasComponent(item)) {
|
|
|
|
|
const action: ComponentAction = {
|
2022-01-08 20:49:21 +08:00
|
|
|
|
basic() {
|
2022-01-21 23:59:14 +08:00
|
|
|
|
itemRoute.component = getLayoutComponent('basic');
|
2022-01-03 22:20:10 +08:00
|
|
|
|
},
|
|
|
|
|
blank() {
|
2022-01-21 23:59:14 +08:00
|
|
|
|
itemRoute.component = getLayoutComponent('blank');
|
2022-01-03 22:20:10 +08:00
|
|
|
|
},
|
2022-01-06 02:00:42 +08:00
|
|
|
|
multi() {
|
|
|
|
|
// 多级路由一定有子路由
|
|
|
|
|
if (hasChildren(item)) {
|
2022-01-06 11:22:14 +08:00
|
|
|
|
Object.assign(itemRoute, { meta: { ...itemRoute.meta, multi: true } });
|
|
|
|
|
delete itemRoute.component;
|
2022-01-06 02:00:42 +08:00
|
|
|
|
} else {
|
|
|
|
|
consoleError('多级路由缺少子路由: ', item);
|
|
|
|
|
}
|
|
|
|
|
},
|
2022-01-03 22:20:10 +08:00
|
|
|
|
self() {
|
|
|
|
|
itemRoute.component = getViewComponent(item.name);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
try {
|
|
|
|
|
action[item.component!]();
|
|
|
|
|
} catch {
|
|
|
|
|
consoleError('路由组件解析失败: ', item);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-05 01:35:32 +08:00
|
|
|
|
|
2022-01-06 02:00:42 +08:00
|
|
|
|
// 注意:单独路由没有children
|
2022-01-03 22:20:10 +08:00
|
|
|
|
if (isSingleRoute(item)) {
|
2022-01-06 02:00:42 +08:00
|
|
|
|
if (hasChildren(item)) {
|
|
|
|
|
consoleError('单独路由不应该有子路由: ', item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 捕获无效路由的需特殊处理
|
|
|
|
|
if (item.name === 'not-found-page') {
|
2022-01-05 11:55:54 +08:00
|
|
|
|
itemRoute.children = [
|
|
|
|
|
{
|
|
|
|
|
path: '',
|
2022-01-05 13:49:42 +08:00
|
|
|
|
name: item.name,
|
2022-01-06 02:00:42 +08:00
|
|
|
|
component: getViewComponent('not-found-page')
|
2022-01-05 11:55:54 +08:00
|
|
|
|
}
|
|
|
|
|
];
|
2022-01-06 02:00:42 +08:00
|
|
|
|
} else {
|
|
|
|
|
const parentPath = `${itemRoute.path}-parent` as AuthRoute.SingleRouteParentPath;
|
2022-01-05 11:55:54 +08:00
|
|
|
|
|
2022-01-21 23:59:14 +08:00
|
|
|
|
const layout = item.meta.singleLayout === 'basic' ? getLayoutComponent('basic') : getLayoutComponent('blank');
|
2022-01-05 11:55:54 +08:00
|
|
|
|
|
2022-01-06 02:00:42 +08:00
|
|
|
|
const parentRoute: RouteRecordRaw = {
|
|
|
|
|
path: parentPath,
|
2022-01-08 20:49:21 +08:00
|
|
|
|
component: layout,
|
2022-01-06 02:00:42 +08:00
|
|
|
|
redirect: item.path,
|
|
|
|
|
children: [itemRoute]
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-06 11:22:14 +08:00
|
|
|
|
return [parentRoute];
|
2022-01-06 02:00:42 +08:00
|
|
|
|
}
|
2022-01-05 11:55:54 +08:00
|
|
|
|
}
|
2022-01-06 02:00:42 +08:00
|
|
|
|
|
2022-01-20 21:24:01 +08:00
|
|
|
|
// 子路由
|
2022-01-05 11:55:54 +08:00
|
|
|
|
if (hasChildren(item)) {
|
2022-01-06 11:22:14 +08:00
|
|
|
|
const children = item.children!.map(child => transformAuthRouteToVueRoute(child)).flat();
|
|
|
|
|
|
|
|
|
|
// 找出第一个不为多级路由中间级的子路由路径作为重定向路径
|
|
|
|
|
const redirectPath: AuthRoute.RoutePath = (children.find(item => !item.meta?.multi)?.path ||
|
|
|
|
|
'/') as AuthRoute.RoutePath;
|
|
|
|
|
if (redirectPath === '/') {
|
|
|
|
|
consoleError('该多级路由没有有效的子路径', item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (item.component === 'multi') {
|
|
|
|
|
// 多级路由,将子路由提取出来变成同级
|
|
|
|
|
resultRoute.push(...children);
|
|
|
|
|
delete itemRoute.children;
|
|
|
|
|
} else {
|
|
|
|
|
itemRoute.children = children;
|
|
|
|
|
}
|
|
|
|
|
itemRoute.redirect = redirectPath;
|
2022-01-03 22:20:10 +08:00
|
|
|
|
}
|
2022-01-20 21:24:01 +08:00
|
|
|
|
|
2022-01-06 11:22:14 +08:00
|
|
|
|
resultRoute.push(itemRoute);
|
2021-11-28 12:17:48 +08:00
|
|
|
|
|
2022-01-06 11:22:14 +08:00
|
|
|
|
return resultRoute;
|
2021-11-28 12:17:48 +08:00
|
|
|
|
}
|
2021-10-21 11:59:55 +08:00
|
|
|
|
|
2022-01-21 23:59:14 +08:00
|
|
|
|
/**
|
|
|
|
|
* 是否有外链
|
|
|
|
|
* @param item - 权限路由
|
|
|
|
|
*/
|
2022-01-20 21:24:01 +08:00
|
|
|
|
function hasHref(item: AuthRoute.Route) {
|
|
|
|
|
return Boolean(item.meta.href);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-21 23:59:14 +08:00
|
|
|
|
/**
|
|
|
|
|
* 是否有动态路由path
|
|
|
|
|
* @param item - 权限路由
|
|
|
|
|
*/
|
2022-01-20 21:24:01 +08:00
|
|
|
|
function hasDynamicPath(item: AuthRoute.Route) {
|
|
|
|
|
return Boolean(item.meta.dynamicPath);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-21 23:59:14 +08:00
|
|
|
|
/**
|
|
|
|
|
* 是否有路由组件
|
|
|
|
|
* @param item - 权限路由
|
|
|
|
|
*/
|
2022-01-03 22:20:10 +08:00
|
|
|
|
function hasComponent(item: AuthRoute.Route) {
|
|
|
|
|
return Boolean(item.component);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-21 23:59:14 +08:00
|
|
|
|
/**
|
|
|
|
|
* 是否有子路由
|
|
|
|
|
* @param item - 权限路由
|
|
|
|
|
*/
|
2022-01-03 22:20:10 +08:00
|
|
|
|
function hasChildren(item: AuthRoute.Route) {
|
|
|
|
|
return Boolean(item.children && item.children.length);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-21 23:59:14 +08:00
|
|
|
|
/**
|
|
|
|
|
* 是否是单层级路由
|
|
|
|
|
* @param item - 权限路由
|
|
|
|
|
*/
|
2022-01-03 22:20:10 +08:00
|
|
|
|
function isSingleRoute(item: AuthRoute.Route) {
|
2022-01-06 02:00:42 +08:00
|
|
|
|
return Boolean(item.meta.singleLayout);
|
2021-11-29 20:34:56 +08:00
|
|
|
|
}
|