菜单问题修改
This commit is contained in:
@ -92,43 +92,53 @@ function convertMenuData(menus: any[]): any[] {
|
||||
}
|
||||
|
||||
// 新增递归过滤函数
|
||||
function filterMenusByLocalConfig(localMenus: any[], remoteMenus: any[]): any[] {
|
||||
function filterMenusByLocalConfig(localMenus: any[], remoteMenus: any[], isRoot = true): any[] {
|
||||
if (!Array.isArray(localMenus) || !Array.isArray(remoteMenus)) return [];
|
||||
// remoteMenus 可能有 children 字段,也可能有 routes 字段
|
||||
const remoteMap = new Map();
|
||||
remoteMenus.forEach(r => {
|
||||
if (r.path) remoteMap.set(r.path, r);
|
||||
|
||||
// 只在第一次递归时找 path: '/' 节点
|
||||
if (isRoot) {
|
||||
const rootMenu = localMenus.find(m => m.path === '/' && Array.isArray(m.routes));
|
||||
if (!rootMenu) return [];
|
||||
const filteredRoutes = filterMenusByLocalConfig(rootMenu.routes, remoteMenus, false);
|
||||
return [{
|
||||
...rootMenu,
|
||||
routes: filteredRoutes,
|
||||
}];
|
||||
}
|
||||
|
||||
// 子级对比,只需要对比 path 就行
|
||||
const remotePathSet = new Set(remoteMenus.map(r => r.path).filter(Boolean));
|
||||
return (localMenus as any[]).filter(local =>
|
||||
local.path && remotePathSet.has(local.path)
|
||||
).map(local => {
|
||||
const remote = remoteMenus.find(r => r.path === local.path);
|
||||
const localChildren = local.children || local.routes;
|
||||
const remoteChildren = remote?.children || remote?.routes;
|
||||
let newChildren: any[] = [];
|
||||
if (
|
||||
localChildren && localChildren.length > 0 &&
|
||||
remoteChildren && remoteChildren.length > 0
|
||||
) {
|
||||
newChildren = filterMenusByLocalConfig(localChildren, remoteChildren, false);
|
||||
}
|
||||
const node = { ...local };
|
||||
if (local.children) {
|
||||
if (newChildren.length > 0) node.children = newChildren;
|
||||
else delete node.children;
|
||||
}
|
||||
if (local.routes) {
|
||||
if (newChildren.length > 0) node.routes = newChildren;
|
||||
else delete node.routes;
|
||||
}
|
||||
return node;
|
||||
});
|
||||
|
||||
// filter 和 map 后的结果直接 return
|
||||
localMenus
|
||||
.filter(local => local.path && remoteMap.has(local.path))
|
||||
.map(local => {
|
||||
const remote = remoteMap.get(local.path);
|
||||
// 兼容 children 或 routes 字段
|
||||
const localChildren = local.children || local.routes;
|
||||
const remoteChildren = remote.children || remote.routes;
|
||||
let newChildren: any[] = [];
|
||||
if (
|
||||
localChildren && localChildren.length > 0 &&
|
||||
remoteChildren && remoteChildren.length > 0
|
||||
) {
|
||||
newChildren = filterMenusByLocalConfig(localChildren, remoteChildren);
|
||||
}
|
||||
// 保留原有结构,只替换 children 或 routes
|
||||
let node = { ...local };
|
||||
if (local.children) {
|
||||
node.children = newChildren;
|
||||
} else if (local.routes) {
|
||||
node.routes = newChildren;
|
||||
}
|
||||
return node;
|
||||
});
|
||||
|
||||
return localMenus
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
const BasicLayout: React.FC<BasicLayoutProps> = (props) => {
|
||||
const { children, tab, dispatch } = props;
|
||||
const location = useLocation();
|
||||
@ -157,6 +167,8 @@ const BasicLayout: React.FC<BasicLayoutProps> = (props) => {
|
||||
if (menuStr) {
|
||||
const menus = JSON.parse(menuStr);
|
||||
const filteredMenus = filterMenusByLocalConfig(routes, menus);
|
||||
console.log(filteredMenus,'filteredMenus');
|
||||
|
||||
setMenuRoutes(convertMenuData(filteredMenus));
|
||||
}
|
||||
}, []);
|
||||
|
Reference in New Issue
Block a user