post: function(params){
var _this = this;
if (plus.networkinfo.getCurrentType() == plus.networkinfo.CONNECTION_NONE) {
mui.confirm('网络异常,请检查网络设置!','提示',['确认'],function(e){})
return
}
var token = this.getToken();

mui.ajax(params.api,{
data: params.data || {},
type: 'post',
headers:{'Authorization': 'Bearer ' + token},
success: function(res){
_this.handleRes(params,res);
},
error: function(xhr,type,errorThrown){
plus.nativeUI.toast('请求失败')
console.log(JSON.stringify(xhr))
console.log(JSON.stringify(type))
console.log(JSON.stringify(errorThrown))
if(xhr.status != 0){
plus.nativeUI.toast('系统繁忙,请稍后再试。'+ xhr.status +';')
}
}
})
},
get: function(params){
if (plus.networkinfo.getCurrentType() == plus.networkinfo.CONNECTION_NONE) {
mui.confirm('网络异常,请检查网络设置!','提示',['确认'],function(e){})
return
}
var _this = this;
var token = this.getToken();
// console.log(token)
mui.ajax(params.api,{
data: params.data || {},
type:'get',
headers:{'Authorization': 'Bearer ' + token},
success: function(res){
_this.handleRes(params,res);
},
error: function(xhr,type,errorThrown){
console.log(JSON.stringify(xhr))
console.log(JSON.stringify(type))
console.log(JSON.stringify(errorThrown))
if(xhr.status!=0){
plus.nativeUI.toast('系统繁忙,请稍后再试。'+xhr.status+';')
}
}
})
},
getToken: function(){
var t = plus.storage.getItem('token');
return t;
},
handleRes: function(params,res){
// console.log(JSON.stringify(res))
plus.nativeUI.closeWaiting();
if(typeof res == 'string' ){
console.log("处理完成")
res = JSON.parse(res)
}
if(res.code == '10000'){
params.success && params.success(res)
} else {
if(res.code == '20001'){
console.log("授权失效或未授权")
plus.nativeUI.toast('授权失效或未授权,请重新登录')
console.log(plus.storage.removeItem('token'))
plus.storage.removeItem('token');
plus.storage.removeItem('userInfo');
setTimeout(function(){
plus.runtime.restart();
},2000)
}else{
params.error && params.error(res);
}
}
},

posted on 2022-05-14 00:51  rider_yang  阅读(170)  评论(0)    收藏  举报