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

70 lines
1.9 KiB
TypeScript
Raw Normal View History

/**
*
* @param routes -
*/
export function getConstantRouteNames(routes: AuthRoute.Route[]) {
return routes.map(route => getConstantRouteName(route)).flat(1);
}
/**
*
* @param routes -
* @param treeMap
*/
export function transformAuthRouteToSearchMenus(routes: AuthRoute.Route[], treeMap: AuthRoute.Route[] = []) {
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) {
transformAuthRouteToSearchMenus(cur.children, treeMap);
}
return acc;
}, treeMap);
}
/** 将路由名字转换成路由路径 */
export function transformRouteNameToRoutePath(name: Exclude<AuthRoute.AllRouteKey, 'not-found'>): AuthRoute.RoutePath {
const rootPath: AuthRoute.RoutePath = '/';
if (name === 'root') return rootPath;
const splitMark = '_';
const pathSplitMark = '/';
const path = name.split(splitMark).join(pathSplitMark);
return (pathSplitMark + path) as AuthRoute.RoutePath;
}
/** 将路由路径转换成路由名字 */
export function transformRoutePathToRouteName<K extends AuthRoute.RoutePath>(path: K) {
if (path === '/') return 'root';
const pathSplitMark = '/';
const routeSplitMark = '_';
const name = path.split(pathSplitMark).slice(1).join(routeSplitMark) as AuthRoute.AllRouteKey;
return name;
}
/**
*
* @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;
}
/**
*
* @param item -
*/
function hasChildren(item: AuthRoute.Route) {
return Boolean(item.children && item.children.length);
}