zhanbo.xu

feat: 新增功能

export function buildMenuTree(menuList) {
// 创建一个映射,用于快速查找菜单项
const menuMap = new Map();
const tree = [];
// 首先将所有菜单项放入映射中
menuList.forEach((menu) => {
menuMap.set(menu.menuId, { ...menu, children: [] });
});
// 构建树形结构
menuList.forEach((menu) => {
const menuItem = menuMap.get(menu.menuId);
if (menu.parentMenuId === 0) {
// 如果是顶级菜单,直接添加到树中
tree.push(menuItem);
} else {
// 如果不是顶级菜单,找到其父菜单并添加到父菜单的children中
const parent = menuMap.get(menu.parentMenuId);
if (parent) {
parent.children.push(menuItem);
}
}
});
return tree;
}
......@@ -39,7 +39,7 @@
:data="treeData"
show-checkbox
default-expand-all
node-key="id"
node-key="menuId"
:default-expanded-keys="expandedKeys"
:default-checked-keys="checkedKeys"
:props="defaultProps"
......@@ -125,7 +125,7 @@ export default {
},
defaultProps: {
children: "children",
label: "label",
label: "menuName",
},
searchForm: {},
loadings: {
......@@ -137,51 +137,81 @@ export default {
};
},
methods: {
buildMenuTree(menuList) {
// 创建一个映射,用于快速查找菜单项
const menuMap = new Map();
const tree = [];
// 首先将所有菜单项放入映射中
menuList.forEach((menu) => {
menuMap.set(menu.menuId, { ...menu, children: [] });
});
// 构建树形结构
menuList.forEach((menu) => {
const menuItem = menuMap.get(menu.menuId);
if (menu.parentMenuId === 0) {
// 如果是顶级菜单,直接添加到树中
tree.push(menuItem);
} else {
// 如果不是顶级菜单,找到其父菜单并添加到父菜单的children中
const parent = menuMap.get(menu.parentMenuId);
if (parent) {
parent.children.push(menuItem);
}
}
});
return tree;
},
//查询菜单数据
searchTreeData() {
this.loadings.dialogLoading = true;
getRouters()
.then((res) => {
if (res.code === "200") {
let arr = [];
res.data.forEach((item) => {
let status = true;
if (item.parentMenuId == 0) {
let obj = {
id: item.menuId,
label: item.menuName,
children: [],
};
arr.forEach((menuItem) => {
if (menuItem.id == item.menuId) {
status = false;
menuItem.label = item.menuName;
}
});
if (status) {
arr.push(obj);
}
} else {
let obj = {
id: item.menuId,
label: item.menuName,
};
arr.forEach((menuItem) => {
if (menuItem.id == item.parentMenuId) {
status = false;
menuItem.children.push(obj);
}
});
if (status) {
arr.push({
id: item.parentMenuId,
label: "",
children: [obj],
});
}
}
});
this.treeData = arr;
console.log(this.buildMenuTree(res.data));
// console.log(res.data);
// let arr = [];
// res.data.forEach((item) => {
// let status = true;
// if (item.parentMenuId == 0) {
// let obj = {
// id: item.menuId,
// label: item.menuName,
// children: [],
// };
// arr.forEach((menuItem) => {
// if (menuItem.id == item.menuId) {
// status = false;
// menuItem.label = item.menuName;
// }
// });
// if (status) {
// arr.push(obj);
// }
// } else {
// let obj = {
// id: item.menuId,
// label: item.menuName,
// };
// arr.forEach((menuItem) => {
// if (menuItem.id == item.parentMenuId) {
// status = false;
// menuItem.children.push(obj);
// }
// });
// if (status) {
// arr.push({
// id: item.parentMenuId,
// label: "",
// children: [obj],
// });
// }
// }
// });
this.treeData = this.buildMenuTree(res.data);
} else {
this.alertWarningMsg(res.message);
}
......