请求封装request.js
var baseUrl = 'https://****';
var sendRrquest = function ({...params}) {
var promise = new Promise(function (resolve, reject) {
wx.request({
url: baseUrl + params.url,
data: params.data,
method: params.method ? params.method :'GET',
header:{
'content-type': 'application/json',
"content-type": "application/x-www-form-urlencoded",
},
success: (res) => {
resolve(res.data);
if (res.data.errcode == '-1000') {
wx.showToast({
title:res.data.errmsg,
icon: 'none',
duration: 2000
})
}
},
fail: reject,
})
});
return promise;
};
module.exports.sendRrquest = sendRrquest
页面使用:
var request = require('../../utils/requestService.js')
代码如下:
onLoad: function () {
this.getCard();
},
getCard() {
request.sendRrquest({
url: '/member/user_card',
data:{
idstr:this.data.idstr//需要传的参数
}
}).then(res => {
if (res.errcode == 1000) {//成功执行的方法
this.setData({
gcardList: res.data.user_info,
})
}
})
},
如图所示: