mirror of
https://github.com/pure-admin/pure-admin-thin.git
synced 2025-11-10 12:43:37 +08:00
perf: http
This commit is contained in:
3
src/api/utils.ts
Normal file
3
src/api/utils.ts
Normal 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}`;
|
||||
47
src/utils/http/ApiAbstract.ts
Normal file
47
src/utils/http/ApiAbstract.ts
Normal 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;
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user