| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 | class HTTP {    constructor({        baseUrl    }) {        this.baseUrl = baseUrl;        this.hangUp = [];        this.intercept = false;        this.logInAgain = null;    }    request({        url,        data = {},        method = "POST",        header = {            'content-type': 'application/json'        },        showLoading = ''    }) {        return new Promise((resolve, reject) => {            if (this.intercept && url != '/index/loginbywechat') {                this.hangUp.push({                    url,                    resolve,                    reject,                    data,                    method,                    header,                    showLoading                })            } else {                this._request(url, resolve, reject, data, method, header, showLoading);            }        })    }    _request(url, resolve, reject, data, method, header, showLoading) {        if (showLoading) uni.showLoading({            title: showLoading,            mask: true        })        uni.request({            url: this.baseUrl + '/yos/rest' + url,            data: data,            method: method,            header: header,            timeout: 60000,            success: res => {                if (res.data.msg == '登陆状态已过期,请重新登陆!') {                    this.intercept = true;                    this.hangUp.push({                        url,                        resolve,                        reject,                        data,                        method,                        header,                        showLoading                    })                    if (this.logInAgain == null) this.wechatLogin().then(token => {                        Promise.all(this.hangUp.map(v => {                            v.data.accesstoken = token;                            v.data.date = Date.now()                            return this._request(v.url, v.resolve, v.reject, v.data, v.method, v.header, v.showLoading);                        })).then(res => {                            console.log("重新获取数据")                            this.hangUp = []                            this.intercept = false;                            this.logInAgain = null;                        })                    })                } else {                    resolve(res.data)                }            },            fial: err => reject(err),            complete: (res) => {                if (showLoading) uni.hideLoading()                if (res.errMsg != 'request:ok') uni.showToast({                    title: '网络异常,请稍后再试',                    icon: "none"                })            }        })    }}export {    HTTP}
 |