const baseUrl = app.globalData.url; //服务器地址
const http = ({ url = '', param = {}, ...other } = {}) => {
wx.showLoading({
title: '请求中,请耐心等候..'
})
let timeStart = Date.now();
return new Promise((resolve, reject) => {
wx.request({
url: getUrl(url),
data: param,
header: {
'content-type': 'application/json',
Authorization: param.token ? 'Bearer ' + param.token : ""
},
...other,
complete: (res) => {
wx.hideLoading();
if (res.statusCode >= 200 && res.statusCode < 300) {
resolve(res.data)
} else {
reject(res)
}
}
})
})
}
const getUrl = (url) => {
if (url.indexOf('://') == -1) {
url = baseUrl + url
}
return url
}
// get方法
const _get = (url, param = {}) => {
return http({
url,
param
})
}
const _post = (url, param = {}) => {
return http({
url,
param,
method: "post"
})
}
module.exports = {
_get,
_post
}
页面调用时
const utils = require("../../utils/util.js");
utils._get(url, data).then(res => { //根据请求方式选择
//你的业务代码
})