uniapp封装中断请求

    requestTree:{},  
    request(options={}){//统一约定,请求调用此方法
        var url = options.url;  
        var method = options.method;  
        var header = options.header || {};  
        var data = options.data || {};  
        //请求方式  
        if(method){  
            method = method.toUpperCase();  
            if(method === "POST" && JSON.stringify(header) == "{}"){  
                header = {"content-type":"application/x-www-form-urlencoded"}  
            }  
        }  
        // 发送请求 加载动画  
        if(!options.hideLoading){  
            uni.showLoading({  
                title:"加载中"  
            })  
        }  

        this.requestTree[options.url]=uni.request({  
            url : url,  
            method : method || "GET",  
            header : header,  
            data : data,  
            success : res =>{  
                if(res.statusCode && res.statusCode !== 200){  
                    uni.showModal({  
                        content:res.data.message  
                    })  
                    return;  
                }  
                typeof options.success == "function" && options.success(res.data);  
            },  
            fail: (e) => { 
                if(e.errMsg === "request:fail abort") return;
                typeof options.fail == "function" && options.fail(e.data);  
            },  
            complete: (e) => {  
                uni.hideLoading();  
                typeof options.complete == "function" && options.complete(e.data);  
                return;  
            }  
        });  
    },  

    abortRequest(options={}){//此方法会打断所有发送中请求 
        for(let name in this.requestTree){  
            console.log(name);  
            this.requestTree[name].abort();  
        }  
    }  

};

你可能感兴趣的:(uniapp封装中断请求)