release: update 3.6.3
This commit is contained in:
parent
761b0e5ec2
commit
841c5bd53a
@ -10,8 +10,8 @@ export default [
|
|||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
data: {
|
data: {
|
||||||
accessToken: "eyJhbGciOiJIUzUxMiJ9.admin",
|
accessToken: "eyJhbGciOiJIUzUxMiJ9.newAdmin",
|
||||||
refreshToken: "eyJhbGciOiJIUzUxMiJ9.adminRefresh",
|
refreshToken: "eyJhbGciOiJIUzUxMiJ9.newAdminRefresh",
|
||||||
// `expires`选择这种日期格式是为了方便调试,后端直接设置时间戳或许更方便(每次都应该递增)。如果后端返回的是时间戳格式,前端开发请来到这个目录`src/utils/auth.ts`,把第`38`行的代码换成expires = data.expires即可。
|
// `expires`选择这种日期格式是为了方便调试,后端直接设置时间戳或许更方便(每次都应该递增)。如果后端返回的是时间戳格式,前端开发请来到这个目录`src/utils/auth.ts`,把第`38`行的代码换成expires = data.expires即可。
|
||||||
expires: "2023/10/30 23:59:59"
|
expires: "2023/10/30 23:59:59"
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "pure-admin-thin",
|
"name": "pure-admin-thin",
|
||||||
"version": "3.6.2",
|
"version": "3.6.3",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "NODE_OPTIONS=--max-old-space-size=4096 vite",
|
"dev": "NODE_OPTIONS=--max-old-space-size=4096 vite",
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"Version": "3.6.2",
|
"Version": "3.6.3",
|
||||||
"Title": "PureAdmin",
|
"Title": "PureAdmin",
|
||||||
"FixedHeader": true,
|
"FixedHeader": true,
|
||||||
"HiddenSideBar": false,
|
"HiddenSideBar": false,
|
||||||
|
@ -70,3 +70,8 @@ export function removeToken() {
|
|||||||
Cookies.remove(TokenKey);
|
Cookies.remove(TokenKey);
|
||||||
sessionStorage.removeItem(sessionKey);
|
sessionStorage.removeItem(sessionKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 格式化token(jwt格式) */
|
||||||
|
export const formatToken = (token: string): string => {
|
||||||
|
return "Bearer " + token;
|
||||||
|
};
|
||||||
|
@ -12,7 +12,7 @@ import {
|
|||||||
import { stringify } from "qs";
|
import { stringify } from "qs";
|
||||||
import NProgress from "../progress";
|
import NProgress from "../progress";
|
||||||
// import { loadEnv } from "@build/index";
|
// import { loadEnv } from "@build/index";
|
||||||
import { getToken } from "@/utils/auth";
|
import { getToken, formatToken } from "@/utils/auth";
|
||||||
import { useUserStoreHook } from "@/store/modules/user";
|
import { useUserStoreHook } from "@/store/modules/user";
|
||||||
|
|
||||||
// 加载环境变量 VITE_PROXY_DOMAIN(开发环境) VITE_PROXY_DOMAIN_REAL(打包后的线上环境)
|
// 加载环境变量 VITE_PROXY_DOMAIN(开发环境) VITE_PROXY_DOMAIN_REAL(打包后的线上环境)
|
||||||
@ -43,27 +43,43 @@ class PureHttp {
|
|||||||
this.httpInterceptorsRequest();
|
this.httpInterceptorsRequest();
|
||||||
this.httpInterceptorsResponse();
|
this.httpInterceptorsResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** token过期后,暂存待执行的请求 */
|
||||||
|
private static requests = [];
|
||||||
|
|
||||||
|
/** 防止重复刷新token */
|
||||||
|
private static isRefreshing = false;
|
||||||
|
|
||||||
/** 初始化配置对象 */
|
/** 初始化配置对象 */
|
||||||
private static initConfig: PureHttpRequestConfig = {};
|
private static initConfig: PureHttpRequestConfig = {};
|
||||||
|
|
||||||
/** 保存当前Axios实例对象 */
|
/** 保存当前Axios实例对象 */
|
||||||
private static axiosInstance: AxiosInstance = Axios.create(defaultConfig);
|
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 {
|
private httpInterceptorsRequest(): void {
|
||||||
PureHttp.axiosInstance.interceptors.request.use(
|
PureHttp.axiosInstance.interceptors.request.use(
|
||||||
async (config: PureHttpRequestConfig) => {
|
async (config: PureHttpRequestConfig) => {
|
||||||
const $config = config;
|
|
||||||
// 开启进度条动画
|
// 开启进度条动画
|
||||||
NProgress.start();
|
NProgress.start();
|
||||||
// 优先判断post/get等方法是否传入回掉,否则执行初始化设置等回掉
|
// 优先判断post/get等方法是否传入回掉,否则执行初始化设置等回掉
|
||||||
if (typeof config.beforeRequestCallback === "function") {
|
if (typeof config.beforeRequestCallback === "function") {
|
||||||
config.beforeRequestCallback($config);
|
config.beforeRequestCallback(config);
|
||||||
return $config;
|
return config;
|
||||||
}
|
}
|
||||||
if (PureHttp.initConfig.beforeRequestCallback) {
|
if (PureHttp.initConfig.beforeRequestCallback) {
|
||||||
PureHttp.initConfig.beforeRequestCallback($config);
|
PureHttp.initConfig.beforeRequestCallback(config);
|
||||||
return $config;
|
return config;
|
||||||
}
|
}
|
||||||
/** 请求白名单,放置一些不需要token的接口(通过设置请求白名单,防止token过期后再请求造成的死循环问题) */
|
/** 请求白名单,放置一些不需要token的接口(通过设置请求白名单,防止token过期后再请求造成的死循环问题) */
|
||||||
const whiteList = ["/refreshToken", "/login"];
|
const whiteList = ["/refreshToken", "/login"];
|
||||||
@ -75,21 +91,30 @@ class PureHttp {
|
|||||||
const now = new Date().getTime();
|
const now = new Date().getTime();
|
||||||
const expired = parseInt(data.expires) - now <= 0;
|
const expired = parseInt(data.expires) - now <= 0;
|
||||||
if (expired) {
|
if (expired) {
|
||||||
// token过期刷新
|
if (!PureHttp.isRefreshing) {
|
||||||
useUserStoreHook()
|
PureHttp.isRefreshing = true;
|
||||||
.handRefreshToken({ refreshToken: data.refreshToken })
|
// token过期刷新
|
||||||
.then(res => {
|
useUserStoreHook()
|
||||||
config.headers["Authorization"] =
|
.handRefreshToken({ refreshToken: data.refreshToken })
|
||||||
"Bearer " + res.data.accessToken;
|
.then(res => {
|
||||||
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 {
|
} else {
|
||||||
config.headers["Authorization"] =
|
config.headers["Authorization"] = formatToken(
|
||||||
"Bearer " + data.accessToken;
|
data.accessToken
|
||||||
resolve($config);
|
);
|
||||||
|
resolve(config);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
resolve($config);
|
resolve(config);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user