import { travelTree } from '/@/utils/util';
|
|
/**
|
* 获取第一个指定 logicType,指定 logicID 的节点
|
* @param treeData
|
* @returns
|
*/
|
export const getSite = (
|
treeData: any[],
|
logicType: {
|
key?: string;
|
value: string;
|
},
|
logicID?: {
|
key?: string;
|
value: string;
|
}
|
) => {
|
let foundSite = null;
|
let firstSite = null;
|
travelTree(treeData, (value) => {
|
const logicTypeKey = logicType?.key ?? 'LogicType';
|
const logicTypeValue = logicType.value;
|
|
if (value[logicTypeKey] === logicTypeValue) {
|
if (!firstSite) {
|
firstSite = value;
|
}
|
if (logicID && logicID?.value) {
|
const logicIDKey = logicID.key ?? 'LogicID';
|
const logicIDValue = logicID.value;
|
if (value[logicIDKey] !== logicIDValue) return;
|
}
|
foundSite = value;
|
return true;
|
}
|
});
|
// 没找到指定 ID 的节点,取第一个
|
if (logicID && logicID?.value && !foundSite) {
|
foundSite = firstSite;
|
}
|
return foundSite;
|
};
|