chenhaozhe
2025-07-10 12793c4e8348acfe80f491ac5d3019d77f1df275
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
class commonUtils {
    serverUrl
 
    constructor() {
        this.serverUrl = uni.getStorageSync('serverUrl') || 'http://47.96.97.237/API';
    }
 
    // 防抖函数
    debounce(func, delay, immediate = false) {
        let timer = null;
        return function() {
            const context = this;
            const args = arguments;
 
            const callNow = immediate && !timer;
            // 如果定时器已经存在,清除它
            if (timer) {
                clearTimeout(timer);
            }
 
            // 设置一个新的定时器
            timer = setTimeout(() => {
                if (!immediate) {
                    func.apply(context, args);
                }
                timer = null;
            }, delay);
 
            // 配置立即执行
            if (callNow == true) {
                func.apply(context, args)
            }
        };
    }
 
    // 判空函数
    isEmpty(value, zeroIsEmpty = false, falseIsEmpty = false) {
        let val = value
        // 检查是否为null  或者 undefind
        if (val === null || val === undefined) {
            return true;
        }
        //如果字符串全部是由数字构成的,则转化为数字型
        if (this.isAllDigits(val) === true) {
            val = Number(val)
        }
 
        // 是否是字符串类型
        if (typeof val === 'string') {
            return val.trim().length === 0;
        }
 
        // 是否是数组
        if (Array.isArray(val)) {
            return val.length === 0;
        }
 
        //是否是对象
        if (typeof val === 'object') {
            return Object.keys(val).length === 0;
        }
 
        // 数字类型默认不为空 
        if (typeof val === 'number') {
            // 数字为0视为空
            if (zeroIsEmpty == true) {
                if (val === 0) {
                    return true
                }
            }
            return false
        }
 
        // 布尔类型默认不为空
        if (typeof val === 'boolean') {
            // false值视为空
            if (falseIsEmpty == true) {
                if (val === 0) {
                    return true
                }
            }
            return false;
        }
    }
 
    // 判断是否全是数字
    isAllDigits(str) {
        return /^\d+$/.test(str);
    }
 
    // uni-app 使用 封装请求函数 使用传统函数当作回调需要传that,箭头函数不需要
    doRequest(url, data, resFunction, errFunction, method, that) {
        that = that || this;
        let errorTip = null;
        uni.showLoading({
            title: '加载中...'
        })
        uni.request({
            method: method || "GET",
            url: this.serverUrl + url,
            data: data || "",
            success: (res) => {
                if (typeof resFunction === 'function') {
                    resFunction.call(that, res)
                } else if (typeof errFunction === 'undefined' || errFunction === null) {
                    return
                } else {
                    throw new TypeError("访问成功回调函数类型不为函数或者空!")
                }
            },
            fail: (err) => {
                console.error(err)
                errorTip = () => {
                    uni.showToast({
                        icon: "none",
                        title: err.errMsg || err.data.message || "接口异常!",
                        duration: 2000
                    })
                }
                if (typeof errFunction === 'function') {
                    errFunction.call(that, err)
                } else if (typeof errFunction === 'undefined' || errFunction === null) {
                    return
                } else {
                    throw new TypeError("访问失败回调函数类型不为函数或者空!")
                }
                
            },complete() {
                setTimeout(() => {
                    uni.hideLoading()
                    if (errorTip != null) {
                        errorTip()
                    }
                }, 1000)
            }
        })
        
    }
}
 
export const CommonUtils = Object.freeze(new commonUtils());