54 lines
1.6 KiB
JavaScript
54 lines
1.6 KiB
JavaScript
axios.defaults.baseURL = BASE_PATH;
|
|
axios.defaults.headers.post['Content-Type'] = 'application/json';
|
|
axios.defaults.timeout = 120000;
|
|
|
|
var localVersion = "1.1";
|
|
document.addEventListener('plusready', function() {
|
|
plus.runtime.getProperty(plus.runtime.appid, function(inf) {
|
|
localVersion = inf.version;
|
|
});
|
|
}, false);
|
|
|
|
// 添加请求拦截器
|
|
axios.interceptors.request.use(function (config) {
|
|
// 对所有POST请加密,必须是json数据提交,不支持表单,可通过后台控制是否要开启加密
|
|
if (config.method == "post" && localStorage.getItem("encrypt") == "true") {
|
|
config.data = EncryptData(JSON.stringify(config.data));
|
|
}
|
|
var token = localStorage.getItem("user_token");
|
|
config.headers.Authorization = token;
|
|
config.headers.appVersion = localVersion;
|
|
return config;
|
|
}, function (error) {
|
|
return Promise.reject(error);
|
|
});
|
|
|
|
// 添加响应拦截器
|
|
axios.interceptors.response.use(function (response) {
|
|
try {
|
|
plus.nativeUI.closeWaiting();
|
|
}catch(e){
|
|
|
|
}
|
|
// 返回字符串表示需要解密操作
|
|
if(typeof(response.data) == "string"){
|
|
response.data = DecryptData(response.data);
|
|
}
|
|
return response;
|
|
}, function (error) {
|
|
try {
|
|
|
|
if(error=="Error: Network Error" || error.toString().indexOf('timeout') > -1){
|
|
if(plus.webview.getWebviewById('error.html')==null){
|
|
clicked('error.html',true,false,{},{flag:1},function(){
|
|
window.location.reload();
|
|
})
|
|
}else{
|
|
$(".loadingBg").hide();
|
|
plus.nativeUI.closeWaiting();
|
|
plus.webview.getWebviewById("error.html").show();
|
|
}
|
|
}
|
|
}catch(e){}
|
|
return Promise.reject(error);
|
|
}); |