gerson
2025-01-22 578928d66720a75e6d06611324532dcb711e079c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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;
};