From de7af347f6570a62805aa46aab6792716fdc1369 Mon Sep 17 00:00:00 2001 From: gaoxq <376340421@qq.com> Date: Thu, 2 Apr 2026 18:04:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=91=E6=96=87=E4=BB=B6=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E4=B8=8A=E4=BC=A0=E7=BB=84=E4=BB=B6=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web-vue/src/components/ChatDialog.vue | 48 +++++++++++++++++++++------ 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/web-vue/src/components/ChatDialog.vue b/web-vue/src/components/ChatDialog.vue index d78060f..8531e31 100644 --- a/web-vue/src/components/ChatDialog.vue +++ b/web-vue/src/components/ChatDialog.vue @@ -429,9 +429,23 @@ const triggerImageUpload = () => { if (!uploading.value) imageInputRef.value?.cl const triggerFileUpload = () => { if (!uploading.value) fileInputRef.value?.click() } const pushMessage = (msg) => { - const contactId = msg.isSelf ? msg.toUserId : msg.fromUserId + // 确定消息应该放在哪个联系人下 + let contactId + if (msg.isSelf) { + // 自己发送的消息,放到接收方(toUserId)下 + contactId = msg.toUserId + } else { + // 收到的消息,放到发送方(fromUserId)下 + contactId = msg.fromUserId + } + if (!messages.value[contactId]) messages.value[contactId] = [] - messages.value[contactId].push(msg) + // 使用唯一 key 避免重复 + const msgKey = msg.tempType ? `temp_${msg.id}` : msg.id + const exists = messages.value[contactId].some(m => (m.tempType ? `temp_${m.id}` : m.id) === msgKey) + if (!exists) { + messages.value[contactId].push(msg) + } } const handleImageSelect = async (e) => { @@ -581,14 +595,24 @@ const onDialogOpen = () => { unsubscribeWs = chatService.onMessage((data) => { if (data.type === 'chat') { const msg = data.message - const isSelf = Number(msg.fromUserId) === Number(userStore.userId) + const currentUserId = Number(userStore.userId) + const msgFromUserId = Number(msg.fromUserId) + const msgToUserId = Number(msg.toUserId) + + // 判断是否是自己发送的消息 + const isSelf = (msgFromUserId === currentUserId) msg.isSelf = isSelf msg.sending = false msg.failed = false - msg.fromColor = colors[Math.abs(Number(msg.fromUserId) % colors.length)] - const targetUserId = isSelf ? msg.toUserId : msg.fromUserId - if (!messages.value[targetUserId]) messages.value[targetUserId] = [] - const list = messages.value[targetUserId] + msg.fromColor = colors[Math.abs(msgFromUserId % colors.length)] + + // 确定消息放在哪个联系人下 + // 自己发的消息放到接收方(toUserId)下,收到的消息放到发送方(fromUserId)下 + const contactId = isSelf ? msgToUserId : msgFromUserId + + if (!messages.value[contactId]) messages.value[contactId] = [] + const list = messages.value[contactId] + // 清除同类型的临时消息(sending 状态的) if (isSelf && (msg.type === 'image' || msg.type === 'file')) { for (let i = list.length - 1; i >= 0; i--) { @@ -598,13 +622,17 @@ const onDialogOpen = () => { } } } - // 去重 - const exists = list.some(m => m.id === msg.id) + + // 去重(根据消息 id 或临时消息的 tempType) + const msgKey = msg.tempType ? `temp_${msg.id}` : msg.id + const exists = list.some(m => (m.tempType ? `temp_${m.id}` : m.id) === msgKey) if (!exists) list.push(msg) + // 滚动 - if (currentContact.value && Number(currentContact.value.id) === Number(targetUserId)) { + if (currentContact.value && Number(currentContact.value.id) === contactId) { nextTick(() => scrollToBottom()) } + // 更新最近聊天 if (!isSelf) { const contact = contacts.value.find(c => Number(c.id) === Number(msg.fromUserId))