feat: base 完善无感刷新
This commit is contained in:
parent
ebf6e48d65
commit
052821526e
|
|
@ -15,7 +15,7 @@ let requests = [];
|
|||
// HTTP request 拦截器
|
||||
axios.interceptors.request.use(
|
||||
(config) => {
|
||||
let token = tool.cookie.get("TOKEN")?tool.cookie.get("TOKEN"):tool.data.get("TOKEN");
|
||||
let token = tool.data.get("TOKEN");
|
||||
if(token){
|
||||
config.headers[sysConfig.TOKEN_NAME] = sysConfig.TOKEN_PREFIX + token
|
||||
}
|
||||
|
|
@ -47,49 +47,41 @@ axios.interceptors.response.use(
|
|||
title: '请求失败',
|
||||
message: "账户密码错误或已禁用"
|
||||
});
|
||||
// if (!isRefreshing) {
|
||||
// isRefreshing = true;
|
||||
// axios({
|
||||
// method: 'post',
|
||||
// url: sysConfig.API_URL+'/auth/token/refresh/',
|
||||
// headers: {
|
||||
// Authorization: `Bearer ${tool.cookie.get("TOKEN")}`,
|
||||
// },
|
||||
// data:{refresh:tool.cookie.get("TOKEN_REFRESH")}
|
||||
// }).then(res=>{
|
||||
// debugger;
|
||||
// console.log(res.data.access);
|
||||
// // 替换本地的token
|
||||
// tool.cookie.set("TOKEN", res.data.access);
|
||||
// tool.data.set("TOKEN_TIME", new Date().getTime());
|
||||
// error.config.headers[sysConfig.TOKEN_NAME] = sysConfig.TOKEN_PREFIX + res.data.access;
|
||||
// isRefreshing = false;
|
||||
// requests.forEach(cb => cb())
|
||||
// //发起后将requests置空
|
||||
// requests=[];
|
||||
// return axios(error.config)
|
||||
// }).catch(()=>{
|
||||
// // refresh_token 有值 , 但是过期了, 请求失败
|
||||
// router.push('/login')
|
||||
// })
|
||||
|
||||
// }else{
|
||||
// //如果正在刷新token,将发起的请求暂存在requests中
|
||||
// return new Promise((resolve)=>{
|
||||
// requests.push(()=>{
|
||||
// resolve(axios(error.config));
|
||||
// })
|
||||
// })
|
||||
// }
|
||||
}else{
|
||||
// ElMessageBox.confirm('当前用户已被登出或无权限访问当前资源,请尝试重新登录后再操作。', '无权限访问', {
|
||||
// type: 'error',
|
||||
// closeOnClickModal: false,
|
||||
// center: true,
|
||||
// confirmButtonText: '重新登录'
|
||||
// }).then(() => {
|
||||
router.replace({path: '/login'});
|
||||
// }).catch(() => {})
|
||||
if (error.config.url.indexOf('auth/token/refresh/') != -1) {
|
||||
isRefreshing = false;
|
||||
requests=[];
|
||||
router.push('/login');
|
||||
return
|
||||
}
|
||||
if (!isRefreshing) {
|
||||
isRefreshing = true;
|
||||
axios({
|
||||
method: 'post',
|
||||
url: sysConfig.API_URL+'/auth/token/refresh/',
|
||||
headers: {
|
||||
Authorization: `Bearer ${tool.data.get("TOKEN")}`,
|
||||
},
|
||||
data:{refresh:tool.data.get("TOKEN_REFRESH")}
|
||||
}).then(res=>{
|
||||
// 替换本地的token
|
||||
tool.data.set("TOKEN", res.data.access);
|
||||
tool.data.set("TOKEN_TIME", new Date().getTime());
|
||||
error.config.headers[sysConfig.TOKEN_NAME] = sysConfig.TOKEN_PREFIX + res.data.access;
|
||||
isRefreshing = false;
|
||||
requests.forEach(cb => cb())
|
||||
//发起后将requests置空
|
||||
requests=[];
|
||||
return axios(error.config)
|
||||
})
|
||||
}else{
|
||||
//如果正在刷新token,将发起的请求暂存在requests中
|
||||
return new Promise((resolve)=>{
|
||||
requests.push(()=>{
|
||||
resolve(axios(error.config));
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Reference in New Issue