wineList-MiniApp/miniprogram/api/request.ts
2023-05-15 14:30:16 +08:00

60 lines
1.6 KiB
TypeScript

// var baseUrl = "https://wine.api.echol.cn";
var baseUrl = "http://192.168.1.40:8083";
// const APPID = "wx29f3e0dc2f7f2c54";
// const SECRET = '95a893451cf25b696612a7d1735a04b8';
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'
}
}
// let auth = base64encode("admin:admin")
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