简单封装下请求方法,能摆脱长篇大论的wx.request写法,就不用一直要重复写header传token
先创个js文件,common.js
const REQUEST = function (_url, _data, _getWay, _cb){
wx.showLoading()
wx.request({
url: _url,
data: _data || '',
method: _getWay,
header: {
'content-type': 'application/json',
'token': app.globalData.token,
},
success: function (res){
wx.hideLoading()
return typeof _cb === 'function' && _cb(res.data);
},
fail: function (err){
wx.hideLoading()
return typeof _cb === 'function' && _cb(false);
}
})
}
module.exports = {
REQUEST: REQUEST,
}
然后到需要的地方引入
const common = require('../../utils/common');
const api = require('../../utils/api');
let url = api.HOUSEINTERFACES.getUserOrderList,
data = {
pageNum: 1,
pageSize: self.data.rows,
status: ''
};
common.REQUEST(url, data, 'get', function (res) {
console.log(res)
})
方便的话是这个方便,但是要提升写的速度的话其实两者没太大的差距,毕竟wx.request能直接wx.req回车就有模板。
对了,如果有好奇api.HOUSEINTERFACES.getUserOrderList的url封装写法的可以看这篇url封装写法