Files
sgxt_web/src/utils/route.js
2026-01-26 17:27:28 +08:00

140 lines
4.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import path from "path";
import { getItem } from '@/utils/storage'
/*
*获取所有的子集路由
*/
const getChildrenRoutes = (routes) => {
const result = [];
const { deptBizType, deptLevel } = getItem('deptId')[0]
const roleList = getItem('roleList') ? getItem('roleList').filter(item => item.roleCode == 'JS_666666').length > 0 : false
const xjLsit = getItem('roleList') ? getItem('roleList').filter(item => item.roleCode == 'JS_999999').length > 0 : false
console.log(roleList, xjLsit);
routes.forEach((route) => {
if (route.children && route.children.length > 0) {
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);
}
}
} 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);
}
}
// 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);
// }
// }
}
});
return result;
};
/*
*处理脱离层级的路由
*/
export const filterRoutes = (routes) => {
//获取到所有的子集路由
const childrenRoutes = getChildrenRoutes(routes);
const data = routes.filter((route) => {
//根据route在childrenRoutes中进行查重把所有重复路由表 剔除
return !childrenRoutes.find((childrenRoute) => {
return childrenRoute.path === route.path;
});
}).filter(item => item.path != '/internalAuditor' && item.path != '/auditList')
return data
};
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;
}