微信小程序-axios封装
2020-09-01 本文已影响0人
刘昊然的弟弟
封装成promise函数
const baseUrl = 'http://192.111.1.118:8801'
const axios = ({ url, method = 'GET', data = {}, tipName = '加载中' }) => {
const header = {}
if (wx.getStorageSync('token')) {
header.token = wx.getStorageSync('token') //登录后拿到token值
}
return new Promise((resolve, reject) => {
wx.showLoading({
title: tipName,
})
wx.request({
url:url.substring(0,1)==='/'?`${baseUrl}${url}`:`${url}`, // 兼容baseUrl不同的情况,可以通过填写完整路径
method,
data,
header,
success: res => {
resolve(res)
},
fail: err => {
reject(err)
},
complete:()=>{
wx.hideLoading()
}
})
})
}
export default axios