Browse Source

企微聊天定时获取新消息

Long 2 weeks ago
parent
commit
07b214b992
3 changed files with 82 additions and 15 deletions
  1. 13 12
      src/components/LemonUI/components/index.vue
  2. 1 1
      src/layout/index.vue
  3. 68 2
      src/views/qw/qwChat/qq.vue

+ 13 - 12
src/components/LemonUI/components/index.vue

@@ -193,7 +193,7 @@ export default {
         if (message.type == 'event'){
             // if(this.user.id == message.fromUser.id){}
             unread = 0;
-        } 
+        }
         if (messageList === undefined) {
           //console.log("qxj appendMessage messageList:"+messageList+",message"+JSON.stringify(message));
           conversation.id=message.toContactId;
@@ -206,7 +206,7 @@ export default {
           let hasMsg = messageList.some(({id})=>id == message.id);
           if (hasMsg) return;
           this._addMessage(message, message.toContactId, 1);
-          
+
           conversation.lastContent=this.lastContentRender(message);
           conversation.lastSendTime=message.sendTime;
           if (message.toContactId == this.currentContactId) {
@@ -234,7 +234,7 @@ export default {
     },
     _handleSend(text) {
       const message = this._createMessage({ content: text });
-      this.appendMessage(message, true);
+      // this.appendMessage(message, true);
       this._emitSend(message, () => {
         this.updateContact({
           id: message.toContactId,
@@ -528,7 +528,7 @@ export default {
             </div>,
           );
       }
-      
+
       //聊天消息View
       nodes.push(
         <div
@@ -692,7 +692,7 @@ export default {
         editorValue,
         lastContent,
       });
-      
+
       var tempDraft=this.CacheDraft.get(cid);
       //console.log("qxj editorValue:"+editorValue+",tempDraft:"+JSON.stringify(tempDraft)+"lastContent:"+lastContent);
 
@@ -762,13 +762,15 @@ export default {
       if (!allMessages[contactId]) {
           this.updateCurrentMessages();
           this._emitPullMessages(isEnd => {
+            setTimeout(() => {
               this.messageViewToBottom();
+            }, 10);
           });
       } else {
-          setTimeout(() => {
-              this.updateCurrentMessages();
-              this.messageViewToBottom();
-          }, 0);
+        this.updateCurrentMessages();
+        setTimeout(() => {
+            this.messageViewToBottom();
+        }, 10);
       }
     },
     /**
@@ -926,7 +928,6 @@ export default {
     initConversations(data) {
       this.conversations = data;
       this.sortConversations();
-      
     },
 
     /**
@@ -1017,7 +1018,7 @@ export default {
      */
     updateContact(data) {
       const conversationId = data.conversationId;
-      const index = this.findConversationIndexById(conversationId);  
+      const index = this.findConversationIndexById(conversationId);
       if (index !== -1) {
         // if(isString(data.unread)) {
         //     if (unread.indexOf("+") === 0 || unread.indexOf("-") === 0) {
@@ -1033,7 +1034,7 @@ export default {
     updateContact1(data) {
       const contactId = data.id;
       delete data.id;
-      const index = this.findContactIndexById(contactId);  
+      const index = this.findContactIndexById(contactId);
       if (index !== -1) {
         const { unread } = data;
         if (isString(unread)) {

+ 1 - 1
src/layout/index.vue

@@ -52,7 +52,7 @@
 
     <el-dialog append-to-body width="1200px" :visible.sync="qw.open" :title="qw.title" :show-close="false">
       <div class="qw-im-content">
-        <QwIM/>
+        <QwIM :showQw="qw.open"/>
       </div>
     </el-dialog>
   </div>

+ 68 - 2
src/views/qw/qwChat/qq.vue

@@ -99,6 +99,9 @@ export default {
     VideoPlayer,
     UserDetail
   },
+  props: {
+    showQw: Boolean
+  },
   data() {
     return {
       contactName:'',
@@ -148,7 +151,10 @@ export default {
       detail: {
         title: '',
         open: false
-      }
+      },
+      messagePollingTimer: null, // 定时器引用
+      pollingInterval: 5000,     // 轮询间隔(毫秒)
+      lastMessageIdMap: {},      // 记录每个会话最后一条消息ID
     };
   },
   created(){
@@ -165,6 +171,19 @@ export default {
       }
     });
   },
+  watch: {
+    showQw(nv, ov) {
+      if (nv) {
+        this.$nextTick(() => {
+          this.$refs.IMUI.messageViewToBottom();
+        });
+        this.startMessagePolling()
+      }
+      else {
+        this.stopMessagePolling();
+      }
+    }
+  },
   mounted() {
     this.$watch('appKey', (newValue, oldValue) => {
       if (newValue) {
@@ -185,7 +204,12 @@ export default {
       }
     ]);
     IMUI.initEmoji(EmojiData);
+    IMUI.initEditorTools([])
 
+    this.startMessagePolling(); // 启动定时轮询
+  },
+  beforeDestroy() {
+    this.stopMessagePolling();
   },
   methods: {
     // 切换企微账号
@@ -206,6 +230,12 @@ export default {
       const IMUI = this.$refs.IMUI;
       getConversations(this.qwUser.id).then(response => {
         this.conversationData = response.data;
+        // 初始化msgId
+        if (Array.isArray(response.data)) {
+          response.data.forEach(conversation => {
+            this.lastMessageIdMap[conversation.conversationId] = conversation.msgId || 0;
+          });
+        }
         IMUI.initConversations(response.data);
         const fstConversation = this.conversationData[0];
         if(fstConversation) {
@@ -459,7 +489,43 @@ export default {
       setTimeout(() => {
         this.$refs.userDetail.getDetail(sessionId);
       }, 1);
-    }
+    },
+    startMessagePolling() {
+      this.stopMessagePolling(); // 避免重复启动
+      this.messagePollingTimer = setInterval(() => {
+        this.fetchAllConversationsLatestMessages();
+      }, this.pollingInterval);
+    },
+    stopMessagePolling() {
+      if (this.messagePollingTimer) {
+        clearInterval(this.messagePollingTimer);
+        this.messagePollingTimer = null;
+      }
+    },
+    fetchAllConversationsLatestMessages() {
+      if (!this.qwUser || !this.conversationData) return;
+      this.conversationData.forEach(conversation => {
+        const lastMsgId = this.lastMessageIdMap[conversation.conversationId] || 0;
+        const params = {
+          pageNum: 1,
+          pageSize: 20,
+          conversationId: conversation.conversationId,
+          userId: this.qwUser.id,
+          msgId: lastMsgId // 用消息ID拉取新消息
+        };
+        getMessageList(params).then(response => {
+          if (response.code === 200 && response.data && response.data.list) {
+            response.data.list.forEach(msg => {
+              // 更新lastMessageIdMap
+              if (!this.lastMessageIdMap[conversation.conversationId] || msg.id > this.lastMessageIdMap[conversation.conversationId]) {
+                this.lastMessageIdMap[conversation.conversationId] = msg.id;
+              }
+              this.appendMessageAction(msg);
+            });
+          }
+        });
+      });
+    },
   },
 };
 </script>