前端拦截器
router.beforeEach((to, from, next) => {
//如果你访问的界面是login或者商家入驻统一都应该放行
if(to.path=="/login" || to.path=="/register"||to.path=="/ShopReg"){
next();
}else{
//获取token的值
var token = localStorage.getItem(“token”);
//如果token有值,证明是登录状态
if(token){
//就应该是放行状态
next();
}else{
//否则就应该跳转到登录界面
next({path: ‘/login’});
}
}
})
//前端拦截器
axios.interceptors.request.use(config=>{
//携带token
let token = localStorage.getItem(“token”);
if(token){
//添加请求头信息
config.headers[‘token’]=token;
}
return config;
},error => {
Promise.reject(error);
})
//axios后台拦截器实现后端已经退出登录的跳转登录页面========//
axios.interceptors.response.use(config=>{
console.log("========")
let data = config.data;
console.debug(data)
if(!data.success && “noLogin”=data.msg)
{
localStorage.removeItem(“token”);
localStorage.removeItem(“user”);
router.push({ path: ‘/login’ });
}
console.log("======")
return config;
},error => {
Promise.reject(error)
})