微信小程序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 => {
      //你要执行的代码
    })
  }

猜你喜欢

转载自www.cnblogs.com/mei1234/p/9965232.html