ke_junjie
2025-06-04 84620534eb627e95811b971a4b552b6a177829bf
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import color from './modules/color.js'
import eleResize from './modules/eleResize.js'
import project from './modules/project.js'
import regexValidate from './modules/regexValidate.js'
 
const __UTILS = {
    color,
    eleResize,
    project,
    regexValidate,
    /*
     * 获取object对象类型
     */
    getObjectType(obj){
        if (typeof obj !== 'object') {
            return null;
        } else {
            let objTypeStr = Object.prototype.toString.call(obj).toLowerCase().trim();
            objTypeStr = objTypeStr.substr(1,objTypeStr.length-2)
            let tempA = objTypeStr.split(" ");
            return tempA[1];
        }
    },
    /* 
     * 从树形结构的数组中获取对象
     * tree:树形结构数组
     * values:对象路径值的数组
     * keyName:节点对象中值对应的key
     * childrenName:节点对象中children对应的key
     */
    getTreeItemByKeyValues(tree,values,keyName,childrenName){
        try {
            let res = null;
            for(let i=0;i<tree.length;i++) {
                if (tree[i][keyName]===values[0]) {
                    res = tree[i];
                    break;
                }
            }
            if (values[1]) {
                let newValues = [...values];
                newValues.shift();
                res = __UTILS.getTreeItemByKeyValues(res[childrenName],newValues,keyName)
            }
            return res;
        } catch (e) {
            return null;
        }
    },
    /* 数字格式化,前面补足 */
    numberFormat(val,fix=2,replaceChar="0"){
        if (typeof val === 'number') {
            val = val.toString();
        }
        if (typeof val !== 'string') {
            return val;
        }
        let len = val.length, newVal = val;
        if (len<fix) {
            for (let i=len;i<fix;i++){
                newVal = replaceChar + newVal;
            }
        } 
        return newVal
    },
    /*
     *    树形结构扁平化
     *     treeArr:原树形数组
     *    props:字段名称
     *  parentVal:默认父节点的值,即根节点
     */
    treeToList(treeArr,props={},parentVal=''){
        let stack = [...treeArr], res = [];
        const defaultProps = {id:'id',parentId:'parentId',children:'children'};
        props = {...defaultProps,...props}
        
        while(stack.length!==0) {
            let shift = stack.shift();
            let tempObj = {...shift};
            if (tempObj[props.children]) {
                delete tempObj[props.children]
            }
            if (parentVal!=='') {
                tempObj[props.parentId] = parentVal;
            }
            res.push(tempObj)
            
            if (shift[props.children]) {
                let childrenRes = __UTILS.treeToList(shift[props.children],props,shift[props.id])
                res = [...res,...childrenRes]
            }
        }
        
        return res
    },
    /* 生成随机uuid
    *    n:uuid的长度
    * type:类型(0-大小写字母+数字,1-小写字母+数字,2-纯数字,3-大小写字母,4-小写字母)
    */
    createUuid(n=6,type=0){
        let res='', uType=0;
        const __uuidLetter = function(){
            const __tempArr = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
            return __tempArr[Math.floor(Math.random()*26)];
        }
        for (let i=0;i<n;i++) {
            switch(type) {
                case 1:
                    uType = Math.floor(Math.random()*2);
                    if (uType===0) {
                        res += Math.floor(Math.random()*10).toString()
                    } else {
                        res += __uuidLetter()
                    }
                    break;
                case 2:
                    res += Math.floor(Math.random()*10).toString()
                    break;
                case 3:
                    uType = Math.floor(Math.random()*2);
                    if (uType===0) {
                        res += __uuidLetter()
                    } else {
                        res += __uuidLetter().toUpperCase()
                    }
                    break;
                case 4:
                    res += __uuidLetter()
                    break;
                default:
                    uType = Math.floor(Math.random()*3);
                    if (uType===0) {
                        res += Math.floor(Math.random()*10).toString()
                    } else if (uType===1) {
                        res += __uuidLetter()
                    } else {
                        res += __uuidLetter().toUpperCase()
                    }
                    break;
            }
        }
        return res;
    },
    /*
     * 在Object类型的数组中,获取某项在数组中的位置
     * 参数:
     *    arr:被查询的数组
     *    searchItem:查询值/对象
     *    compareField:数组中对比的字段
     *    compareItemField:查询对象中对比的字段
     */
    getObjectArrayIndex(arr,searchItem,compareField,compareItemField=null){
        let res = -1;
        let searchItemType = __UTILS.getObjectType(searchItem);
        for (let i=0;i<arr.length;i++) {
            if (searchItemType===null) {
                if (arr[i][compareField]===searchItem) {
                    res = i;
                    break;
                } 
            } else {
                if (compareItemField===null) {
                    if (arr[i][compareField]===searchItem[compareField]) {
                        res = i;
                        break;
                    }
                } else {
                    if (arr[i][compareField]===searchItem[compareItemField]) {
                        res = i;
                        break;
                    }
                }
            }
        }
        return res;
    }
}
 
export default __UTILS