微信小程序请求封装
//封装request请求
unityRequest:function (opts) {
wx.showLoading({
mask: true
})
var appThat=this;
wx.request({
url: opts.url,
data:opts.data,
header: opts.header,
method:opts.method,
dataType:opts.dataType,
fail:opts.fail,
complete:opts.complete,
success: function(res) {
wx.hideLoading()
if (res.data.status == "success") {
opts.success(res)
}else if(res.data.status == 'error'){
wx.showModal({
title:"提示",
content: res.data.msg?res.data.msg:'网络错误',
showCancel:false
});
}else {
console.log(opts.url)
wx.showModal({
title: "提示",
content: '加载数据失败、请检查网络、或者重启试试',
showCancel: false
});
}
},
fail(){
wx.hideLoading()
wx.showModal({
title: "提示",
content: '加载数据失败、请检查网络、或者重启试试',
showCancel: false
});
}
})
},
调用:
引入或者写在app全局中也可以;
app.unityRequest({
url: “XXXX”,
method: 'POST',
header: {
timestamp: headerdata.timestamp,
sign: headerdata.sign,
token: token
},
success(res) {
wx.showToast({ title: '领取成功', icon: 'success', duration: 2000 })
that.hideModal()
}
})
状态可根据自己项目需求更改;
直接在需要的文件中引入调用就可以啦!