diff --git a/Touchkebao/src/store/module/websocket/websocket.ts b/Touchkebao/src/store/module/websocket/websocket.ts index 51134cc2..57ed7f15 100644 --- a/Touchkebao/src/store/module/websocket/websocket.ts +++ b/Touchkebao/src/store/module/websocket/websocket.ts @@ -53,6 +53,7 @@ interface WebSocketState { reconnectTimer: NodeJS.Timeout | null; aliveStatusTimer: NodeJS.Timeout | null; // 客服用户状态查询定时器 aliveStatusUnsubscribe: (() => void) | null; + aliveStatusLastRequest: number | null; // 方法 connect: (config: Partial) => void; @@ -88,6 +89,8 @@ const DEFAULT_CONFIG: WebSocketConfig = { maxReconnectAttempts: 5, }; +const ALIVE_STATUS_MIN_INTERVAL = 5 * 1000; // ms + export const useWebSocketStore = createPersistStore( (set, get) => ({ status: WebSocketStatus.DISCONNECTED, @@ -99,6 +102,7 @@ export const useWebSocketStore = createPersistStore( reconnectTimer: null, aliveStatusTimer: null, aliveStatusUnsubscribe: null, + aliveStatusLastRequest: null, // 连接WebSocket connect: (config: Partial) => { @@ -234,11 +238,6 @@ export const useWebSocketStore = createPersistStore( currentState.status !== WebSocketStatus.CONNECTED || !currentState.ws ) { - // Toast.show({ - // content: "WebSocket未连接,正在重新连接...", - // position: "top", - // }); - // 重置连接状态并发起重新连接 set({ status: WebSocketStatus.DISCONNECTED }); if (currentState.config) { @@ -485,6 +484,14 @@ export const useWebSocketStore = createPersistStore( return; } + const now = Date.now(); + if ( + state.aliveStatusLastRequest && + now - state.aliveStatusLastRequest < ALIVE_STATUS_MIN_INTERVAL + ) { + return; + } + const { customerList } = useCustomerStore.getState(); const { kfUserList } = useCkChatStore.getState(); const targets = @@ -498,6 +505,7 @@ export const useWebSocketStore = createPersistStore( state.sendCommand("CmdRequestWechatAccountsAliveStatus", { wechatAccountIds: targets.map(v => v.id), }); + set({ aliveStatusLastRequest: now }); } }; @@ -556,7 +564,11 @@ export const useWebSocketStore = createPersistStore( if (currentState.aliveStatusTimer) { clearInterval(currentState.aliveStatusTimer); } - set({ aliveStatusTimer: null, aliveStatusUnsubscribe: null }); + set({ + aliveStatusTimer: null, + aliveStatusUnsubscribe: null, + aliveStatusLastRequest: null, + }); }, }), { @@ -568,6 +580,7 @@ export const useWebSocketStore = createPersistStore( messages: state.messages.slice(-100), // 只保留最近100条消息 unreadCount: state.unreadCount, reconnectAttempts: state.reconnectAttempts, + aliveStatusLastRequest: state.aliveStatusLastRequest, // 注意:定时器不需要持久化,重新连接时会重新创建 }), onRehydrateStorage: () => state => {