|
|
@@ -13,12 +13,11 @@ export type ParseMenuType = {
|
|
|
};
|
|
|
|
|
|
export function sortMenu(menus: TreeRoleMenuData[]) {
|
|
|
- const topMenu: ParseMenuType[] = [],
|
|
|
- childMenu: ParseMenuType[] = [];
|
|
|
+ const result: ParseMenuType[] = [];
|
|
|
+ const tempAreaMap = new Map<string, ParseMenuType[]>();
|
|
|
|
|
|
- // 拆分一级菜单和二级菜单
|
|
|
- menus.forEach(function({id, pId, name, url, img}) {
|
|
|
- const data = {
|
|
|
+ menus.forEach(function({id, name, url, img, pId}) {
|
|
|
+ const tempValue: ParseMenuType = {
|
|
|
key: id,
|
|
|
title: name,
|
|
|
label: name,
|
|
|
@@ -27,16 +26,27 @@ export function sortMenu(menus: TreeRoleMenuData[]) {
|
|
|
icon: img && <FontIcon name={img} />,
|
|
|
};
|
|
|
|
|
|
- pId === '0' ? topMenu.push(data) : childMenu.push(data);
|
|
|
- });
|
|
|
+ if (pId === '0') {
|
|
|
+ // 是一级菜单找是否在临时区域有保存的二级菜单
|
|
|
+ const tempList = tempAreaMap.get(id);
|
|
|
+
|
|
|
+ result.push({...tempValue, children: tempList ? [...tempList] : []});
|
|
|
+ } else {
|
|
|
+ // 二级菜单判断是否有对应的一级菜单 如果有加到父组件的children里
|
|
|
+ // 如果没有放在临时Map中
|
|
|
+ const idx = result.findIndex(val => val.key === pId);
|
|
|
|
|
|
- // 填充一级菜单
|
|
|
- childMenu.forEach(function(data) {
|
|
|
- const index = topMenu.findIndex(val => val.key === data.pid);
|
|
|
- topMenu[index]?.children
|
|
|
- ? topMenu[index]!.children!.push(data)
|
|
|
- : (topMenu[index] = {...topMenu[index], children: [data]});
|
|
|
+ if (idx >= 0) {
|
|
|
+ result[idx].children!.push(tempValue);
|
|
|
+ } else {
|
|
|
+ if (tempAreaMap.has(pId)) {
|
|
|
+ tempAreaMap.get(pId)!.push(tempValue);
|
|
|
+ } else {
|
|
|
+ tempAreaMap.set(pId, [tempValue]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
});
|
|
|
|
|
|
- return topMenu;
|
|
|
+ return result;
|
|
|
}
|