zhanbo.xu

feat: 修改权限树的逻辑

......@@ -25,3 +25,30 @@ export function buildMenuTree(menuList) {
return tree;
}
export function getBindingLeafMenuIds(menuList) {
// 创建一个映射,用于快速查找菜单项
const menuMap = new Map();
const bindingMenuIds = new Set();
const parentMenuIds = new Set();
// 首先处理所有菜单项
menuList.forEach((menu) => {
menuMap.set(menu.menuId, menu);
// 记录所有父级menuId
if (menu.parentMenuId !== 0) {
parentMenuIds.add(menu.parentMenuId);
}
// 记录所有isBinding为true的menuId
if (menu.isBinding) {
bindingMenuIds.add(menu.menuId);
}
});
// 从bindingMenuIds中移除所有父级menuId
parentMenuIds.forEach((parentId) => {
bindingMenuIds.delete(parentId);
});
return Array.from(bindingMenuIds);
}
......
......@@ -138,11 +138,8 @@ export default {
},
methods: {
buildMenuTree(menuList) {
// 创建一个映射,用于快速查找菜单项
const menuMap = new Map();
const tree = [];
// 首先将所有菜单项放入映射中
menuList.forEach((menu) => {
menuMap.set(menu.menuId, { ...menu, children: [] });
});
......@@ -151,10 +148,8 @@ export default {
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);
......@@ -292,7 +287,8 @@ export default {
if (res.code === "200") {
this.msgSuccess(res.message);
this.close(true);
this.$store.dispatch("GenerateRoutes");
// 立即刷新菜单相当于load
// this.$store.dispatch("GenerateRoutes");
} else {
this.alertWarningMsg(res.message);
this.loadings.dialogLoading = false;
......@@ -317,15 +313,38 @@ export default {
this.$emit("close", flag);
},
// 加载已绑定的菜单
getBindingLeafMenuIds(menuList) {
const menuMap = new Map();
const bindingMenuIds = new Set();
const parentMenuIds = new Set();
menuList.forEach((menu) => {
menuMap.set(menu.menuId, menu);
if (menu.parentMenuId !== 0) {
parentMenuIds.add(menu.parentMenuId);
}
if (menu.isBinding) {
bindingMenuIds.add(menu.menuId);
}
});
parentMenuIds.forEach((parentId) => {
bindingMenuIds.delete(parentId);
});
return Array.from(bindingMenuIds);
},
loadCheckedKeys() {
if (this.formData.roleId) {
getMenuByRoleId(this.formData.roleId)
.then((res) => {
if (res.code === "200") {
this.checkedKeys = res.data
.filter((item) => item.isBinding && item.parentMenuId != 0)
.map((item) => item.menuId);
//console.log('初始 checkedKeys:', this.checkedKeys);
// console.log(this.getBindingLeafMenuIds(res.data));
this.checkedKeys = this.getBindingLeafMenuIds(res.data);
// this.checkedKeys = res.data
// .filter((item) => item.isBinding && item.parentMenuId != 0)
// .map((item) => item.menuId);
// console.log("初始 checkedKeys:", this.checkedKeys);
this.$nextTick(() => {
this.$refs.tree.setCheckedKeys(this.checkedKeys);
this.updateTreeCheckData();
......