From 3f34df2a9fb406bf631b8e23ef1c0995a00cec31 Mon Sep 17 00:00:00 2001 From: liqi Date: Fri, 5 Dec 2025 16:35:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E6=BC=8F=E6=B4=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/views/chat/ChatView.vue | 99 +++++++++++++++++++++++++++++++++++-- 1 file changed, 94 insertions(+), 5 deletions(-) diff --git a/src/views/chat/ChatView.vue b/src/views/chat/ChatView.vue index 17c1dad..4f59152 100644 --- a/src/views/chat/ChatView.vue +++ b/src/views/chat/ChatView.vue @@ -765,6 +765,68 @@ async function sendMessage(type: number, content: string, extra: any = {}, durat const roomId = getRoomId(chatStore.currentTarget) const isGroup = chatStore.currentTarget.is_group || chatStore.currentTarget.room_type === 'group' + // 发送消息前,确保会话存在(如果不存在则创建) + const existingConv = conversationStore.conversations.find(c => + isGroup + ? c.room_id === roomId + : (c.target_id === (chatStore.currentTarget?.user_id || chatStore.currentTarget?.id)) + ) + + if (!existingConv) { + try { + // 调用接口创建会话 + const conv: any = await conversationApi.getConversationByRoom(roomId) + // 检查会话是否已存在(可能在其他地方已添加) + const exists = conversationStore.conversations.find(c => + c.room_id === conv.room_id || + (c.target_id === conv.target_id && c.type === conv.type) + ) + if (!exists) { + const isGroupConv = conv.type === 2 + const targetGroup = conv.room || conv.target_group + + const formattedConv: Conversation = { + id: conv.id, + target_id: conv.target_id, + room_id: conv.room_id, + type: conv.type, + is_group: isGroupConv, + is_top: conv.is_top || false, + is_muted: conv.is_muted || false, + unread_count: conv.unread_count || 0, + last_message: conv.last_message || '', + last_message_time: conv.last_time, + name: isGroupConv + ? (targetGroup?.room_name || targetGroup?.name || conv.name || conv.room_id || '群聊') + : (conv.target_user?.name || conv.name || '未知用户'), + display_name: isGroupConv + ? (targetGroup?.room_name || targetGroup?.name || conv.name || conv.room_id || '群聊') + : (conv.target_user?.name || conv.name || '未知用户'), + avatar: isGroupConv + ? (targetGroup?.room_avatar || targetGroup?.avatar || conv.avatar || '') + : (conv.target_user?.avatar || conv.avatar || ''), + user: conv.target_user, + room: conv.room, + target_group: targetGroup ? { + id: targetGroup.room_id || targetGroup.id, + room_id: targetGroup.room_id, + room_type: 'group', + name: targetGroup.room_name || targetGroup.name, + avatar: targetGroup.room_avatar || targetGroup.avatar, + owner_id: targetGroup.owner_id, + member_count: targetGroup.member_count, + created_at: targetGroup.created_at, + } : undefined + } + conversationStore.conversations.push(formattedConv) + conversationStore.sortConversations() + } + } catch (error) { + // 如果创建会话失败,仍然继续发送消息(后端会在发送消息时创建会话) + console.warn('Failed to create conversation before sending message:', error) + } + } + // 群聊时 receiver_user_id 应该为空或群ID,确保后端能正确识别为群聊 const receiverUserId = isGroup ? '' : (chatStore.currentTarget.user_id || chatStore.currentTarget.id) @@ -1106,13 +1168,40 @@ async function handleMarkRead(conv: Conversation) { toastStore.success('已标记为已读') } -function handleDeleteConversation() { +async function handleDeleteConversation() { if (!pendingDeleteConversation.value) return const conv = pendingDeleteConversation.value - conversationStore.conversations = conversationStore.conversations.filter(c => c.id !== conv.id) - toastStore.success('会话已移除') - showDeleteConversationConfirm.value = false - pendingDeleteConversation.value = null + + try { + // 调用后端API真正删除数据库中的会话记录 + await conversationApi.deleteConversation(conv.target_id) + + // 从前端列表中移除会话 + conversationStore.conversations = conversationStore.conversations.filter(c => c.id !== conv.id) + + // 如果当前正在查看被删除的会话,关闭聊天窗口 + if (chatStore.currentTarget) { + const currentRoomId = getRoomId(chatStore.currentTarget) + const convRoomId = conv.room_id || conv.target_id + if (currentRoomId === convRoomId || + (conv.type === 2 && currentRoomId === conv.room_id) || + (!conv.is_group && (chatStore.currentTarget.user_id === conv.target_id || chatStore.currentTarget.id === conv.target_id))) { + chatStore.setCurrentTarget(null) + storage.setSelectedConversation('') + storage.setSelectedRoomId('') + if (isMobile.value) { + chatVisible.value = false + } + } + } + + toastStore.success('会话已删除') + showDeleteConversationConfirm.value = false + pendingDeleteConversation.value = null + } catch (e: any) { + console.error('Failed to delete conversation:', e) + toastStore.error(e?.response?.data?.message || e?.message || '删除失败') + } } // ----------------------------------------------