From b84a3255c6d65dea32c04d138d7173c9b5de9ac6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B6=85=E7=BA=A7=E8=80=81=E7=99=BD=E5=85=94?= Date: Mon, 8 Sep 2025 10:30:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(weChat):=20=E4=BF=AE=E5=A4=8D=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E5=8A=A0=E8=BD=BD=E9=A1=BA=E5=BA=8F=E5=92=8C=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E7=8A=B6=E6=80=81=E6=98=BE=E7=A4=BA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 调整消息数组拼接顺序,确保新消息正确追加 添加加载状态图标并优化加载更多消息的逻辑 设置消息加载状态延迟更新以避免闪烁 --- .../components/MessageRecord/index.tsx | 35 +++++++++++++++++-- Cunkebao/src/store/module/weChat/weChat.ts | 8 +++-- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/Cunkebao/src/pages/pc/ckbox/components/ChatWindow/components/MessageRecord/index.tsx b/Cunkebao/src/pages/pc/ckbox/components/ChatWindow/components/MessageRecord/index.tsx index 20c1f580..662aa53c 100644 --- a/Cunkebao/src/pages/pc/ckbox/components/ChatWindow/components/MessageRecord/index.tsx +++ b/Cunkebao/src/pages/pc/ckbox/components/ChatWindow/components/MessageRecord/index.tsx @@ -1,6 +1,6 @@ import React, { useEffect, useRef } from "react"; import { Avatar, Divider } from "antd"; -import { UserOutlined } from "@ant-design/icons"; +import { UserOutlined, LoadingOutlined } from "@ant-design/icons"; import { ChatRecord, ContractData, weChatGroup } from "@/pages/pc/ckbox/data"; import { formatWechatTime } from "@/utils/common"; import styles from "./MessageRecord.module.scss"; @@ -194,11 +194,40 @@ const MessageRecord: React.FC = ({ contract }) => { ); }; + const loadMoreMessages = () => { + // 兼容性处理:检查消息数组和时间戳 + if (!currentMessages || currentMessages.length === 0) { + console.warn("No messages available for loading more"); + return; + } + + const firstMessage = currentMessages[0]; + if (!firstMessage || !firstMessage.createTime) { + console.warn("Invalid message or createTime"); + return; + } + + // 兼容性处理:确保时间戳格式正确 + let timestamp; + try { + const date = new Date(firstMessage.createTime); + if (isNaN(date.getTime())) { + console.warn("Invalid createTime format:", firstMessage.createTime); + return; + } + timestamp = date.getTime() - 20000; + } catch (error) { + console.error("Error parsing createTime:", error); + return; + } + + loadChatMessages(false, timestamp); + }; return (
-
loadChatMessages(false)}> - 点击加载更早的信息{messagesLoading ? "中" : "1"} +
loadMoreMessages()}> + 点击加载更早的信息 {messagesLoading ? : ""}
{groupMessagesByTime(currentMessages).map((group, groupIndex) => ( diff --git a/Cunkebao/src/store/module/weChat/weChat.ts b/Cunkebao/src/store/module/weChat/weChat.ts index 612aa37b..2941c710 100644 --- a/Cunkebao/src/store/module/weChat/weChat.ts +++ b/Cunkebao/src/store/module/weChat/weChat.ts @@ -72,8 +72,8 @@ export const useWeChatStore = create()( } else { set({ currentMessages: [ - ...state.currentMessages, ...(messages || []), + ...state.currentMessages, ], }); } @@ -85,8 +85,8 @@ export const useWeChatStore = create()( } else { set({ currentMessages: [ - ...state.currentMessages, ...(messages || []), + ...state.currentMessages, ], }); } @@ -94,7 +94,9 @@ export const useWeChatStore = create()( } catch (error) { console.error("获取聊天消息失败:", error); } finally { - set({ messagesLoading: false }); + setTimeout(() => { + set({ messagesLoading: false }); + }, 1500); } },