修复了一些逻辑漏洞
This commit is contained in:
@@ -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 || '删除失败')
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user