瀏覽代碼

fix:修复移除群聊后还能收到消息的bug

qiao 1 月之前
父節點
當前提交
eb7170f6a2
共有 1 個文件被更改,包括 27 次插入26 次删除
  1. 27 26
      src/stores/chat.js

+ 27 - 26
src/stores/chat.js

@@ -1,3 +1,4 @@
+
 export const useChatStore = defineStore('chat', () => {
 
     const config = useRuntimeConfig()
@@ -15,9 +16,9 @@ export const useChatStore = defineStore('chat', () => {
     const localMessages = ref([])
     const onNewMessage = ref(Date.now())
     const onPlayMsgAudio = ref(Date.now())//消息提示音
-     // 会话列表
-     const chatList = ref([])
-     const conversations = computed(() => chatList.value)
+    // 会话列表
+    const chatList = ref([])
+    const conversations = computed(() => chatList.value)
     // 当前正在聊天的会话
     const curConversiton = ref({})
     // 最新消息的会话
@@ -49,12 +50,12 @@ export const useChatStore = defineStore('chat', () => {
                 console.log('接收到消息:', e.data)
                 await reqChatList()
 
-                onNewMessage.value = Date.now() + '' + Math.random() * 1000000
+                onNewMessage.value = Date.now() + '' + Math.random() * 10000
                 try {
                     const messageBody = JSON.parse(e.data)
                     console.log('messageBody', messageBody)
-                    for(let i = 0; i < conversations.value.length; i++){
-                        if(messageBody.groupId == conversations.value[i].groupId){
+                    for (let i = 0; i < conversations.value.length; i++) {
+                        if (messageBody.groupId == conversations.value[i].groupId) {
                             newMsgConversation.value = conversations.value[i]
                             break
                         }
@@ -75,7 +76,8 @@ export const useChatStore = defineStore('chat', () => {
                         }
                         console.log('isReceive', isReceive)
                         // 如果是收到的消息,则将消息push到receive数组中
-                        if (isReceive) {
+                        const isExist = chatList.value.some(item => messageBody.groupId == item.groupId)
+                        if (isReceive && isExist) {
                             onPlayMsgAudio.value = Date.now() + '' + Math.random() * 1000000
                             receive.value.push(messageBody)
                         }
@@ -98,20 +100,19 @@ export const useChatStore = defineStore('chat', () => {
     }
 
     // 关闭连接
-    function close(){
+    function close() {
         ws.value.close()
     }
-
-   
+    
     // 请求聊天会话列表
     const reqChatListLoading = ref(false)
     async function reqChatList() {
 
-        if(reqChatListLoading.value) return
+        if (reqChatListLoading.value) return
 
         reqChatListLoading.value = true
 
-        const { data } = await request('/website/tourism/fans/getTourMemberList').finally(()=>reqChatListLoading.value = false)
+        const { data } = await request('/website/tourism/fans/getTourMemberList').finally(() => reqChatListLoading.value = false)
         const { list } = data || {}
         if (Array.isArray(list)) {
             // 找到当前会话,清除未读消息
@@ -129,21 +130,21 @@ export const useChatStore = defineStore('chat', () => {
     const messages = ref('first message')
 
     const newMessageLoading = ref(false)
-    async function getNewMessage(groupId,messageId){
-        console.log(groupId,messageId)
+    async function getNewMessage(groupId, messageId) {
+        console.log(groupId, messageId)
 
-        if(newMessageLoading.value) return
-        
-        if(!groupId || !messageId) return
+        if (newMessageLoading.value) return
 
-        const query={
-            groupId,messageId
+        if (!groupId || !messageId) return
+
+        const query = {
+            groupId, messageId
         }
         newMessageLoading.value = true
-        const {data} = await request('/website/tourMessage/getLatestMessage',{query}).finally(()=>newMessageLoading.value=false)
+        const { data } = await request('/website/tourMessage/getLatestMessage', { query }).finally(() => newMessageLoading.value = false)
         localMessages.value = []
-        receive.value = [...receive.value,...data]
-        console.log('新消息:',res)
+        receive.value = [...receive.value, ...data]
+        console.log('新消息:', res)
     }
 
     /**
@@ -183,7 +184,7 @@ export const useChatStore = defineStore('chat', () => {
 
         if (!id) return
 
-        if(fansStatus != 0 && fansStatus != 1 && fansStatus != 2 && fansStatus != 4) return
+        if (fansStatus != 0 && fansStatus != 1 && fansStatus != 2 && fansStatus != 4) return
 
         // 确保每次操作请求结束后 再执行下一次操作
         if (followLoadding.value) return
@@ -193,7 +194,7 @@ export const useChatStore = defineStore('chat', () => {
             lock: true,
             background: 'rgba(0, 0, 0, 0.2)'
         })
-        await request('/website/tourism/fans/saveConcern', { method: 'post', body: { attentionId: id } }).finally(() =>{
+        await request('/website/tourism/fans/saveConcern', { method: 'post', body: { attentionId: id } }).finally(() => {
             followLoadding.value = false
             ElLoading.service().close()
         })
@@ -218,12 +219,12 @@ export const useChatStore = defineStore('chat', () => {
         if (noticeType == 1 && !getUserId) return ElMessage.error('缺少必要参数')
 
         if (!sendUserId || !groupId) return ElMessage.error('缺少必要参数')
-        
+
         ElLoading.service({
             lock: true,
             background: 'rgba(0, 0, 0, 0.3)'
         })
-        await request('/website/tourGroup/createMember', { method: 'post', body: data }).finally(()=>ElLoading.service().close())
+        await request('/website/tourGroup/createMember', { method: 'post', body: data }).finally(() => ElLoading.service().close())
 
         await reqChatList()