wineList-MiniApp/miniprogram/api/request.ts
2023-04-28 18:04:49 +08:00

59 lines
1.6 KiB
TypeScript

// var baseUrl = "https://dfhd.guwengkj.com";
var baseUrl = "http://192.168.1.40:8083";
// const APPID = "wx3d38ce1103a82225";
// const SECRET = '3c41ca428b4d0f43cfaef6f567a1cc06';
const service = {
request:(options:any) => {
let {api,data={},method="POST",header='',callback} = options
var url=baseUrl+api
if(api.includes("http")){// 特殊接口
url=api
}
//获取token 缓存
var token= wx.getStorageSync('token')
if(token)token="Bearer "+token
if(!header){
header={
// 'content-type': 'application/x-www-form-urlencoded'
'content-type': 'application/json'
}
}
header["Authorization"]=token || 'Basic ZGV2OmRldjEyMw== '
return new Promise((resolve,reject)=>{
wx.request({
url: url,
data,
method,
header,
success (res:any) {
// console.log(res.data)
resolve(res.data)
},
fail(error:any){
wx.showToast({
title: error.errMsg,
icon: 'error',
duration: 2000
})
reject(error)
},
complete(res:any){
// console.log(res);
if(res.data.code == 401) { // token过期
wx.showToast({
title: res.data.message,
icon: 'error',
duration: 2000
})
wx.setStorageSync('token', '');//清空token
// wx.navigateTo({ // 跳转登录页
// url: '/pages/login/login',
// })
}
}
})
})
}
}
// export default service;
module.exports = service