perf: http

This commit is contained in:
1332987
2024-03-12 23:08:08 +08:00
parent 73a347f34f
commit 52ab1f8a33
7 changed files with 97 additions and 33 deletions

3
src/api/utils.ts Normal file
View File

@@ -0,0 +1,3 @@
export const baseUrlApi = (url: string) => `/api/${url}`;
export const baseUrlAuth = (url: string) => `/auth/${url}`;
export const baseUrlAvatar = (url: string) => `/avatar/${url}`;

View File

@@ -0,0 +1,47 @@
export class ApiAbstract<T = any> {
public status: number;
public timestamp: Date;
public message: string;
public data: T | T[] | Page<T> | any;
}
export class Page<T> {
content: T[];
totalElements: number;
}
export class PageQuery {
page?: number = 0;
size?: number = 10;
sort?: string = "id,asc";
}
export class VersionEntity {
/**
* 乐观锁
*/
version: number;
}
export class BaseEntity extends VersionEntity {
/**
* 创建人
*/
createBy?: string;
/**
* 创建时间
*/
createTime?: Date;
/**
* 更新人
*/
updateBy?: string;
/**
* 更新时间
*/
updateTime?: Date;
/**
* 删除状态
*/
deleted?: number;
}

View File

@@ -12,8 +12,6 @@ import type {
import { stringify } from "qs";
import NProgress from "../progress";
import { getToken, formatToken } from "@/utils/auth";
import { useUserStoreHook } from "@/store/modules/user";
// 相关配置请参考www.axios-js.com/zh-cn/docs/#axios-request-config-1
const defaultConfig: AxiosRequestConfig = {
// 请求超时时间
@@ -73,37 +71,20 @@ class PureHttp {
return config;
}
/** 请求白名单放置一些不需要token的接口通过设置请求白名单防止token过期后再请求造成的死循环问题 */
const whiteList = ["/refresh-token", "/login"];
return whiteList.find(url => url === config.url)
const whiteList = ["/refresh-token", "/login", "/auth", "/auth/*"];
return whiteList.find(
url =>
url === config.url ||
(url.endsWith("*") &&
config.url.startsWith(url.replace("*", "")) &&
config.url != "/auth/logout")
)
? config
: new Promise(resolve => {
const data = getToken();
if (data) {
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 => {
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"] = formatToken(
data.accessToken
);
resolve(config);
}
config.headers["Authorization"] = formatToken(data.accessToken);
resolve(config);
} else {
resolve(config);
}