添加高频轮询接口白名单,优化请求截流逻辑以支持特定接口的无截流请求。允许通过配置控制截流功能,提升请求处理的灵活性和用户体验。

This commit is contained in:
乘风
2025-12-01 16:58:58 +08:00
parent 11a4ec8c67
commit 1dbdee155e
2 changed files with 53 additions and 18 deletions

View File

@@ -10,6 +10,13 @@ const { token } = useUserStore.getState();
const DEFAULT_DEBOUNCE_GAP = 1000;
const debounceMap = new Map<string, number>();
// 需要高频轮询、不走截流的接口白名单(按实际接口路径调整)
const NO_DEBOUNCE_URLS = [
"/wechat/friend/list", // 好友列表
"/wechat/group/list", // 群组列表
"/wechat/message/list", // 消息列表
];
const instance: AxiosInstance = axios.create({
baseURL: (import.meta as any).env?.VITE_API_BASE_URL || "/api",
timeout: 20000,
@@ -64,21 +71,31 @@ export function request(
url: string,
data?: any,
method: Method = "GET",
config?: AxiosRequestConfig,
// 允许通过 config.debounce 控制是否开启截流,默认开启
config?: AxiosRequestConfig & { debounce?: boolean },
debounceGap?: number,
): Promise<any> {
const gap =
typeof debounceGap === "number" ? debounceGap : DEFAULT_DEBOUNCE_GAP;
const key = `${method}_${url}_${JSON.stringify(data)}`;
const now = Date.now();
const last = debounceMap.get(key) || 0;
if (gap > 0 && now - last < gap) {
// Toast.show({ content: '请求过于频繁,请稍后再试', position: 'top' });
return Promise.reject("请求过于频繁,请稍后再试");
}
debounceMap.set(key, now);
const axiosConfig: AxiosRequestConfig = {
const enableDebounce = config?.debounce !== false;
const isInNoDebounceList = NO_DEBOUNCE_URLS.some(pattern =>
url.includes(pattern),
);
const shouldDebounce = enableDebounce && !isInNoDebounceList;
if (shouldDebounce) {
const key = `${method}_${url}_${JSON.stringify(data)}`;
const now = Date.now();
const last = debounceMap.get(key) || 0;
if (gap > 0 && now - last < gap) {
// Toast.show({ content: '请求过于频繁,请稍后再试', position: 'top' });
return Promise.reject("请求过于频繁,请稍后再试");
}
debounceMap.set(key, now);
}
const axiosConfig: AxiosRequestConfig & { debounce?: boolean } = {
url,
method,
...config,

View File

@@ -9,11 +9,20 @@ import { useUserStore } from "@/store/module/user";
const DEFAULT_DEBOUNCE_GAP = 1000;
const debounceMap = new Map<string, number>();
// 需要高频轮询、不走截流的接口白名单(按实际接口路径调整)
const NO_DEBOUNCE_URLS = [
"/wechat/friend/list",
"/wechat/group/list",
"/wechat/message/list",
];
interface RequestConfig extends AxiosRequestConfig {
headers: {
headers?: {
Client?: string;
"Content-Type"?: string;
};
// 是否开启截流,默认开启
debounce?: boolean;
}
const instance: AxiosInstance = axios.create({
@@ -63,14 +72,23 @@ export function request(
): Promise<any> {
const gap =
typeof debounceGap === "number" ? debounceGap : DEFAULT_DEBOUNCE_GAP;
const key = `${method}_${url}_${JSON.stringify(data)}`;
const now = Date.now();
const last = debounceMap.get(key) || 0;
if (gap > 0 && now - last < gap) {
// Toast.show({ content: '请求过于频繁,请稍后再试', position: 'top' });
return Promise.reject("请求过于频繁,请稍后再试");
const enableDebounce = config?.debounce !== false;
const isInNoDebounceList = NO_DEBOUNCE_URLS.some(pattern =>
url.includes(pattern),
);
const shouldDebounce = enableDebounce && !isInNoDebounceList;
if (shouldDebounce) {
const key = `${method}_${url}_${JSON.stringify(data)}`;
const now = Date.now();
const last = debounceMap.get(key) || 0;
if (gap > 0 && now - last < gap) {
// Toast.show({ content: '请求过于频繁,请稍后再试', position: 'top' });
return Promise.reject("请求过于频繁,请稍后再试");
}
debounceMap.set(key, now);
}
debounceMap.set(key, now);
const axiosConfig: RequestConfig = {
url,