wangzhibo
2026-07-16 b57020623cf0c946706573bf102e548c3a544423
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import { isArray, isNumber, isString, orderBy } from 'lodash-es';
import { resolveComponent } from 'vue';
import storage from './storage';
 
// 首字母大写
export function firstUpperCase(value: string): string {
    return value.replace(/\b(\w)(\w*)/g, function ($0, $1, $2) {
        return $1.toUpperCase() + $2;
    });
}
 
// 获取方法名
export function getNames(value: any) {
    return Object.getOwnPropertyNames(value.constructor.prototype);
}
 
// 获取地址栏参数
export function getUrlParam(name: string): string | null {
    const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
    const r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURIComponent(r[2]);
    return null;
}
 
// 文件名
export function filename(path: string): string {
    return basename(path.substring(0, path.lastIndexOf('.')));
}
 
// 路径名称
export function basename(path: string): string {
    let index = path.lastIndexOf('/');
    index = index > -1 ? index : path.lastIndexOf('\\');
    if (index < 0) {
        return path;
    }
    return path.substring(index + 1);
}
 
// 文件扩展名
export function extname(path: string): string {
    return path.substring(path.lastIndexOf('.') + 1).split(/(\?|&)/)[0];
}
 
// 横杠转驼峰
export function toCamel(str: string): string {
    return str.replace(/([^-])(?:-+([^-]))/g, function ($0, $1, $2) {
        return $1 + $2.toUpperCase();
    });
}
 
// uuid
export function uuid(separator = '-'): string {
    const s: any[] = [];
    const hexDigits = '0123456789abcdef';
    for (let i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = '4';
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
    s[8] = s[13] = s[18] = s[23] = separator;
 
    return s.join('');
}
 
// 浏览器信息
export function getBrowser() {
    const { clientHeight, clientWidth } = document.documentElement;
 
    // 浏览器信息
    const ua = navigator.userAgent.toLowerCase();
 
    // 浏览器类型
    let type = (ua.match(/firefox|chrome|safari|opera/g) || 'other')[0];
 
    if ((ua.match(/msie|trident/g) || [])[0]) {
        type = 'msie';
    }
 
    // 平台标签
    let tag = '';
 
    const isTocuh =
        'ontouchstart' in window || ua.indexOf('touch') !== -1 || ua.indexOf('mobile') !== -1;
    if (isTocuh) {
        if (ua.indexOf('ipad') !== -1) {
            tag = 'pad';
        } else if (ua.indexOf('mobile') !== -1) {
            tag = 'mobile';
        } else if (ua.indexOf('android') !== -1) {
            tag = 'androidPad';
        } else {
            tag = 'pc';
        }
    } else {
        tag = 'pc';
    }
 
    // 浏览器内核
    let prefix = '';
 
    switch (type) {
        case 'chrome':
        case 'safari':
        case 'mobile':
            prefix = 'webkit';
            break;
        case 'msie':
            prefix = 'ms';
            break;
        case 'firefox':
            prefix = 'Moz';
            break;
        case 'opera':
            prefix = 'O';
            break;
        default:
            prefix = 'webkit';
            break;
    }
 
    // 操作平台
    const plat = ua.indexOf('android') > 0 ? 'android' : navigator.platform.toLowerCase();
 
    // 屏幕信息
    let screen = 'full';
 
    if (clientWidth < 768) {
        screen = 'xs';
    } else if (clientWidth < 992) {
        screen = 'sm';
    } else if (clientWidth < 1200) {
        screen = 'md';
    } else if (clientWidth < 1920) {
        screen = 'xl';
    } else {
        screen = 'full';
    }
 
    // 是否 ios
    const isIOS = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
 
    // 是否 PC 端
    const isPC = tag === 'pc';
 
    // 是否移动端
    const isMobile = isPC ? false : true;
 
    // 是否移动端 + 屏幕宽过小
    const isMini = screen === 'xs' || isMobile;
 
    return {
        height: clientHeight,
        width: clientWidth,
        type,
        plat,
        tag,
        prefix,
        isMobile,
        isIOS,
        isPC,
        isMini,
        screen
    };
}
 
// 路径转数组
export function deepPaths(paths: string[], splitor?: string) {
    const list: any[] = [];
 
    paths.forEach(e => {
        const arr: string[] = e.split(splitor || '/').filter(Boolean);
 
        let c = list;
 
        arr.forEach((a, i) => {
            let d = c.find(e => e.label == a);
 
            if (!d) {
                d = {
                    label: a,
                    value: a,
                    children: arr[i + 1] ? [] : null
                };
 
                c.push(d);
            }
 
            if (d.children) {
                c = d.children;
            }
        });
    });
 
    return list;
}
 
// 列表转树形
export function deepTree(list: any[], sort?: 'desc' | 'asc'): any[] {
    const newList: any[] = [];
    const map: any = {};
 
    orderBy(list, 'orderNum', sort)
        .map(e => {
            map[e.id] = e;
            return e;
        })
        .forEach(e => {
            const parent = map[e.parentId];
 
            if (parent) {
                (parent.children || (parent.children = [])).push(e);
            } else {
                newList.push(e);
            }
        });
 
    return newList;
}
 
// 树形转列表
export function revDeepTree(list: any[]) {
    const arr: any[] = [];
    let id = 0;
 
    function deep(list: any[], parentId: number) {
        list.forEach(e => {
            if (!e.id) {
                e.id = ++id;
            }
 
            if (!e.parentId) {
                e.parentId = parentId;
            }
 
            arr.push(e);
 
            if (e.children && isArray(e.children) && e.id) {
                deep(e.children, e.id);
            }
        });
    }
 
    deep(list || [], 0);
 
    return arr;
}
 
// 路径转对象
export function path2Obj(list: any[]) {
    const data: any = {};
 
    list.forEach(({ path, value }) => {
        if (path) {
            const arr: string[] = path.split('/');
            const parents = arr.slice(0, arr.length - 1);
            const name = basename(path).replace('.ts', '');
 
            let curr = data;
 
            parents.forEach(k => {
                if (!curr[k]) {
                    curr[k] = {};
                }
 
                curr = curr[k];
            });
 
            curr[name] = value;
        }
    });
 
    return data;
}
 
// 是否是组件
export function isComponent(name: string) {
    return !isString(resolveComponent(name));
}
 
// 是否Promise
export function isPromise(val: any) {
    return val && Object.prototype.toString.call(val) === '[object Promise]';
}
 
// 单位转换
export function parsePx(val: string | number) {
    return isNumber(val) ? `${val}px` : val;
}
 
// 延迟
export function sleep(duration: number) {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve(true);
        }, duration);
    });
}
 
export { storage };
export * from './loading';