1.mpvue 封装request
//定义请求地址
const host = ‘’;
function request(url, method, data, header = {}) {
wx.showLoading({
title: ‘正在加载中…’
})
return new Promise((resolve, reject) => {
wx.request({
url: host + url,
method: method,
data: data,
headers: {
// ‘content-type’: ‘application/json’ // 默认转为json格式
‘content-type’: ‘application/x-www-form-urlencoded;charset=utf-8’
},
success: function (res) {
wx.hideLoading();
if (res.code != 200) {
wx.showToast({
title: “网络出错,稍后再试”,
icon: “none”
});
return false;
}
resolve(res.data)
},
fail: function (error) {
wx.hideLoading();
reject(false)
},
complete: function () {
wx.hideLoading();
}
})
})
}
function get(obj) {
return request(obj.url, ‘GET’, obj.data)
}
function post(obj) {
return request(obj.url, ‘POST’, obj.data)
}
export default {
request,
get,
post
}