uniapp請求封裝

Vue.prototype.sendRequest = function(url,param,method,callBack){
    var _self = this, 
        // method = param.method,
        header = {},
        data = param|| {}, 
        token =uni.getStorageSync("access_token");
    //拼接完整請求地址
    var requestUrl = this.websiteUrl + url;

    var timestamp = Date.parse(new Date());//時間戳
    data["timestamp"] = timestamp;
    // #ifdef MP-WEIXIN
    data["device"] = "wxapp";
    data["ver"] = "1.1.30";
    // #endif
    // #ifdef APP-PLUS || H5
    data["device"] = "iosapp";
    data["ver"] = "1.0.0";
    data["access_token"]=token;
    // #endif
    //請求方式:GET或POST(POST需配置header: {'content-type' : "application/x-www-form-urlencoded"},)
    if(method){
        method = method.toUpperCase();//小寫改為大寫
        if(method=="POST"){
            header = {'content-type' : "application/x-www-form-urlencoded"};
        }else{
            header = {'content-type' : "application/json"};
        }
    }else{
        method = "GET";
        header = {'content-type' : "application/json"};
    }
    //網絡請求
    uni.request({
        url: requestUrl,
        data: data,
        method: method, 
        header: header,
        success: res => {
            if(res.data.code==200){
                callBack(res.data.data); 
            }else{
                _self.login(false);
            }
        },
        fail: (e) => {
            //console.log("網絡請求fail:" + JSON.stringify(e));
            uni.showModal({
                content:"" + e.errMsg
            });
            // typeof param.fail == "function" && param.fail(e.data);
        },
        complete: () => {
            //console.log("網絡請求complete");
            // uni.hideLoading();
            // typeof param.complete == "function" && param.complete();
            //return;
        }
    });
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容