This commit is contained in:
‘Liammcl’
2025-01-14 22:33:10 +08:00
parent 7e592df34e
commit e4aa910d4e
11 changed files with 77 additions and 60 deletions

View File

@@ -13,15 +13,21 @@ const getAntIcon = (iconName) => {
const generateMenuItems = (routes, menuKeys = [], parentPath = "") => {
return routes
.filter((route) => {
if (!menuKeys.length) return !route.hidden;
if (route.hidden) return false;
if (!menuKeys.length) return true;
const isRouteAllowed = menuKeys.includes(route.key);
// 如果有子路由,只要子路由中有被授权的,父路由就应该显示
if (route.children) {
const hasAllowedChildren = route.children.some(child =>
menuKeys.includes(child.key) ||
(child.children && child.children.some(grandChild => menuKeys.includes(grandChild.key)))
!child.hidden && (
menuKeys.includes(child.key) ||
(child.children && child.children.some(grandChild =>
!grandChild.hidden && menuKeys.includes(grandChild.key)
))
)
);
return hasAllowedChildren || isRouteAllowed;
}
@@ -56,16 +62,12 @@ export const filterRoutesByMenuKeys = (routes, menuKeys) => {
return [];
}
return routes.reduce((acc, route) => {
// 检查当前路由的 key 是否在 menuKeys 中
const isRouteAllowed = menuKeys.includes(route.key);
// 递归处理子路由
let filteredChildren = [];
if (route.children) {
filteredChildren = filterRoutesByMenuKeys(route.children, menuKeys);
}
// 如果当前路由被允许或者有被允许的子路由,则保留该路由
if (isRouteAllowed || filteredChildren.length > 0) {
acc.push({
...route,