93 lines
2.4 KiB
JavaScript
93 lines
2.4 KiB
JavaScript
const { miniProgram } = wx.getAccountInfoSync();
|
|
const envVersion = miniProgram.envVersion;
|
|
let baseUrl = '';
|
|
if(envVersion=='develop'){
|
|
baseUrl = 'https://api-dev.beefast.co';
|
|
}else{
|
|
baseUrl = 'https://api.beefast.co'
|
|
}
|
|
let app = getApp();
|
|
|
|
const sendRequest = (options)=>{
|
|
if(!app)app = getApp();
|
|
if(options.method!='get'){
|
|
wx.showLoading({
|
|
title: '请等待...',
|
|
})
|
|
}
|
|
return new Promise((rs,rj)=>{
|
|
wx.request({
|
|
url: `${baseUrl}${options.url}`,
|
|
success:(result)=>{
|
|
if(options.method!='get'){
|
|
wx.hideLoading();
|
|
}
|
|
if(result.statusCode==200){
|
|
if(result.data.code==200){
|
|
rs(result.data.data);
|
|
}else{
|
|
|
|
if(!options.options.noTips){
|
|
wx.showToast({
|
|
icon:'error',
|
|
title: result.data.message||'发生错误',
|
|
});
|
|
}
|
|
rj(result.data);
|
|
}
|
|
}else if(result.statusCode==401){
|
|
if(!app)app = getApp();
|
|
app.logout();
|
|
// const pages = getCurrentPages();
|
|
// const currentPages = pages[pages.length-1];
|
|
// if(currentPages&¤tPages.route.indexOf('pages/login/login')>-1){
|
|
// return;
|
|
// }
|
|
// wx.navigateTo({
|
|
// url: '/pages/login/index',
|
|
// })
|
|
}else{
|
|
rj(result.data);
|
|
if(!options.options.noTips){
|
|
wx.showToast({
|
|
icon:'error',
|
|
title: result.data.message||'发生错误',
|
|
});
|
|
}
|
|
}
|
|
},
|
|
|
|
method:options.method,
|
|
data:options.data,
|
|
header:{
|
|
Authorization: `Bearer ${app.globalData.accessToken}`,
|
|
"content-type":options.data&&options.data.file?'application/x-www-form-urlencoded':'application/json'
|
|
},
|
|
fail:(res)=>{
|
|
if(options.method!='get'){
|
|
wx.hideLoading();
|
|
}
|
|
wx.showToast({
|
|
title: 'Request Error',
|
|
})
|
|
rj(res);
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
export default {
|
|
baseUrl:baseUrl,
|
|
get(url,data,options){
|
|
return sendRequest({url,method:'get',data,options:options||{}});
|
|
},
|
|
post(url,data,options){
|
|
return sendRequest({url,method:'post',data,options:options||{}});
|
|
},
|
|
put(url,data,options){
|
|
return sendRequest({url,method:'put',data,options:options||{}});
|
|
},
|
|
delete(url,data,options){
|
|
return sendRequest({url,method:'delete',data,options:options||{}});
|
|
}
|
|
} |