You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
2.0 KiB
59 lines
2.0 KiB
import axios from 'axios'; |
|
import store from '@/vuex/store.js'; |
|
|
|
//设置请求超时时间(10s) |
|
axios.defaults.timeout = 10000; |
|
axios.interceptors.request.use( |
|
function(config){ |
|
// return config; |
|
// } |
|
// config.url = process.env.BASEAPI+process.env.ROOT+config.url; |
|
if(sessionStorage.getItem('token') && !config.headers.requestType){ |
|
// if(!config.params){ |
|
// config.params = {}; |
|
// } |
|
config.headers['X-Requested-Token'] = sessionStorage.getItem('token'); |
|
return config; |
|
}else if(config.headers.requestType==="noToken"){ |
|
return config; |
|
} |
|
else{ |
|
// window.location.href = window.location.href.split("#")[0]+'#/login'; |
|
// return Promise.reject({message:'token无效,重新登陆'}); |
|
return config; |
|
} |
|
},function(error){ |
|
return Promise.reject(error); |
|
} |
|
); |
|
|
|
axios.interceptors.response.use( |
|
function(response){ |
|
let location = window.location.href.split("#")[0]+'#/login'; |
|
let code = response.data.code; |
|
if(parseInt(code) === 101||parseInt(code) === 10){//token无效 |
|
// store.commit("SAVETOKEN",null); |
|
sessionStorage.removeItem('token'); |
|
window.location.href = location;//刷新页面 |
|
Promise.reject(response); |
|
} |
|
return response; |
|
},function(error){ |
|
if(error.response){ |
|
switch(error.response.status){//404,网络错误,地址有问题。 |
|
case 404: |
|
alert("网络错误!"); |
|
break; |
|
} |
|
}else{ |
|
let errorMsg = error.message; |
|
if(error.message.indexOf("timeout")>-1){ |
|
errorMsg = "请求超时"; |
|
} |
|
return Promise.resolve({//在报错的时候传回一个正常的promise对象,错误可以交到正常处理 |
|
data:{code:1,msg:error.message,extmsg:errorMsg} |
|
}); |
|
} |
|
|
|
} |
|
); |