ruoyi-plus-soybean/src/utils/router/helpers.ts

156 lines
4.0 KiB
TypeScript
Raw Normal View History

import type { RouteRecordRaw } from 'vue-router';
import { Layout } from '@/layouts';
import { consoleError } from '../common';
import { getViewComponent } from './component';
type ComponentAction = {
[key in AuthRoute.RouteComponent]: () => void;
};
/** 将权限路由类型转换成vue路由类型 */
export function transformAuthRouteToVueRoute(item: AuthRoute.Route) {
const { name, path, meta } = item;
const itemRoute: Partial<RouteRecordRaw> = {
name,
path,
meta
};
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)) {
2022-01-05 01:35:32 +08:00
(itemRoute as any).props = item.props;
}
if (isSingleRoute(item)) {
if (item.name === 'redirect-not-found') {
itemRoute.children = [
{
path: '',
component: getViewComponent('redirect-not-found')
}
];
return itemRoute as RouteRecordRaw;
}
const singleRoute = {
...itemRoute
};
Object.assign(singleRoute, { component: getViewComponent(item.name) });
const singlePath = (
hasSingleOriginPath(item) ? item.meta.singleOriginPath : item.path
) as AuthRoute.SingleRoutePath;
const parenPath = `${singlePath}-parent` as AuthRoute.SingleRouteParentPath;
const parentRoute: Partial<RouteRecordRaw> = {
path: parenPath,
component: itemRoute.component,
redirect: singlePath,
children: [singleRoute as RouteRecordRaw]
};
return parentRoute as RouteRecordRaw;
}
if (hasChildren(item)) {
itemRoute.children = item.children!.map(child => transformAuthRouteToVueRoute(child)) as RouteRecordRaw[];
}
return itemRoute as RouteRecordRaw;
}
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);
}
function isSingleRoute(item: AuthRoute.Route) {
return Boolean(item.meta.single);
}
function hasSingleOriginPath(item: AuthRoute.Route) {
return Boolean(item.meta.singleOriginPath);
}
/**
* key获取AuthRoute数据
* @param key - key
* @param routes -
*/
export function findAuthRouteByKey(key: AuthRoute.RouteKey, routes: AuthRoute.Route[]) {
const paths = getRouteKeyPathsByKey(key);
const route = recursiveFindRouteByPaths(paths, routes);
return route;
}
/**
* key的paths获递归取路由
* @param paths - key的路径
* @param routes -
*/
function recursiveFindRouteByPaths(
paths: AuthRoute.RouteKey[],
routes: AuthRoute.Route[]
): AuthRoute.Route | undefined {
const item = routes.find(route => paths.length && route.name === paths[0]);
if (item && hasComponent(item)) {
return recursiveFindRouteByPaths(paths.slice(1), item.children!);
}
return item;
}
/**
* 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);
}
});
return keyPaths;
}