69 lines
1.6 KiB
JavaScript
69 lines
1.6 KiB
JavaScript
const baseUrl = 'https://api-dev.beefast.co';
|
|
let app = getApp();
|
|
|
|
const sendRequest = (options)=>{
|
|
if(!app)app = getApp();
|
|
let timer;
|
|
if(options.delayLoading){
|
|
timer = setTimeout(()=>{
|
|
wx.showLoading({
|
|
title: '加载中...',
|
|
})
|
|
},800)
|
|
}
|
|
return new Promise((rs,rj)=>{
|
|
wx.request({
|
|
url: `${baseUrl}${options.url}`,
|
|
success:(result)=>{
|
|
if(timer){
|
|
clearTimeout(timer);
|
|
wx.hideLoading();
|
|
}
|
|
if(result.statusCode==200){
|
|
if(result.data.code==200){
|
|
rs(result.data.data);
|
|
}else{
|
|
wx.showToast({
|
|
icon:'error',
|
|
title: result.data.message,
|
|
});
|
|
rj(result.data);
|
|
}
|
|
}else if(result.statusCode==401){
|
|
wx.navigateTo({
|
|
url: '/pages/login/login',
|
|
})
|
|
}
|
|
},
|
|
|
|
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)=>{
|
|
wx.showToast({
|
|
title: 'Request Error',
|
|
})
|
|
rj(res);
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
export default {
|
|
baseUrl:baseUrl,
|
|
get(url,data,delayLoading){
|
|
return sendRequest({url,method:'get',data,delayLoading});
|
|
},
|
|
post(url,data){
|
|
return sendRequest({url,method:'post',data});
|
|
},
|
|
put(url,data){
|
|
return sendRequest({url,method:'put',data});
|
|
},
|
|
delete(url,data){
|
|
return sendRequest({url,method:'delete',data});
|
|
}
|
|
} |