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,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') {