Files
sgxt_web/src/utils/route.js

140 lines
4.4 KiB
JavaScript
Raw Normal View History

2025-04-12 14:54:02 +08:00
import path from "path";
2025-11-22 21:59:58 +08:00
import { getItem } from '@/utils/storage'
2025-04-12 14:54:02 +08:00
/*
*获取所有的子集路由
*/
const getChildrenRoutes = (routes) => {
const result = [];
2025-12-27 11:10:31 +08:00
const { deptBizType, deptLevel } = getItem('deptId')[0]
2026-01-06 22:09:05 +08:00
const roleList = getItem('roleList') ? getItem('roleList').filter(item => item.roleCode == 'JS_666666').length > 0 : false
2026-01-26 17:27:28 +08:00
const xjLsit = getItem('roleList') ? getItem('roleList').filter(item => item.roleCode == 'JS_999999').length > 0 : false
console.log(roleList, xjLsit);
2025-04-12 14:54:02 +08:00
routes.forEach((route) => {
if (route.children && route.children.length > 0) {
2026-01-26 17:27:28 +08:00
if (deptBizType == '23') {
if (roleList) {
result.push(...route.children);
} else if (xjLsit) {
if (route.path == '/JudgmentHome') {
route.children.splice(route.children.findIndex(item => item.path == '/internalAuditor'), 1)
result.push(...route.children);
} else {
result.push(...route.children);
}
} else {
if (route.path == '/HumanIntelligence') {
route.children.splice(route.children.findIndex(item => item.path == '/auditList'), 1)
result.push(...route.children);
} else {
result.push(...route.children);
}
if (route.path == '/JudgmentHome') {
route.children.splice(route.children.findIndex(item => item.path == '/internalAuditor'), 1)
result.push(...route.children);
} else {
result.push(...route.children);
}
}
2025-12-27 11:10:31 +08:00
} else {
2026-01-23 15:43:22 +08:00
if (route.path == '/HumanIntelligence') {
route.children.splice(route.children.findIndex(item => item.path == '/auditList'), 1)
result.push(...route.children);
} else {
result.push(...route.children);
}
2025-12-27 11:10:31 +08:00
if (route.path == '/JudgmentHome') {
2026-01-16 12:40:42 +08:00
route.children.splice(route.children.findIndex(item => item.path == '/internalAuditor'), 1)
2025-12-27 11:10:31 +08:00
result.push(...route.children);
} else {
result.push(...route.children);
}
}
2026-01-26 17:27:28 +08:00
// if (deptBizType == '23' && (roleList || xjLsit)) {
// // 在这个条件分支中也需要过滤掉/internalAuditor路由
// result.push(...route.children);
// } else {
// if (route.path == '/JudgmentHome' && xjLsit) {
// route.children.splice(route.children.findIndex(item => item.path == '/internalAuditor'), 1)
// result.push(...route.children);
// } else {
// result.push(...route.children);
// }
// }
2025-04-12 14:54:02 +08:00
}
});
return result;
};
/*
*处理脱离层级的路由
*/
export const filterRoutes = (routes) => {
2026-01-16 12:40:42 +08:00
2025-04-12 14:54:02 +08:00
//获取到所有的子集路由
const childrenRoutes = getChildrenRoutes(routes);
2026-01-16 12:40:42 +08:00
const data = routes.filter((route) => {
2025-04-12 14:54:02 +08:00
//根据route在childrenRoutes中进行查重把所有重复路由表 剔除
return !childrenRoutes.find((childrenRoute) => {
return childrenRoute.path === route.path;
});
2026-01-23 15:43:22 +08:00
}).filter(item => item.path != '/internalAuditor' && item.path != '/auditList')
2026-01-16 12:40:42 +08:00
return data
2025-04-12 14:54:02 +08:00
};
function isNull(data) {
if (!data) return true;
if (JSON.stringify(data) === "{}") return true;
if (JSON.stringify(data) === "[]") return true;
return false;
}
/*
*根据routesfilterRoutes 数据 返回对应的menu规则数据
*/
export function generateMenus(routes, basePath = "") {
const result = [];
// 遍历路由表
routes.forEach((item) => {
// 不存在 children && 不存在 meta 直接 return
if (isNull(item.meta) && isNull(item.children)) return;
// 存在 children 不存在 meta进入迭代
if (isNull(item.meta) && !isNull(item.children)) {
result.push(...generateMenus(item.children));
return;
}
// 合并 path 作为跳转路径
const routePath = path.resolve(basePath, item.path);
// 路由分离之后,存在同名父路由的情况,需要单独处理
let route = result.find((item) => item.path === routePath);
if (!route) {
route = {
...item,
path: routePath,
children: []
};
// icon 与 title 必须全部存在
if (route.meta.icon && route.meta.title) {
// meta 存在生成 route 对象,放入 arr
result.push(route);
}
}
// 存在 children 进入迭代到children
if (!isNull(item.children)) {
route.children.push(...generateMenus(item.children, route.path));
}
});
return result;
}
2026-01-16 12:40:42 +08:00