diff --git a/Touchkebao/src/api/request.ts b/Touchkebao/src/api/request.ts index adfa7ea5..35e7bbe5 100644 --- a/Touchkebao/src/api/request.ts +++ b/Touchkebao/src/api/request.ts @@ -10,6 +10,13 @@ const { token } = useUserStore.getState(); const DEFAULT_DEBOUNCE_GAP = 1000; const debounceMap = new Map(); +// 需要高频轮询、不走截流的接口白名单(按实际接口路径调整) +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 { 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, diff --git a/Touchkebao/src/api/request2.ts b/Touchkebao/src/api/request2.ts index deb3d9bf..13732be0 100644 --- a/Touchkebao/src/api/request2.ts +++ b/Touchkebao/src/api/request2.ts @@ -9,11 +9,20 @@ import { useUserStore } from "@/store/module/user"; const DEFAULT_DEBOUNCE_GAP = 1000; const debounceMap = new Map(); +// 需要高频轮询、不走截流的接口白名单(按实际接口路径调整) +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 { 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,