From 76544eb61d74c993b88f70f66ae02427add0cbda Mon Sep 17 00:00:00 2001 From: fupeijie <1161353057@qq.com> Date: Wed, 4 Dec 2024 14:53:09 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=90=88=E5=B9=B6=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rds-console/console-ui/src/utils/ruoyi.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/rds-console/console-ui/src/utils/ruoyi.js b/rds-console/console-ui/src/utils/ruoyi.js index 9e0b0f7..629ae82 100644 --- a/rds-console/console-ui/src/utils/ruoyi.js +++ b/rds-console/console-ui/src/utils/ruoyi.js @@ -141,19 +141,23 @@ export function praseStrEmpty(str) { // 数据合并 export function mergeRecursive(source, target) { - for (var p in target) { - try { - if (target[p].constructor == Object) { - source[p] = mergeRecursive(source[p], target[p]); + if (!source || typeof source !== 'object') source = {}; + if (!target || typeof target !== 'object') return source; + + for (const key in target) { + if (Object.prototype.hasOwnProperty.call(target, key)) { + const targetValue = target[key]; + const sourceValue = source[key]; + + if (targetValue && typeof targetValue === 'object' && !Array.isArray(targetValue)) { + source[key] = mergeRecursive(sourceValue, targetValue); } else { - source[p] = target[p]; + source[key] = targetValue; } - } catch (e) { - source[p] = target[p]; } } return source; -}; +} /** * 构造树型结构数据 -- Gitee From 399d9c4332ba8d1f937a945be5f217c1734f3c47 Mon Sep 17 00:00:00 2001 From: fupeijie <1161353057@qq.com> Date: Wed, 4 Dec 2024 16:09:49 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=9E=84=E9=80=A0?= =?UTF-8?q?=E6=A0=91=E5=BD=A2=E7=BB=93=E6=9E=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rds-console/console-ui/src/utils/ruoyi.js | 73 ++++++++++++----------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/rds-console/console-ui/src/utils/ruoyi.js b/rds-console/console-ui/src/utils/ruoyi.js index 629ae82..47e43e5 100644 --- a/rds-console/console-ui/src/utils/ruoyi.js +++ b/rds-console/console-ui/src/utils/ruoyi.js @@ -161,52 +161,53 @@ export function mergeRecursive(source, target) { /** * 构造树型结构数据 - * @param {*} data 数据源 - * @param {*} id id字段 默认 'id' - * @param {*} parentId 父节点字段 默认 'parentId' - * @param {*} children 孩子节点字段 默认 'children' + * @param {Array} data 数据源 + * @param {string} [id='id'] id字段 + * @param {string} [parentId='parentId'] 父节点字段 + * @param {string} [children='children'] 孩子节点字段 + * @returns {Array} 树型结构数据 */ -export function handleTree(data, id, parentId, children) { - let config = { - id: id || 'id', - parentId: parentId || 'parentId', - childrenList: children || 'children' - }; - - var childrenListMap = {}; - var nodeIds = {}; - var tree = []; - - for (let d of data) { - let parentId = d[config.parentId]; - if (childrenListMap[parentId] == null) { - childrenListMap[parentId] = []; +export function handleTree(data, id = 'id', parentId = 'parentId', children = 'children') { + const config = { id, parentId, children }; + const childrenListMap = new Map(); + const nodeMap = new Map(); + const tree = []; + + // 构建节点映射和子节点映射 + for (const item of data) { + const pid = item[config.parentId]; + const itemId = item[config.id]; + + if (!childrenListMap.has(pid)) { + childrenListMap.set(pid, []); } - nodeIds[d[config.id]] = d; - childrenListMap[parentId].push(d); + childrenListMap.get(pid).push(item); + nodeMap.set(itemId, item); } - for (let d of data) { - let parentId = d[config.parentId]; - if (nodeIds[parentId] == null) { - tree.push(d); + // 构造树的根节点 + for (const item of data) { + const pid = item[config.parentId]; + if (!nodeMap.has(pid)) { + tree.push(item); } } - for (let t of tree) { - adaptToChildrenList(t); - } - - function adaptToChildrenList(o) { - if (childrenListMap[o[config.id]] !== null) { - o[config.childrenList] = childrenListMap[o[config.id]]; - } - if (o[config.childrenList]) { - for (let c of o[config.childrenList]) { - adaptToChildrenList(c); + // 递归组装子节点 + const buildTree = (node) => { + const nodeId = node[config.id]; + if (childrenListMap.has(nodeId)) { + node[config.children] = childrenListMap.get(nodeId); + for (const child of node[config.children]) { + buildTree(child); } } + }; + + for (const rootNode of tree) { + buildTree(rootNode); } + return tree; } -- Gitee