yusijie
3 天以前 21c2a7c125a5d07ad87fd7a266a697f61d0fdefd
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)
@@ -326,6 +328,7 @@
                method: method || "GET",
                url: this.serverUrl + url,
                data: data || "",
                // header: this.getHeader(),
                success: (res) => {
                    resolve(res)
                },
@@ -340,22 +343,27 @@
            })
        })
    }
   //没有添加锁的异步查询
    //没有添加锁的异步查询
    async doRequest2Async({
        url,
        data,
        method,
        showLoading = true,
    }) {
        return new Promise((resolve, reject) => {
            // that = that || this;
            let errorTip = null;
            uni.showLoading({
                title: '加载中...'
            })
            if (showLoading) {
                uni.showLoading({
                    title: '加载中...'
                })
            }
            uni.request({
                method: method || "GET",
                url: this.serverUrl + url,
                data: data || "",
                // header: this.getHeader(),
                success: (res) => {
                    resolve(res)
                },
@@ -363,7 +371,9 @@
                    reject(err)
                },
                complete: () => {
                    uni.hideLoading()
                    if (showLoading) {
                        uni.hideLoading()
                    }
                }
            })
        })
@@ -395,7 +405,7 @@
            console.log('播放暂停,销毁');
            innerAudioContext.destroy();
        });
    }
    // playSound(e) {
    //     // 全局维护一个音频实例,防止缓存溢出
@@ -459,28 +469,32 @@
            return handler(key, match);
        });
    }
    fieldListFilterRole({FieldList, ExcludeKeys = [] ,RoleList = null}) {
        if(!RoleList) {
    fieldListFilterRole({
        FieldList,
        ExcludeKeys = [],
        RoleList = null
    }) {
        if (!RoleList) {
            RoleList = [
                /^[a-zA-Z]+$/,
                /id$/i
            ]
        }
        if(!Array.isArray(RoleList)){
        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,
@@ -488,34 +502,43 @@
            Message: ""
        }
    }
    emptyValueFilter(item, fieldList){
    emptyValueFilter(item, fieldList) {
        return fieldList.filter(e => {
            return item[e.ColmCols]
        })
    }
    httpFormatWs(httpUrl=this.serverUrl) {
        if(httpUrl.indexOf("http://") === 0){
    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 = "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{
            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()