fix(chat-ui): 修复账号未选中时的会话加载与实时刷新异常

- 在账号列表已加载但当前账号为空时强制刷新,避免聊天页进入无账号状态
- 为实时增量刷新补充异常兜底和日志,避免单次失败阻断后续更新
- 保留消息归一化中的原始 type/localId 字段,减少前端后续处理丢失信息
This commit is contained in:
2977094657
2026-03-22 15:34:05 +08:00
Unverified
parent a01dba4a93
commit bb2f4b2686
3 changed files with 46 additions and 27 deletions
+40 -26
View File
@@ -35,16 +35,20 @@ export const useChatMessages = ({
}
const logMessagePhase = (phase, details = {}) => {
if (!isDesktopRenderer()) return
try {
window.wechatDesktop?.logDebug?.('chat-messages', phase, details)
} catch {}
console.info(`[chat-messages] ${phase}`, {
const payload = {
account: String(selectedAccount.value || '').trim(),
selectedUsername: String(selectedContact.value?.username || '').trim(),
activeMessagesFor: String(activeMessagesFor.value || '').trim(),
...details
})
}
if (isDesktopRenderer()) {
try {
window.wechatDesktop?.logDebug?.('chat-messages', phase, payload)
} catch {}
}
console.info(`[chat-messages] ${phase}`, payload)
}
const summarizeRenderTypes = (list) => {
@@ -556,28 +560,38 @@ export const useChatMessages = ({
params.render_types = messageTypeFilter.value
}
const response = await api.listChatMessages(params)
if (selectedContact.value?.username !== username) return
try {
const response = await api.listChatMessages(params)
if (selectedContact.value?.username !== username) return
const latest = (response?.messages || []).map(normalizeMessage)
const seenIds = new Set(existing.map((message) => String(message?.id || '')))
const newOnes = []
for (const message of latest) {
const id = String(message?.id || '')
if (!id || seenIds.has(id)) continue
seenIds.add(id)
newOnes.push(message)
const rawMessages = response?.messages || []
const latest = rawMessages.map(normalizeMessage)
const seenIds = new Set(existing.map((message) => String(message?.id || '')))
const newOnes = []
for (const message of latest) {
const id = String(message?.id || '')
if (!id || seenIds.has(id)) continue
seenIds.add(id)
newOnes.push(message)
}
if (!newOnes.length) return
allMessages.value = { ...allMessages.value, [username]: [...existing, ...newOnes] }
await nextTick()
const nextContainer = messageContainerRef.value
if (nextContainer && atBottom) {
nextContainer.scrollTop = nextContainer.scrollHeight
}
updateJumpToBottomState()
} catch (error) {
console.error('[chat-messages] refreshRealtimeIncremental:error', {
account: String(selectedAccount.value || '').trim(),
username: String(username || '').trim(),
error
})
}
if (!newOnes.length) return
allMessages.value = { ...allMessages.value, [username]: [...existing, ...newOnes] }
await nextTick()
const nextContainer = messageContainerRef.value
if (nextContainer && atBottom) {
nextContainer.scrollTop = nextContainer.scrollHeight
}
updateJumpToBottomState()
}
let realtimeRefreshFuture = null
@@ -250,7 +250,11 @@ export const useChatSessions = ({ chatAccounts, selectedAccount, realtimeEnabled
isLoadingContacts.value = true
contactsError.value = ''
try {
const hadLoadedAccountSnapshot = !!chatAccounts.loaded
await chatAccounts.ensureLoaded()
if (!selectedAccount.value && hadLoadedAccountSnapshot) {
await chatAccounts.ensureLoaded({ force: true })
}
if (!selectedAccount.value) {
clearContactsState(chatAccounts.error || '未检测到已解密账号,请先解密数据库。')
+2 -1
View File
@@ -178,8 +178,10 @@ export const createMessageNormalizer = ({ apiBase, getSelectedAccount, getSelect
return {
id: msg.id,
localId: Number(msg.localId || 0),
serverId: msg.serverId || 0,
serverIdStr,
type: Number(msg.type || 0),
sender,
senderUsername: msg.senderUsername || '',
senderDisplayName: msg.senderDisplayName || '',
@@ -188,7 +190,6 @@ export const createMessageNormalizer = ({ apiBase, getSelectedAccount, getSelect
fullTime: formatMessageFullTime(msg.createTime),
createTime: Number(msg.createTime || 0),
isSent,
type: 'text',
renderType: msg.renderType || 'text',
voipType: msg.voipType || '',
title: msg.title || '',