2022-05-09 23:51:19 +08:00
|
|
|
/**
|
|
|
|
* 获取所有固定路由的名称集合
|
|
|
|
* @param routes - 固定路由
|
|
|
|
*/
|
|
|
|
export function getConstantRouteNames(routes: AuthRoute.Route[]) {
|
|
|
|
return routes.map(route => getConstantRouteName(route)).flat(1);
|
|
|
|
}
|
2021-11-28 12:17:48 +08:00
|
|
|
|
2022-03-05 01:55:21 +08:00
|
|
|
/**
|
|
|
|
* 将权限路由转换成搜索的菜单数据
|
|
|
|
* @param routes - 权限路由
|
|
|
|
* @param treeMap
|
|
|
|
*/
|
2022-11-08 01:14:59 +08:00
|
|
|
export function transformAuthRouteToSearchMenus(routes: AuthRoute.Route[], treeMap: AuthRoute.Route[] = []) {
|
2022-01-24 15:34:57 +08:00
|
|
|
if (routes && routes.length === 0) return [];
|
|
|
|
return routes.reduce((acc, cur) => {
|
|
|
|
if (!cur.meta?.hide) {
|
|
|
|
acc.push(cur);
|
|
|
|
}
|
|
|
|
if (cur.children && cur.children.length > 0) {
|
2022-11-08 01:14:59 +08:00
|
|
|
transformAuthRouteToSearchMenus(cur.children, treeMap);
|
2022-01-24 15:34:57 +08:00
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, treeMap);
|
|
|
|
}
|
|
|
|
|
2022-04-23 02:21:02 +08:00
|
|
|
/** 将路由名字转换成路由路径 */
|
2022-11-08 01:14:59 +08:00
|
|
|
export function transformRouteNameToRoutePath(name: Exclude<AuthRoute.AllRouteKey, 'not-found'>): AuthRoute.RoutePath {
|
2022-04-23 02:21:02 +08:00
|
|
|
const rootPath: AuthRoute.RoutePath = '/';
|
|
|
|
if (name === 'root') return rootPath;
|
|
|
|
|
2022-11-08 01:14:59 +08:00
|
|
|
const splitMark = '_';
|
2022-04-23 02:21:02 +08:00
|
|
|
const pathSplitMark = '/';
|
|
|
|
const path = name.split(splitMark).join(pathSplitMark);
|
|
|
|
|
|
|
|
return (pathSplitMark + path) as AuthRoute.RoutePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 将路由路径转换成路由名字 */
|
2022-11-08 01:14:59 +08:00
|
|
|
export function transformRoutePathToRouteName<K extends AuthRoute.RoutePath>(path: K) {
|
2022-04-23 02:21:02 +08:00
|
|
|
if (path === '/') return 'root';
|
|
|
|
|
|
|
|
const pathSplitMark = '/';
|
2022-11-08 01:14:59 +08:00
|
|
|
const routeSplitMark = '_';
|
2022-04-23 02:21:02 +08:00
|
|
|
|
2022-11-08 01:14:59 +08:00
|
|
|
const name = path.split(pathSplitMark).slice(1).join(routeSplitMark) as AuthRoute.AllRouteKey;
|
2022-04-23 02:21:02 +08:00
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2022-05-09 23:51:19 +08:00
|
|
|
/**
|
|
|
|
* 获取所有固定路由的名称集合
|
|
|
|
* @param route - 固定路由
|
|
|
|
*/
|
|
|
|
function getConstantRouteName(route: AuthRoute.Route) {
|
|
|
|
const names = [route.name];
|
|
|
|
if (hasChildren(route)) {
|
|
|
|
names.push(...route.children!.map(item => getConstantRouteName(item)).flat(1));
|
|
|
|
}
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|