微信小程序api封裝(promise)


順帶這是我平時公司切換改變網絡環境

直接上代碼,我相信就可以懂了,

//app.js
function fetchApi(url, type, params, method) {
    return new Promise((resolve, reject) => {
        wx.request({
            url: `${url}/${type}`,
            data: params,
            method: method,
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: resolve,
            fail: reject
        })
    })
}

module.exports = {
    // 登錄
    GetLogin: function(params) {
        return fetchApi(url1, 'wx/login.html', params, 'POST')
            .then(res => res.data)
    },
}


// login.js
  var api = require('../../utils/api.js'); //引入api.js頁面
  
  nextClick: function() {
    var params = {
      dataFrom: xxx,
      code: xxx,
      identity: xxx,
      username: xxx,
      password: xxx,
    }
    api.GetLogin(params)
    .then(res => {
      //你要執行的代碼
    })
  }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM