// get请求
export const getRequest = (url, data = {}) => {
wx.showLoading({ title: "加载中" });
let header = {
'content-type':
'application/x-www-form-urlencoded', // 默认值
}
if (wx.getStorageSync('token')) {
header.Authorization = 'Bearer '
+ wx.getStorageSync('token')
}
return new Promise((resolve, reject) => {
wx.request({
url, //仅为示例,并非真实的接口地址
data, //data为对象
method: "get",
header,
success(res) {
resolve(res.data) //成功回调
},
fail(err) {
reject(err) //失败回调
},
complier() {
wx.hideLoading() //关闭loading
}
})
})
}
//post请求
export function postRequest(url, data = {}) {
wx.showLoading({ title: "加载中" });
let header = {
'content-type':
'application/x-www-form-urlencoded', // 默认值
}
if (wx.getStorageSync('token')) {
header.Authorization = 'Bearer '
+ wx.getStorageSync('token')
}
return new Promise((resolve, reject) => {
wx.request({
url,
data,
method: "post",
header,
success(res) {
resolve(res.data)
},
fail(err) {
reject(err)
},
complier() {
wx.hideLoading() //关闭loading
}
})
})
}
微信小程序http请求封装
最新推荐文章于 2024-07-15 17:12:02 发布