Browse Source

🐛 fix(classroom): 修复聊天历史消息加载和排序问题

- 将聊天历史消息分页大小从50调整为200,加载更多历史记录
- 更改排序顺序为降序(DESC)以获取最新的历史消息
- 添加数组反转处理,确保消息按时间正序显示
yourname 2 months ago
parent
commit
9d7c7cadc2
1 changed files with 6 additions and 6 deletions
  1. 6 6
      src/client/mobile/components/Classroom/useClassroom.ts

+ 6 - 6
src/client/mobile/components/Classroom/useClassroom.ts

@@ -740,17 +740,17 @@ export const useClassroom = ({ user }:{ user : User }) => {
         const response = await chatMessageClient.$get({
           query: {
             page: 1,
-            pageSize: 50, 
+            pageSize: 200,
             filters: JSON.stringify({classId}),
             sortBy: 'timestamp',
-            sortOrder: 'ASC'
+            sortOrder: 'DESC'
           }
         });
-        
+
         if (response.status === 200) {
           const historyData = await response.json();
           if (historyData.data && historyData.data.length > 0) {
-            // 将历史消息添加到消息列表
+            // 将历史消息添加到消息列表 - 反转顺序以保持正序显示
             const historyMessages: Message[] = historyData.data.map((msg) => ({
               type: msg.type,
               content: msg.type === 'image' && msg.file ? msg.file.fullUrl : msg.content,
@@ -758,8 +758,8 @@ export const useClassroom = ({ user }:{ user : User }) => {
               senderId: msg.senderId?.toString() || '',
               senderType: msg.senderType as UserType, // 添加senderType字段
               timestamp: msg.timestamp
-            }));
-            
+            })).reverse(); // 反转数组以保持正序显示
+
             setMessageList(prev => [...historyMessages, ...prev]);
             showSystemMessage(`已加载 ${historyData.data.length} 条历史消息`);
           }