zrg
4 天以前 fb0d024283a24042e55c70019dffbcd104792135
utils/common.js
@@ -211,6 +211,7 @@
            method: method || "GET",
            url: this.serverUrl + url,
            data: data || "",
            // header: this.getHeader(),
            success: (res) => {
                if (typeof resFunction === 'function') {
                    resFunction.call(that, res)
@@ -266,6 +267,7 @@
            method: method || "GET",
            url: this.serverUrl + url,
            data: data || "",
            // header: this.getHeader(),
            success: (res) => {
                if (typeof resFunction === 'function') {
                    resFunction.call(that, res)
@@ -311,11 +313,10 @@
        data,
        method,
    }) {
        if(this.requestLock){
            console.warn("该请求被锁定,不能重复请求!!!")
        if (this.requestLock) {
            console.log("该请求被锁定,不能重复请求!!!")
            return
        }
        this.requestLock = true
        return new Promise((resolve, reject) => {
            // that = that || this;
@@ -327,6 +328,7 @@
                method: method || "GET",
                url: this.serverUrl + url,
                data: data || "",
                // header: this.getHeader(),
                success: (res) => {
                    resolve(res)
                },
@@ -341,6 +343,42 @@
            })
        })
    }
    //没有添加锁的异步查询
    async doRequest2Async({
        url,
        data,
        method,
        showLoading = true,
    }) {
        return new Promise((resolve, reject) => {
            // that = that || this;
            let errorTip = null;
            if (showLoading) {
                uni.showLoading({
                    title: '加载中...'
                })
            }
            uni.request({
                method: method || "GET",
                url: this.serverUrl + url,
                data: data || "",
                // header: this.getHeader(),
                success: (res) => {
                    resolve(res)
                },
                fail: (err) => {
                    reject(err)
                },
                complete: () => {
                    if (showLoading) {
                        uni.hideLoading()
                    }
                }
            })
        })
    }
    stringToBoolean(str) {
        // 忽略大小写的转换
        return str?.toLowerCase() === "true";
@@ -364,9 +402,10 @@
            console.log(res.errCode);
        });
        innerAudioContext.onPause(function() {
            console.log('播放出现错误,销毁');
            console.log('播放暂停,销毁');
            innerAudioContext.destroy();
        })
        });
    }
    // playSound(e) {
    //     // 全局维护一个音频实例,防止缓存溢出
@@ -400,6 +439,29 @@
    // }
    showTips({
        type,
        message,
        title,
        duration
    }) {
        if (!message) {
            return
        }
        if (message.length < 20) {
            return uni.showToast({
                icon: type || 'none',
                title: message
            })
        }
        return uni.showModal({
            title: title,
            content: message,
            showCancel: false
        })
    }
    replaceWithFunction(str, handler) {
        return str.replace(/\{(.+?)\}/g, (match, key) => {
@@ -407,6 +469,76 @@
            return handler(key, match);
        });
    }
    fieldListFilterRole({
        FieldList,
        ExcludeKeys = [],
        RoleList = null
    }) {
        if (!RoleList) {
            RoleList = [
                /^[a-zA-Z]+$/,
                /id$/i
            ]
        }
        if (!Array.isArray(RoleList)) {
            return {
                status: false,
                data: null,
                Message: "过滤字段列表失败,规则必须是数组。"
            }
        }
        let FieldListCache = Array(...FieldList)
        RoleList.forEach(role => {
            FieldListCache = FieldListCache.filter(elem => !role.test(elem.ColmCols))
        })
        FieldListCache = FieldListCache.filter(elem => !ExcludeKeys.includes(elem.ColmCols))
        return {
            status: true,
            data: FieldListCache,
            Message: ""
        }
    }
    emptyValueFilter(item, fieldList) {
        return fieldList.filter(e => {
            return item[e.ColmCols]
        })
    }
    httpFormatWs(httpUrl = this.serverUrl) {
        if (httpUrl.indexOf("http://") === 0) {
            httpUrl = httpUrl.replace("http://", "")
            httpUrl = httpUrl.substring(0, httpUrl.length - 1)
            let BackSlashLast = httpUrl.lastIndexOf('/')
            httpUrl = httpUrl.substring(0, BackSlashLast)
            httpUrl = "ws://" + httpUrl.split(":")[0] + ":12345/ws"
        } else if (httpUrl.indexOf("https://") === 0) {
            httpUrl = httpUrl.replace("https://", "")
            httpUrl = httpUrl.substring(0, httpUrl.length - 1)
            let BackSlashLast = httpUrl.lastIndexOf('/')
            httpUrl = httpUrl.substring(0, BackSlashLast)
            httpUrl = "wss://" + httpUrl.split(":")[0] + ":12345/ws"
        } else {
            // 提示传入连接错误
        }
        return httpUrl
    }
    getHeader() {
        let header = {}
        let Token = uni.getStorageSync("Token")
        if (Token) {
            header['Authorization'] = `Bearer ${uni.getStorageSync("Token")}`
        }
        return header
    }
}
export const CommonUtils = new commonUtils()