微信小程序封装
import __config from '后端地址'
const request = (url, method, data, showLoading) => {
let _url = __config.basePath + url
return new Promise((resolve, reject) => {
if (showLoading) {
wx.showLoading({
title: '加载中',
})
}
wx.request({
url: _url,
method: method,
data: data,
header: {
'app-id': wx.getAccountInfoSync().miniProgram.appId,
'third-session': getApp().globalData.thirdSession != null ? getApp().globalData.thirdSession : ''
},
success(res) {
if (res.statusCode == 200) {
if (res.data.code != 200) {
console.log(res.data)
wx.showModal({
title: '提示',
content: res.data.msg ? res.data.msg : '没有数据' + '',
success() {
},
complete() {
if (res.data.code == 60001) {
//session过期,则清除过期session,并重新加载当前页
getApp().globalData.thirdSession = null
wx.reLaunch({
url: getApp().getCurrentPageUrlWithArgs()
})
}
}
})
reject(res.data.msg)
}
resolve(res.data)
} else if (res.statusCode == 404) {
wx.showModal({
title: '提示',
content: '接口请求出错,请检查手机网络',
success(res) {
}
}