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(递归添加所有层级的父菜单)