feat:对返回上一页做了处理

This commit is contained in:
许永平
2025-07-05 21:09:28 +08:00
parent 7b29660a51
commit 0385a4cfa9
13 changed files with 317 additions and 23 deletions

View File

@@ -1,4 +1,4 @@
import { refreshAuthToken, isTokenExpiringSoon, clearToken } from './utils';
import { refreshAuthToken, isTokenExpiringSoon, clearToken, requestDeduplicator } from './utils';
// Token过期处理
export const handleTokenExpired = () => {
@@ -6,6 +6,9 @@ export const handleTokenExpired = () => {
// 清除本地存储
clearToken();
// 清除所有待处理的请求
requestDeduplicator.clear();
// 跳转到登录页面
setTimeout(() => {
window.location.href = '/login';
@@ -125,6 +128,8 @@ export const setupNetworkListener = () => {
const handleOffline = () => {
console.log('网络已断开');
// 清除所有待处理的请求
requestDeduplicator.clear();
showApiError(null, '网络连接已断开,请检查网络设置');
};
@@ -148,5 +153,7 @@ export const initInterceptors = () => {
if (cleanupNetwork) {
cleanupNetwork();
}
// 清理所有待处理的请求
requestDeduplicator.clear();
};
};

View File

@@ -1,5 +1,6 @@
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import { requestInterceptor, responseInterceptor, errorInterceptor } from './interceptors';
import { requestDeduplicator, throttle } from './utils';
// 创建axios实例
const request: AxiosInstance = axios.create({
@@ -48,26 +49,52 @@ request.interceptors.response.use(
}
);
// 封装GET请求
// 带节流和去重的GET请求
export const get = <T = any>(url: string, config?: AxiosRequestConfig): Promise<T> => {
return request.get(url, config);
return requestDeduplicator.execute(
'GET',
url,
() => request.get(url, config),
config
);
};
// 封装POST请求
// 带节流和去重的POST请求
export const post = <T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> => {
return request.post(url, data, config);
return requestDeduplicator.execute(
'POST',
url,
() => request.post(url, data, config),
data
);
};
// 封装PUT请求
// 带节流和去重的PUT请求
export const put = <T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> => {
return request.put(url, data, config);
return requestDeduplicator.execute(
'PUT',
url,
() => request.put(url, data, config),
data
);
};
// 封装DELETE请求
// 带节流和去重的DELETE请求
export const del = <T = any>(url: string, config?: AxiosRequestConfig): Promise<T> => {
return request.delete(url, config);
return requestDeduplicator.execute(
'DELETE',
url,
() => request.delete(url, config),
config
);
};
// 带节流的请求函数(用于需要节流但不需要去重的场景)
export const throttledGet = throttle(get, 1000);
export const throttledPost = throttle(post, 1000);
export const throttledPut = throttle(put, 1000);
export const throttledDelete = throttle(del, 1000);
// 导出request实例
export { request };
export default request;

View File

@@ -1,5 +1,85 @@
import { authApi } from './auth';
// 节流函数 - 防止重复请求
export const throttle = <T extends (...args: any[]) => any>(
func: T,
delay: number = 1000
): T => {
let lastCall = 0;
let lastCallTimer: NodeJS.Timeout | null = null;
return ((...args: any[]) => {
const now = Date.now();
if (now - lastCall < delay) {
// 如果在节流时间内,取消之前的定时器并设置新的
if (lastCallTimer) {
clearTimeout(lastCallTimer);
}
lastCallTimer = setTimeout(() => {
lastCall = now;
func(...args);
}, delay - (now - lastCall));
} else {
// 如果超过节流时间,直接执行
lastCall = now;
func(...args);
}
}) as T;
};
// 请求去重管理器
class RequestDeduplicator {
private pendingRequests: Map<string, Promise<any>> = new Map();
// 生成请求的唯一键
private generateKey(method: string, url: string, data?: any): string {
const dataStr = data ? JSON.stringify(data) : '';
return `${method}:${url}:${dataStr}`;
}
// 执行请求如果相同请求正在进行中则返回已存在的Promise
async execute<T>(
method: string,
url: string,
requestFn: () => Promise<T>,
data?: any
): Promise<T> {
const key = this.generateKey(method, url, data);
// 如果相同的请求正在进行中返回已存在的Promise
if (this.pendingRequests.has(key)) {
console.log(`请求去重: ${key}`);
return this.pendingRequests.get(key)!;
}
// 创建新的请求Promise
const requestPromise = requestFn().finally(() => {
// 请求完成后从pendingRequests中移除
this.pendingRequests.delete(key);
});
// 将请求Promise存储到Map中
this.pendingRequests.set(key, requestPromise);
return requestPromise;
}
// 清除所有待处理的请求
clear() {
this.pendingRequests.clear();
}
// 获取当前待处理请求数量
getPendingCount(): number {
return this.pendingRequests.size;
}
}
// 创建全局请求去重实例
export const requestDeduplicator = new RequestDeduplicator();
// 设置token到localStorage
export const setToken = (token: string) => {
if (typeof window !== 'undefined') {