|
@@ -10,9 +10,9 @@ import type {
|
|
|
PureHttpRequestConfig
|
|
PureHttpRequestConfig
|
|
|
} from "./types.d";
|
|
} from "./types.d";
|
|
|
import { stringify } from "qs";
|
|
import { stringify } from "qs";
|
|
|
-import { getToken } from "@/utils/auth";
|
|
|
|
|
-import { useUserStoreHook } from "@/store/modules/user";
|
|
|
|
|
|
|
+import { getToken, removeToken } from "@/utils/auth";
|
|
|
import { ElMessage, ElMessageBox } from "element-plus";
|
|
import { ElMessage, ElMessageBox } from "element-plus";
|
|
|
|
|
+import router from "@/router";
|
|
|
|
|
|
|
|
// API 基础 URL
|
|
// API 基础 URL
|
|
|
const baseUrl = import.meta.env.VITE_API_URL || `${location.origin}/admin/`;
|
|
const baseUrl = import.meta.env.VITE_API_URL || `${location.origin}/admin/`;
|
|
@@ -65,6 +65,9 @@ class PureHttp {
|
|
|
/** 防止重复刷新`token` */
|
|
/** 防止重复刷新`token` */
|
|
|
private static isRefreshing = false;
|
|
private static isRefreshing = false;
|
|
|
|
|
|
|
|
|
|
+ /** 防止重复跳转登录页 */
|
|
|
|
|
+ private static isRedirecting = false;
|
|
|
|
|
+
|
|
|
/** 初始化配置对象 */
|
|
/** 初始化配置对象 */
|
|
|
private static initConfig: PureHttpRequestConfig = {};
|
|
private static initConfig: PureHttpRequestConfig = {};
|
|
|
|
|
|
|
@@ -129,12 +132,20 @@ class PureHttp {
|
|
|
if (res.code !== undefined && res.code !== 0 && res.code !== 200) {
|
|
if (res.code !== undefined && res.code !== 0 && res.code !== 200) {
|
|
|
// token 过期或未登录
|
|
// token 过期或未登录
|
|
|
if (res.code === 401) {
|
|
if (res.code === 401) {
|
|
|
|
|
+ if (PureHttp.isRedirecting) {
|
|
|
|
|
+ return Promise.reject(new Error(res.msg || '未授权'));
|
|
|
|
|
+ }
|
|
|
|
|
+ PureHttp.isRedirecting = true;
|
|
|
|
|
+ removeToken();
|
|
|
ElMessageBox.alert('你已被登出,请重新登录', '提示', {})
|
|
ElMessageBox.alert('你已被登出,请重新登录', '提示', {})
|
|
|
.then(() => {
|
|
.then(() => {
|
|
|
- window.location.href = '/#/login';
|
|
|
|
|
|
|
+ router.push('/login');
|
|
|
})
|
|
})
|
|
|
.catch(() => {
|
|
.catch(() => {
|
|
|
- window.location.href = '/#/login';
|
|
|
|
|
|
|
+ router.push('/login');
|
|
|
|
|
+ })
|
|
|
|
|
+ .finally(() => {
|
|
|
|
|
+ PureHttp.isRedirecting = false;
|
|
|
});
|
|
});
|
|
|
return Promise.reject(new Error(res.msg || '未授权'));
|
|
return Promise.reject(new Error(res.msg || '未授权'));
|
|
|
}
|
|
}
|