Prechádzať zdrojové kódy

feat: 对接聊天功能

qinyuyue 2 mesiacov pred
rodič
commit
2571d2ba07

+ 10 - 5
src/pages/chat/group-chat.vue

@@ -124,8 +124,7 @@ const getChatList = async (type = 'init') => {
     })
     pageNum.value = page;
     await handleResponse(res);
-    currConversationChatList.value = handleChatList(res.data?.data)
-    console.log(currConversationChatList.value, 'currConversationChatList')
+    currConversationChatList.value = chatsStore.handleMessageList(res.data?.data)
     if (type === 'init') await scrollToBottom()
   } catch (e) {
     console.error(e)
@@ -271,11 +270,17 @@ onMounted(() => {
   initGroupId()
 
   XYWebSocket.SocketEventsBus.on(XYWebSocket.SocketEvents.chatEvent, async (chat) => {
+    console.log('订阅群聊消息', chat)
     const isCurrGroupId = chat.groupId && chat.groupId === groupId.value;
     const isOtherUserMessage = chat.sendUserId && chatsStore.isRealMessage(chat.sendUserId);
-    if (isCurrGroupId && isOtherUserMessage) {
-      currConversationChatList.value.push(chat)
-      await scrollToBottom()
+    if(isCurrGroupId) {
+      if (isOtherUserMessage) {
+        currConversationChatList.value.push(chat)
+        await scrollToBottom()
+      }
+      if(!isOtherUserMessage) {
+        await getChatList('init')
+      }
     }
   })
 })

+ 10 - 5
src/pages/chat/single-chat.vue

@@ -146,7 +146,6 @@ const sendTextMessage = async (text) => {
     currConversationChatList.value.push(msg)
     await scrollToBottom()
     await chatsStore.sendSocketMessage(msg)
-    await getChatList('init')
   } catch (e) {
     console.log(e, '2')
   } finally {
@@ -267,13 +266,19 @@ function getLocalId() {
 onMounted(() => {
   initGroupId()
   XYWebSocket.SocketEventsBus.on(XYWebSocket.SocketEvents.chatEvent, async (chat) => {
-    console.log(chat, '单聊页面消息订阅')
+    console.log('单聊页面消息订阅', chat)
     const isCurrGroupId = chat.groupId && chat.groupId === groupId.value;
     const isOtherUserMessage = chat.sendUserId && chatsStore.isRealMessage(chat.sendUserId)
-    if (isCurrGroupId && isOtherUserMessage) {
-      currConversationChatList.value.push(chat)
-      await scrollToBottom()
+    if(isCurrGroupId) {
+      if (isOtherUserMessage) {
+        currConversationChatList.value.push(chat)
+        await scrollToBottom()
+      }
+      if(!isOtherUserMessage) {
+        await getChatList('init')
+      }
     }
+
   })
 })
 

+ 1 - 3
src/utils/XYWebSocket.ts

@@ -44,11 +44,9 @@ export namespace XYWebSocket {
                 if (!e) return;
                 if (!e.data) return;
                 const messagePackage = JSON.parse(e.data) || {};
+                SocketEventsBus.emit(SocketEvents.chatEvent, messagePackage)
                 console.log('---------接收---------', messagePackage, '-----------消息包------');
                 const {web_request_id = null} = messagePackage.map || {};
-
-                SocketEventsBus.emit(SocketEvents.chatEvent, messagePackage)
-
                 // 处理自己本地发送的消息
                 if (web_request_id && this.eventPoll.has(web_request_id)) {
                     const eventItem = this.eventPoll.get(web_request_id)!;