zhanbo.xu

feat: 修改权限树的逻辑

...@@ -25,3 +25,30 @@ export function buildMenuTree(menuList) { ...@@ -25,3 +25,30 @@ export function buildMenuTree(menuList) {
25 25
26 return tree; 26 return tree;
27 } 27 }
28 +
29 +export function getBindingLeafMenuIds(menuList) {
30 + // 创建一个映射,用于快速查找菜单项
31 + const menuMap = new Map();
32 + const bindingMenuIds = new Set();
33 + const parentMenuIds = new Set();
34 +
35 + // 首先处理所有菜单项
36 + menuList.forEach((menu) => {
37 + menuMap.set(menu.menuId, menu);
38 + // 记录所有父级menuId
39 + if (menu.parentMenuId !== 0) {
40 + parentMenuIds.add(menu.parentMenuId);
41 + }
42 + // 记录所有isBinding为true的menuId
43 + if (menu.isBinding) {
44 + bindingMenuIds.add(menu.menuId);
45 + }
46 + });
47 +
48 + // 从bindingMenuIds中移除所有父级menuId
49 + parentMenuIds.forEach((parentId) => {
50 + bindingMenuIds.delete(parentId);
51 + });
52 +
53 + return Array.from(bindingMenuIds);
54 +}
......
...@@ -138,11 +138,8 @@ export default { ...@@ -138,11 +138,8 @@ export default {
138 }, 138 },
139 methods: { 139 methods: {
140 buildMenuTree(menuList) { 140 buildMenuTree(menuList) {
141 - // 创建一个映射,用于快速查找菜单项
142 const menuMap = new Map(); 141 const menuMap = new Map();
143 const tree = []; 142 const tree = [];
144 -
145 - // 首先将所有菜单项放入映射中
146 menuList.forEach((menu) => { 143 menuList.forEach((menu) => {
147 menuMap.set(menu.menuId, { ...menu, children: [] }); 144 menuMap.set(menu.menuId, { ...menu, children: [] });
148 }); 145 });
...@@ -151,10 +148,8 @@ export default { ...@@ -151,10 +148,8 @@ export default {
151 menuList.forEach((menu) => { 148 menuList.forEach((menu) => {
152 const menuItem = menuMap.get(menu.menuId); 149 const menuItem = menuMap.get(menu.menuId);
153 if (menu.parentMenuId === 0) { 150 if (menu.parentMenuId === 0) {
154 - // 如果是顶级菜单,直接添加到树中
155 tree.push(menuItem); 151 tree.push(menuItem);
156 } else { 152 } else {
157 - // 如果不是顶级菜单,找到其父菜单并添加到父菜单的children中
158 const parent = menuMap.get(menu.parentMenuId); 153 const parent = menuMap.get(menu.parentMenuId);
159 if (parent) { 154 if (parent) {
160 parent.children.push(menuItem); 155 parent.children.push(menuItem);
...@@ -292,7 +287,8 @@ export default { ...@@ -292,7 +287,8 @@ export default {
292 if (res.code === "200") { 287 if (res.code === "200") {
293 this.msgSuccess(res.message); 288 this.msgSuccess(res.message);
294 this.close(true); 289 this.close(true);
295 - this.$store.dispatch("GenerateRoutes"); 290 + // 立即刷新菜单相当于load
291 + // this.$store.dispatch("GenerateRoutes");
296 } else { 292 } else {
297 this.alertWarningMsg(res.message); 293 this.alertWarningMsg(res.message);
298 this.loadings.dialogLoading = false; 294 this.loadings.dialogLoading = false;
...@@ -317,15 +313,38 @@ export default { ...@@ -317,15 +313,38 @@ export default {
317 this.$emit("close", flag); 313 this.$emit("close", flag);
318 }, 314 },
319 // 加载已绑定的菜单 315 // 加载已绑定的菜单
316 + getBindingLeafMenuIds(menuList) {
317 + const menuMap = new Map();
318 + const bindingMenuIds = new Set();
319 + const parentMenuIds = new Set();
320 +
321 + menuList.forEach((menu) => {
322 + menuMap.set(menu.menuId, menu);
323 + if (menu.parentMenuId !== 0) {
324 + parentMenuIds.add(menu.parentMenuId);
325 + }
326 + if (menu.isBinding) {
327 + bindingMenuIds.add(menu.menuId);
328 + }
329 + });
330 +
331 + parentMenuIds.forEach((parentId) => {
332 + bindingMenuIds.delete(parentId);
333 + });
334 +
335 + return Array.from(bindingMenuIds);
336 + },
320 loadCheckedKeys() { 337 loadCheckedKeys() {
321 if (this.formData.roleId) { 338 if (this.formData.roleId) {
322 getMenuByRoleId(this.formData.roleId) 339 getMenuByRoleId(this.formData.roleId)
323 .then((res) => { 340 .then((res) => {
324 if (res.code === "200") { 341 if (res.code === "200") {
325 - this.checkedKeys = res.data 342 + // console.log(this.getBindingLeafMenuIds(res.data));
326 - .filter((item) => item.isBinding && item.parentMenuId != 0) 343 + this.checkedKeys = this.getBindingLeafMenuIds(res.data);
327 - .map((item) => item.menuId); 344 + // this.checkedKeys = res.data
328 - //console.log('初始 checkedKeys:', this.checkedKeys); 345 + // .filter((item) => item.isBinding && item.parentMenuId != 0)
346 + // .map((item) => item.menuId);
347 + // console.log("初始 checkedKeys:", this.checkedKeys);
329 this.$nextTick(() => { 348 this.$nextTick(() => {
330 this.$refs.tree.setCheckedKeys(this.checkedKeys); 349 this.$refs.tree.setCheckedKeys(this.checkedKeys);
331 this.updateTreeCheckData(); 350 this.updateTreeCheckData();
......