2022-01-03 22:20:10 +08:00
|
|
|
import type { RouteRecordRaw } from 'vue-router';
|
|
|
|
import { Layout } from '@/layouts';
|
|
|
|
import { consoleError } from '../common';
|
|
|
|
import { getViewComponent } from './component';
|
2021-11-28 12:17:48 +08:00
|
|
|
|
2022-01-03 22:20:10 +08:00
|
|
|
type ComponentAction = {
|
|
|
|
[key in AuthRoute.RouteComponent]: () => void;
|
|
|
|
};
|
2021-11-28 12:17:48 +08:00
|
|
|
|
2022-01-03 22:20:10 +08:00
|
|
|
/** 将权限路由类型转换成vue路由类型 */
|
|
|
|
export function transformAuthRouteToVueRoute(item: AuthRoute.Route) {
|
|
|
|
const { name, path } = item;
|
|
|
|
const itemRoute: Partial<RouteRecordRaw> = {
|
|
|
|
name,
|
|
|
|
path,
|
|
|
|
meta: item.meta
|
2021-11-28 12:17:48 +08:00
|
|
|
};
|
2022-01-03 22:20:10 +08:00
|
|
|
if (hasRedirect(item)) {
|
|
|
|
itemRoute.redirect = item.redirect;
|
|
|
|
}
|
|
|
|
if (hasComponent(item)) {
|
|
|
|
const action: ComponentAction = {
|
|
|
|
layout() {
|
|
|
|
itemRoute.component = Layout;
|
|
|
|
},
|
|
|
|
blank() {
|
|
|
|
itemRoute.component = Layout;
|
|
|
|
if (itemRoute.meta) {
|
|
|
|
itemRoute.meta.blankLayout = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
multi() {},
|
|
|
|
self() {
|
|
|
|
itemRoute.component = getViewComponent(item.name);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
action[item.component!]();
|
|
|
|
} catch {
|
|
|
|
consoleError('路由组件解析失败: ', item);
|
|
|
|
}
|
|
|
|
}
|
2022-01-05 01:35:32 +08:00
|
|
|
|
|
|
|
if (hasProps(item) && !isSingleRoute(item)) {
|
|
|
|
(itemRoute as any).props = item.props;
|
|
|
|
}
|
|
|
|
|
2022-01-03 22:20:10 +08:00
|
|
|
if (isSingleRoute(item)) {
|
|
|
|
itemRoute.children = [
|
|
|
|
{
|
|
|
|
path: '',
|
2022-01-04 00:00:48 +08:00
|
|
|
name: item.name,
|
2022-01-05 01:35:32 +08:00
|
|
|
component: getViewComponent(item.name),
|
|
|
|
props: hasProps(item) ? item.props : undefined
|
2022-01-03 22:20:10 +08:00
|
|
|
}
|
|
|
|
];
|
|
|
|
} else if (hasChildren(item)) {
|
|
|
|
itemRoute.children = item.children!.map(child => transformAuthRouteToVueRoute(child));
|
|
|
|
}
|
2021-11-28 12:17:48 +08:00
|
|
|
|
2022-01-03 22:20:10 +08:00
|
|
|
return itemRoute as RouteRecordRaw;
|
2021-11-28 12:17:48 +08:00
|
|
|
}
|
2021-10-21 11:59:55 +08:00
|
|
|
|
2022-01-03 22:20:10 +08:00
|
|
|
function hasComponent(item: AuthRoute.Route) {
|
|
|
|
return Boolean(item.component);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hasRedirect(item: AuthRoute.Route) {
|
|
|
|
return Boolean(item.redirect);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hasChildren(item: AuthRoute.Route) {
|
|
|
|
return Boolean(item.children && item.children.length);
|
|
|
|
}
|
|
|
|
|
2022-01-05 01:35:32 +08:00
|
|
|
function hasProps(item: AuthRoute.Route) {
|
|
|
|
return Boolean(item.props);
|
|
|
|
}
|
|
|
|
|
2022-01-03 22:20:10 +08:00
|
|
|
function isSingleRoute(item: AuthRoute.Route) {
|
|
|
|
return Boolean(item.meta.single);
|
2021-11-21 18:44:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-03 22:20:10 +08:00
|
|
|
* 根据路由key获取AuthRoute数据
|
|
|
|
* @param key - 路由key
|
2021-11-21 18:44:18 +08:00
|
|
|
* @param routes - 路由
|
|
|
|
*/
|
2022-01-03 22:20:10 +08:00
|
|
|
export function findAuthRouteByKey(key: AuthRoute.RouteKey, routes: AuthRoute.Route[]) {
|
|
|
|
const paths = getRouteKeyPathsByKey(key);
|
|
|
|
const route = recursiveFindRouteByPaths(paths, routes);
|
|
|
|
|
|
|
|
return route;
|
2021-10-21 11:59:55 +08:00
|
|
|
}
|
|
|
|
|
2021-11-28 12:17:48 +08:00
|
|
|
/**
|
2022-01-03 22:20:10 +08:00
|
|
|
* 根据路由key的paths获递归取路由
|
|
|
|
* @param paths - 路由key的路径
|
2021-11-28 12:17:48 +08:00
|
|
|
* @param routes - 路由
|
|
|
|
*/
|
2022-01-03 22:20:10 +08:00
|
|
|
function recursiveFindRouteByPaths(
|
|
|
|
paths: AuthRoute.RouteKey[],
|
|
|
|
routes: AuthRoute.Route[]
|
|
|
|
): AuthRoute.Route | undefined {
|
|
|
|
const item = routes.find(route => paths.length && route.name === paths[0]);
|
2021-11-28 12:17:48 +08:00
|
|
|
|
2022-01-03 22:20:10 +08:00
|
|
|
if (item && hasComponent(item)) {
|
|
|
|
return recursiveFindRouteByPaths(paths.slice(1), item.children!);
|
2021-11-28 12:17:48 +08:00
|
|
|
}
|
2022-01-03 22:20:10 +08:00
|
|
|
return item;
|
2021-11-28 12:17:48 +08:00
|
|
|
}
|
|
|
|
|
2022-01-03 22:20:10 +08:00
|
|
|
/**
|
|
|
|
* 根据路由key获取从第一级路由到当前路由key的paths
|
|
|
|
* @param key - 路由key
|
|
|
|
*/
|
|
|
|
function getRouteKeyPathsByKey(key: AuthRoute.RouteKey) {
|
|
|
|
const splitMark: AuthRoute.RouteSplitMark = '_';
|
|
|
|
const keys = key.split(splitMark);
|
|
|
|
const keyPaths: AuthRoute.RouteKey[] = [];
|
|
|
|
|
|
|
|
keys.forEach((itemKey, index) => {
|
|
|
|
if (index === 0) {
|
|
|
|
keyPaths.push(itemKey as AuthRoute.RouteKey);
|
|
|
|
} else {
|
|
|
|
const concatKey = keyPaths[index - 1] + splitMark + itemKey;
|
|
|
|
keyPaths.push(concatKey as AuthRoute.RouteKey);
|
|
|
|
}
|
|
|
|
});
|
2021-11-29 20:34:56 +08:00
|
|
|
|
2022-01-03 22:20:10 +08:00
|
|
|
return keyPaths;
|
2021-11-29 20:34:56 +08:00
|
|
|
}
|