import axios from 'axios';
import router from '../router'
axios.interceptors.request.use(
config => {
if (localStorage.getItem("token")) { //统一将token带上
config.headers.Authorization = localStorage.getItem("myToken");
}else{
router.replace({
path: '/'
})
}
return config;
},
err => {
return Promise.reject(err);
}
);
axios.interceptors.response.use(response => {
if(response.data.code === '9009'){ //过期
router.replace({
path: '/'
})
}
}else{
return response; //只要不是系统级别的错误,将错误抛到页面处理
}
},err => {
switch (err.response.status) {
case 400:
break
case 404:
err.message = '不存在'
break
default:
}
//在这写全局处理
/////////////////////////////////////////////
/////////////////////////////////////////////
/////////////////////////////////////////////
/////////////////////////////////////////////
//在这写全局处理
return Promise.reject(err);
});
export default axios;
=========================引用==========================
import axios from './axios';
export function getInfo() {
return axios ({
url: '/m/getInfo',
method: 'get'
});
}
export function getInfo() {
return axios ({
url: '/m/getInfo',
method: 'get'
});
}
=========================引用==========================