修复 refresh token 过期时,不会自动跳转到首页的问题
parent
c738b2c84d
commit
7926ea9896
|
@ -1,5 +1,5 @@
|
|||
import axios from 'axios'
|
||||
import { Notification, MessageBox, Message } from 'element-ui'
|
||||
import {Message, MessageBox, Notification} from 'element-ui'
|
||||
import store from '@/store'
|
||||
import {getAccessToken, getRefreshToken, setToken} from '@/utils/auth'
|
||||
import errorCode from '@/utils/errorCode'
|
||||
|
@ -7,6 +7,12 @@ import Cookies from "js-cookie";
|
|||
import {getPath, getTenantEnable} from "@/utils/ruoyi";
|
||||
import {refreshToken} from "@/api/login";
|
||||
|
||||
// 需要忽略的提示。忽略后,自动 Promise.reject('error')
|
||||
const ignoreMsgs = [
|
||||
"无效的刷新令牌", // 刷新令牌被删除时,不用提示
|
||||
"刷新令牌已过期" // 使用刷新令牌,刷新获取新的访问令牌时,结果因为过期失败,此时需要忽略。否则,会导致继续 401,无法跳转到登出界面
|
||||
]
|
||||
|
||||
// 是否显示重新登录
|
||||
export let isRelogin = { show: false };
|
||||
// Axios 无感知刷新令牌,参考 https://www.dashingdog.cn/article/11 与 https://segmentfault.com/a/1190000020210980 实现
|
||||
|
@ -15,6 +21,7 @@ let requestList = []
|
|||
// 是否正在刷新中
|
||||
let isRefreshToken = false
|
||||
|
||||
|
||||
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
|
||||
// 创建axios实例
|
||||
const service = axios.create({
|
||||
|
@ -70,8 +77,11 @@ service.interceptors.response.use( async res => {
|
|||
// 未设置状态码则默认成功状态
|
||||
const code = res.data.code || 200;
|
||||
// 获取错误信息
|
||||
const msg = errorCode[code] || res.data.msg || errorCode['default']
|
||||
if (code === 401) {
|
||||
const msg = res.data.msg || errorCode[code] || errorCode['default']
|
||||
if (ignoreMsgs.indexOf(msg) !== -1) { // 如果是忽略的错误码,直接返回 msg 异常
|
||||
console.log('132312311');
|
||||
return Promise.reject(msg)
|
||||
} else if (code === 401) {
|
||||
// 如果未认证,并且未进行刷新令牌,说明可能是访问令牌过期了
|
||||
if (!isRefreshToken) {
|
||||
isRefreshToken = true;
|
||||
|
@ -124,7 +134,7 @@ service.interceptors.response.use( async res => {
|
|||
return Promise.reject(new Error(msg))
|
||||
} else if (code !== 200) {
|
||||
if (msg === '无效的刷新令牌') { // hard coding:忽略这个提示,直接登出
|
||||
console.log('无效的刷新令牌')
|
||||
console.log(msg)
|
||||
} else {
|
||||
Notification.error({
|
||||
title: msg
|
||||
|
@ -134,17 +144,14 @@ service.interceptors.response.use( async res => {
|
|||
} else {
|
||||
return res.data
|
||||
}
|
||||
},
|
||||
error => {
|
||||
}, error => {
|
||||
console.log('err' + error)
|
||||
let {message} = error;
|
||||
if (message === "Network Error") {
|
||||
message = "后端接口连接异常";
|
||||
}
|
||||
else if (message.includes("timeout")) {
|
||||
} else if (message.includes("timeout")) {
|
||||
message = "系统接口请求超时";
|
||||
}
|
||||
else if (message.includes("Request failed with status code")) {
|
||||
} else if (message.includes("Request failed with status code")) {
|
||||
message = "系统接口" + message.substr(message.length - 3) + "异常";
|
||||
}
|
||||
Message({
|
||||
|
|
Loading…
Reference in New Issue