fix: 角色分配菜单勾选父子级联转出子节点关联根节点数组

This commit is contained in:
TsMask
2024-12-16 11:07:50 +08:00
parent e25cd91df1
commit 5a704146a5
2 changed files with 65 additions and 6 deletions

View File

@@ -126,7 +126,7 @@ export function parseDataToTreeExclude(
}
/**
* 解析树结构数据转出一维id数组
* 解析树结构数据转出所有一维id数组
*
* @param data 数组数据
* @param fieldId 读取节点字段 默认 'id'
@@ -158,7 +158,7 @@ export function parseTreeKeys(
}
/**
* 解析树结构数据转出含子节点的一维id数组
* 解析树结构数据转出节点的一维id数组
*
* @param data 数组数据
* @param fieldId 读取节点字段 默认 'id'
@@ -221,3 +221,43 @@ export function parseDataToOptions(
return options;
}
/**
* 解析树结构数据转出子节点关联根节点的一维id数组
*
* @param data 数组数据
* @param checkedKeys 子节点数组数据
* @param fieldId 读取节点字段 默认 'id'
* @param fieldChildren 读取子节点字段 默认 'children'
* @returns 层级数组
*/
export function parseTreeNodeKeysByChecked(
data: Record<string, any>[],
checkedKeys: (string | number)[],
fieldId: string = 'id',
fieldChildren: string = 'children'
) {
// 节点id
let treeIds: (string | number)[] = [];
componet(data);
/**闭包递归函数 */
function componet(data: Record<string, any>[]) {
if (data.length <= 0) return false;
let hasKey = false;
for (const iterator of data) {
const key = iterator[fieldId];
if (checkedKeys.includes(key)) {
hasKey = true;
}
let nodes = iterator[fieldChildren];
if (Array.isArray(nodes) && nodes.length > 0) {
if (componet(nodes)) {
treeIds.push(key);
hasKey = true;
}
}
}
return hasKey;
}
return treeIds;
}

View File

@@ -23,7 +23,11 @@ import { saveAs } from 'file-saver';
import { parseDateToStr } from '@/utils/date-utils';
import useDictStore from '@/store/modules/dict';
import { DataNode } from 'ant-design-vue/es/tree';
import { parseTreeKeys, parseTreeNodeKeys } from '@/utils/parse-tree-utils';
import {
parseTreeKeys,
parseTreeNodeKeys,
parseTreeNodeKeysByChecked,
} from '@/utils/parse-tree-utils';
import { hasPermissions } from '@/plugins/auth-user';
import { MENU_PATH_INLINE } from '@/constants/menu-constants';
import { RESULT_CODE_SUCCESS } from '@/constants/result-constants';
@@ -328,7 +332,12 @@ function fnModalVisibleByVive(roleId: string | number) {
menuTree.treeData = menus;
modalState.menuTree.treeData = menus;
modalState.menuTree.checkedKeys = checkedKeys;
modalState.from.menuIds = checkedKeys;
if (modalState.from.menuCheckStrictly === '1') {
const ids = parseTreeNodeKeysByChecked(menus, checkedKeys, 'id');
modalState.from.menuIds = ids.concat(checkedKeys);
} else {
modalState.from.menuIds = checkedKeys;
}
}
modalState.title = t('views.system.role.roleInfo');
modalState.openByView = true;
@@ -385,7 +394,12 @@ function fnModalVisibleByEdit(roleId?: string | number) {
menuTree.treeData = menus;
modalState.menuTree.treeData = menus;
modalState.menuTree.checkedKeys = checkedKeys;
modalState.from.menuIds = checkedKeys;
if (modalState.from.menuCheckStrictly === '1') {
const ids = parseTreeNodeKeysByChecked(menus, checkedKeys, 'id');
modalState.from.menuIds = ids.concat(checkedKeys);
} else {
modalState.from.menuIds = checkedKeys;
}
}
modalState.title =
t('common.editText') + t('views.system.role.roleInfo');
@@ -567,7 +581,12 @@ function fnRecordDataScope(roleId: string | number) {
deptTree.treeData = depts;
modalState.deptTree.treeData = depts;
modalState.deptTree.checkedKeys = checkedKeys;
modalState.from.deptIds = checkedKeys;
if (modalState.from.deptCheckStrictly === '1') {
const ids = parseTreeNodeKeysByChecked(depts, checkedKeys, 'id');
modalState.from.deptIds = ids.concat(checkedKeys);
} else {
modalState.from.deptIds = checkedKeys;
}
}
modalState.title = t('views.system.role.distribute');
modalState.openByDataScope = true;