Jelajahi Sumber

消息的已读/未读状态2

mazhenhang 1 bulan lalu
induk
melakukan
a1c95a7a57
1 mengubah file dengan 49 tambahan dan 1 penghapusan
  1. 49 1
      LiangZhiYUMao/pages/message/chat.vue

+ 49 - 1
LiangZhiYUMao/pages/message/chat.vue

@@ -440,6 +440,51 @@ export default {
     }
   },
   
+  /**
+   * 页面显示时(从其他页面返回)
+   */
+  async onShow() {
+    console.log('=== 聊天页面显示 ===');
+    
+    // 🔥 重新查询会话的 peerReadTime,更新已读状态
+    // 这样即使 A 离开页面期间 B 阅读了消息,A 返回时也能看到最新的已读状态
+    if (this.conversationID && timManager.tim) {
+      try {
+        console.log('🔄 重新查询会话已读状态...');
+        const conversationRes = await timManager.tim.getConversationProfile(this.conversationID);
+        
+        if (conversationRes && conversationRes.data && conversationRes.data.conversation) {
+          const peerReadTime = conversationRes.data.conversation.peerReadTime;
+          console.log('   - 对方最后阅读时间:', peerReadTime, peerReadTime > 0 ? new Date(peerReadTime * 1000).toLocaleString() : '未读');
+          
+          if (peerReadTime && peerReadTime > 0) {
+            let updatedCount = 0;
+            
+            // 更新所有发送时间 <= peerReadTime 的消息为已读
+            this.messages.forEach((msg, index) => {
+              if (msg.fromUserId === this.userId && !msg.isPeerRead && msg.sendStatus !== 4) {
+                const msgTime = Math.floor(msg.sendTime.getTime() / 1000);
+                
+                if (msgTime <= peerReadTime) {
+                  this.$set(this.messages[index], 'isPeerRead', true);
+                  updatedCount++;
+                  console.log(`   - 消息 ${msg.messageId} 已标记为已读`);
+                }
+              }
+            });
+            
+            console.log(`✅ 页面显示时更新了 ${updatedCount} 条消息为已读状态`);
+          }
+        }
+      } catch (error) {
+        console.error('❌ 重新查询会话已读状态失败:', error);
+      }
+    }
+    
+    // 重新标记当前会话为已读(通知对方)
+    this.markConversationRead();
+  },
+  
   onUnload() {
     // 页面卸载时移除监听
     timManager.offMessage(this.handleNewMessage);
@@ -975,9 +1020,12 @@ export default {
       
       // 监听消息已读回执事件
       const handleMessageReadByPeer = async (event) => {
-        console.log('=== 📖 收到已读回执事件 ===');
+        console.log('=== 📖 收到已读回执事件 MESSAGE_READ_BY_PEER ===');
+        console.log('   - 触发时间:', new Date().toLocaleString());
         console.log('   - 完整事件对象:', event);
         console.log('   - 事件数据:', JSON.stringify(event.data));
+        console.log('   - 当前会话ID:', this.conversationID);
+        console.log('   - 当前用户ID:', this.userId);
         
         // event.data 包含已读的消息列表
         if (event.data && Array.isArray(event.data)) {