From 039a7d7270e28914bdb8044631298f02aed12afa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=9F=91=E8=9E=82?= Date: Mon, 30 Mar 2026 21:37:25 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20CompanyMenuDrawer=E5=8F=96=E6=B6=88?= =?UTF-8?q?=E5=8B=BE=E9=80=89=E8=8F=9C=E5=8D=95=E5=90=8E=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E4=B8=8D=E7=94=9F=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题原因:当取消勾选父菜单时,其子节点仍在checkedKeys中, ensureParentMenuIds会重新把父菜单加回来,导致取消勾选无效。 修复:在handleTreeCheckChange中,当检测到有节点被取消勾选时, 递归移除该节点的所有子节点。 --- .../src/components/CompanyMenuDrawer.vue | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/ecom-dmb-frontend/apps/web-antd/src/components/CompanyMenuDrawer.vue b/ecom-dmb-frontend/apps/web-antd/src/components/CompanyMenuDrawer.vue index 38a37390..9558bcfe 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/components/CompanyMenuDrawer.vue +++ b/ecom-dmb-frontend/apps/web-antd/src/components/CompanyMenuDrawer.vue @@ -283,7 +283,41 @@ function handleSelectNone() { // 处理树形控件的选中变化 function handleTreeCheckChange(checkedKeysValue: any) { - checkedKeys.value = checkedKeysValue; + // 对比新旧值,找出被取消勾选的节点 + const oldKeys = new Set(checkedKeys.value); + const newKeys = new Set(checkedKeysValue); + + // 找出新增的(勾选)和移除的(取消勾选)的节点 + const addedKeys = newKeys.difference(oldKeys); + const removedKeys = oldKeys.difference(newKeys); + + // 如果是取消勾选,需要把取消节点的子节点也一并移除 + if (removedKeys.size > 0) { + // 构建 parentId -> childIds 的映射 + const parentToChildren = new Map(); + for (const menu of systemMenuList.value) { + if (menu.parentId) { + const children = parentToChildren.get(menu.parentId) || []; + children.push(menu.id); + parentToChildren.set(menu.parentId, children); + } + } + + // 递归移除子节点 + function removeDescendants(nodeId: number) { + const children = parentToChildren.get(nodeId) || []; + for (const childId of children) { + newKeys.delete(childId); + removeDescendants(childId); + } + } + + for (const removedId of removedKeys) { + removeDescendants(removedId); + } + } + + checkedKeys.value = [...newKeys]; } // 确保提交时包含必要的父菜单ID(递归添加所有层级的父菜单)