1. 程式人生 > >封裝小程序網絡請求

封裝小程序網絡請求

1.0 ase eject 簡單 參數 tor 新的 接口 true

/*
    1.對小程序wx.require的封裝
*/
class Ajax{
    constructor(dev=true){
        this.base = dev ? ‘https://生產環境‘ : ‘https://開發環境‘;
        this.token = "";
        this.sv = "1.0.0";
    }

    // 參數處理
    handleData(data){
        data.token = this.token;
        if(this.sv){
            data.sv = this.sv;
        }
    }

    
// promise封裝 ajax(url,data={},type=‘get‘){ this.handleData(data); return new Promise((resolve,reject)=>{ wx.request({ url:this.base + url, data:data, method:type, success:res =>{ resolve(res);
/* 1.埋個頁面,方便審核 2.當提交審核時,讓後臺把所有的接口都返回10007狀態 */ if (res.data.errno == 10007) { wx.reLaunch({ url: ‘/pages/index/index‘, //簡單的頁面,應對審核 }); } }, fail:rej
=>{ reject(rej); } }) }) } }

經歷幾周的開發,小程序終於上線啦。
在開發過程中遇見了很多的問題,解決了問題,學到了一些新的思路。
下班前,記錄一波一點收獲

封裝小程序網絡請求