diff --git a/Cunkebao/src/pages/pc/ckbox/components/ChatWindow/index.tsx b/Cunkebao/src/pages/pc/ckbox/components/ChatWindow/index.tsx index bb9edc97..72c88cb7 100644 --- a/Cunkebao/src/pages/pc/ckbox/components/ChatWindow/index.tsx +++ b/Cunkebao/src/pages/pc/ckbox/components/ChatWindow/index.tsx @@ -43,10 +43,10 @@ const ChatWindow: React.FC = ({ useEffect(() => { const prevMessages = prevMessagesRef.current; - // 检查是否有视频状态变化(从加载中变为已完成) + // 检查是否有视频状态变化(从加载中变为已完成或开始加载) const hasVideoStateChange = currentMessages.some((msg, index) => { const prevMsg = prevMessages[index]; - if (!prevMsg) return false; + if (!prevMsg || prevMsg.id !== msg.id) return false; try { const currentContent = @@ -58,37 +58,33 @@ const ChatWindow: React.FC = ({ ? JSON.parse(prevMsg.content) : prevMsg.content; - // 检查是否从加载中变为已完成(有videoUrl) - return ( - prevContent.isLoading === true && - currentContent.isLoading === false && - currentContent.videoUrl - ); + // 检查视频状态是否发生变化(开始加载、完成加载、获得URL) + const currentHasVideo = + currentContent.previewImage && currentContent.tencentUrl; + const prevHasVideo = prevContent.previewImage && prevContent.tencentUrl; + + if (currentHasVideo && prevHasVideo) { + // 检查加载状态变化或视频URL变化 + return ( + currentContent.isLoading !== prevContent.isLoading || + currentContent.videoUrl !== prevContent.videoUrl + ); + } + + return false; } catch (e) { return false; } }); - // 检查是否有视频正在加载中 - const hasLoadingVideo = currentMessages.some(msg => { - try { - const content = - typeof msg.content === "string" - ? JSON.parse(msg.content) - : msg.content; - return content.isLoading === true; - } catch (e) { - return false; - } - }); - - // 只有在没有视频加载且没有视频状态变化时才自动滚动到底部 - if (!hasLoadingVideo && !hasVideoStateChange) { + // 只有在没有视频状态变化时才自动滚动到底部 + if (!hasVideoStateChange) { scrollToBottom(); } // 更新上一次的消息状态 prevMessagesRef.current = currentMessages; + console.log("视频状态变化:", hasVideoStateChange, currentMessages); }, [currentMessages]); const scrollToBottom = () => { diff --git a/Cunkebao/src/store/module/weChat/weChat.data.ts b/Cunkebao/src/store/module/weChat/weChat.data.ts index 1f938d32..dfd2e103 100644 --- a/Cunkebao/src/store/module/weChat/weChat.data.ts +++ b/Cunkebao/src/store/module/weChat/weChat.data.ts @@ -17,4 +17,5 @@ export interface WeChatState { // 视频消息处理方法 setVideoLoading: (messageId: number, isLoading: boolean) => void; setVideoUrl: (messageId: number, videoUrl: string) => void; + addMessage: (message: ChatRecord) => void; } diff --git a/Cunkebao/src/store/module/websocket/msgManage.ts b/Cunkebao/src/store/module/websocket/msgManage.ts index 4efc7a56..66610f93 100644 --- a/Cunkebao/src/store/module/websocket/msgManage.ts +++ b/Cunkebao/src/store/module/websocket/msgManage.ts @@ -7,7 +7,8 @@ import { Messages } from "./msg.data"; import { useWeChatStore } from "@/store/module/weChat/weChat"; // 消息处理器类型定义 type MessageHandler = (message: WebSocketMessage) => void; - +const setVideoUrl = useWeChatStore.getState().setVideoUrl; +const addMessage = useWeChatStore.getState().addMessage; // 消息处理器映射 const messageHandlers: Record = { // 微信账号存活状态响应 @@ -35,6 +36,7 @@ const messageHandlers: Record = { //收到消息 CmdNewMessage: (message: Messages) => { console.log("收到消息", message.friendMessage); + addMessage(message.friendMessage); // 在这里添加具体的处理逻辑 }, @@ -56,7 +58,7 @@ const messageHandlers: Record = { CmdDownloadVideoResult: message => { // 在这里添加具体的处理逻辑 - useWeChatStore.getState().setVideoUrl(message.friendMessageId, message.url); + setVideoUrl(message.friendMessageId, message.url); }, // 可以继续添加更多处理器... }; diff --git a/Cunkebao/src/store/module/websocket/websocket.ts b/Cunkebao/src/store/module/websocket/websocket.ts index 485931c6..d67e2a06 100644 --- a/Cunkebao/src/store/module/websocket/websocket.ts +++ b/Cunkebao/src/store/module/websocket/websocket.ts @@ -231,7 +231,16 @@ export const useWebSocketStore = createPersistStore( currentState.status !== WebSocketStatus.CONNECTED || !currentState.ws ) { - Toast.show({ content: "WebSocket未连接", position: "top" }); + Toast.show({ + content: "WebSocket未连接,正在重新连接...", + position: "top", + }); + + // 重置连接状态并发起重新连接 + set({ status: WebSocketStatus.DISCONNECTED }); + if (currentState.config) { + currentState.connect(currentState.config); + } return; } @@ -247,6 +256,12 @@ export const useWebSocketStore = createPersistStore( } catch (error) { // console.error("命令发送失败:", error); Toast.show({ content: "命令发送失败", position: "top" }); + + // 发送失败时也尝试重新连接 + set({ status: WebSocketStatus.DISCONNECTED }); + if (currentState.config) { + currentState.connect(currentState.config); + } } },