|
|
@ -12,7 +12,7 @@ import { |
|
|
|
import { stringify } from "qs"; |
|
|
|
import NProgress from "../progress"; |
|
|
|
// import { loadEnv } from "@build/index";
|
|
|
|
import { getToken } from "@/utils/auth"; |
|
|
|
import { getToken, formatToken } from "@/utils/auth"; |
|
|
|
import { useUserStoreHook } from "@/store/modules/user"; |
|
|
|
|
|
|
|
// 加载环境变量 VITE_PROXY_DOMAIN(开发环境) VITE_PROXY_DOMAIN_REAL(打包后的线上环境)
|
|
|
@ -43,27 +43,43 @@ class PureHttp { |
|
|
|
this.httpInterceptorsRequest(); |
|
|
|
this.httpInterceptorsResponse(); |
|
|
|
} |
|
|
|
|
|
|
|
/** token过期后,暂存待执行的请求 */ |
|
|
|
private static requests = []; |
|
|
|
|
|
|
|
/** 防止重复刷新token */ |
|
|
|
private static isRefreshing = false; |
|
|
|
|
|
|
|
/** 初始化配置对象 */ |
|
|
|
private static initConfig: PureHttpRequestConfig = {}; |
|
|
|
|
|
|
|
/** 保存当前Axios实例对象 */ |
|
|
|
private static axiosInstance: AxiosInstance = Axios.create(defaultConfig); |
|
|
|
|
|
|
|
/** 重连原始请求 */ |
|
|
|
private static retryOriginalRequest(config: PureHttpRequestConfig) { |
|
|
|
return new Promise(resolve => { |
|
|
|
PureHttp.requests.push((token: string) => { |
|
|
|
config.headers["Authorization"] = formatToken(token); |
|
|
|
resolve(config); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
/** 请求拦截 */ |
|
|
|
private httpInterceptorsRequest(): void { |
|
|
|
PureHttp.axiosInstance.interceptors.request.use( |
|
|
|
async (config: PureHttpRequestConfig) => { |
|
|
|
const $config = config; |
|
|
|
// 开启进度条动画
|
|
|
|
NProgress.start(); |
|
|
|
// 优先判断post/get等方法是否传入回掉,否则执行初始化设置等回掉
|
|
|
|
if (typeof config.beforeRequestCallback === "function") { |
|
|
|
config.beforeRequestCallback($config); |
|
|
|
return $config; |
|
|
|
config.beforeRequestCallback(config); |
|
|
|
return config; |
|
|
|
} |
|
|
|
if (PureHttp.initConfig.beforeRequestCallback) { |
|
|
|
PureHttp.initConfig.beforeRequestCallback($config); |
|
|
|
return $config; |
|
|
|
PureHttp.initConfig.beforeRequestCallback(config); |
|
|
|
return config; |
|
|
|
} |
|
|
|
/** 请求白名单,放置一些不需要token的接口(通过设置请求白名单,防止token过期后再请求造成的死循环问题) */ |
|
|
|
const whiteList = ["/refreshToken", "/login"]; |
|
|
@ -75,21 +91,30 @@ class PureHttp { |
|
|
|
const now = new Date().getTime(); |
|
|
|
const expired = parseInt(data.expires) - now <= 0; |
|
|
|
if (expired) { |
|
|
|
if (!PureHttp.isRefreshing) { |
|
|
|
PureHttp.isRefreshing = true; |
|
|
|
// token过期刷新
|
|
|
|
useUserStoreHook() |
|
|
|
.handRefreshToken({ refreshToken: data.refreshToken }) |
|
|
|
.then(res => { |
|
|
|
config.headers["Authorization"] = |
|
|
|
"Bearer " + res.data.accessToken; |
|
|
|
resolve($config); |
|
|
|
const token = res.data.accessToken; |
|
|
|
config.headers["Authorization"] = formatToken(token); |
|
|
|
PureHttp.requests.forEach(cb => cb(token)); |
|
|
|
PureHttp.requests = []; |
|
|
|
}) |
|
|
|
.finally(() => { |
|
|
|
PureHttp.isRefreshing = false; |
|
|
|
}); |
|
|
|
} |
|
|
|
resolve(PureHttp.retryOriginalRequest(config)); |
|
|
|
} else { |
|
|
|
config.headers["Authorization"] = |
|
|
|
"Bearer " + data.accessToken; |
|
|
|
resolve($config); |
|
|
|
config.headers["Authorization"] = formatToken( |
|
|
|
data.accessToken |
|
|
|
); |
|
|
|
resolve(config); |
|
|
|
} |
|
|
|
} else { |
|
|
|
resolve($config); |
|
|
|
resolve(config); |
|
|
|
} |
|
|
|
}); |
|
|
|
}, |
|
|
|