Skip to content

Commit 91bb09b

Browse files
committed
fix(chat-ui): 修复账号未选中时的会话加载与实时刷新异常
- 在账号列表已加载但当前账号为空时强制刷新,避免聊天页进入无账号状态 - 为实时增量刷新补充异常兜底和日志,避免单次失败阻断后续更新 - 保留消息归一化中的原始 type/localId 字段,减少前端后续处理丢失信息
1 parent 6aa5b32 commit 91bb09b

3 files changed

Lines changed: 45 additions & 26 deletions

File tree

frontend/composables/chat/useChatMessages.js

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,20 @@ export const useChatMessages = ({
3535
}
3636

3737
const logMessagePhase = (phase, details = {}) => {
38-
if (!isDesktopRenderer()) return
39-
try {
40-
window.wechatDesktop?.logDebug?.('chat-messages', phase, details)
41-
} catch {}
42-
console.info(`[chat-messages] ${phase}`, {
38+
const payload = {
4339
account: String(selectedAccount.value || '').trim(),
4440
selectedUsername: String(selectedContact.value?.username || '').trim(),
4541
activeMessagesFor: String(activeMessagesFor.value || '').trim(),
4642
...details
47-
})
43+
}
44+
45+
if (isDesktopRenderer()) {
46+
try {
47+
window.wechatDesktop?.logDebug?.('chat-messages', phase, payload)
48+
} catch {}
49+
}
50+
51+
console.info(`[chat-messages] ${phase}`, payload)
4852
}
4953

5054
const summarizeRenderTypes = (list) => {
@@ -556,28 +560,38 @@ export const useChatMessages = ({
556560
params.render_types = messageTypeFilter.value
557561
}
558562

559-
const response = await api.listChatMessages(params)
560-
if (selectedContact.value?.username !== username) return
561-
562-
const latest = (response?.messages || []).map(normalizeMessage)
563-
const seenIds = new Set(existing.map((message) => String(message?.id || '')))
564-
const newOnes = []
565-
for (const message of latest) {
566-
const id = String(message?.id || '')
567-
if (!id || seenIds.has(id)) continue
568-
seenIds.add(id)
569-
newOnes.push(message)
570-
}
571-
if (!newOnes.length) return
563+
try {
564+
const response = await api.listChatMessages(params)
565+
if (selectedContact.value?.username !== username) return
566+
567+
const rawMessages = response?.messages || []
568+
const latest = rawMessages.map(normalizeMessage)
569+
570+
const seenIds = new Set(existing.map((message) => String(message?.id || '')))
571+
const newOnes = []
572+
for (const message of latest) {
573+
const id = String(message?.id || '')
574+
if (!id || seenIds.has(id)) continue
575+
seenIds.add(id)
576+
newOnes.push(message)
577+
}
578+
if (!newOnes.length) return
572579

573-
allMessages.value = { ...allMessages.value, [username]: [...existing, ...newOnes] }
580+
allMessages.value = { ...allMessages.value, [username]: [...existing, ...newOnes] }
574581

575-
await nextTick()
576-
const nextContainer = messageContainerRef.value
577-
if (nextContainer && atBottom) {
578-
nextContainer.scrollTop = nextContainer.scrollHeight
582+
await nextTick()
583+
const nextContainer = messageContainerRef.value
584+
if (nextContainer && atBottom) {
585+
nextContainer.scrollTop = nextContainer.scrollHeight
586+
}
587+
updateJumpToBottomState()
588+
} catch (error) {
589+
console.error('[chat-messages] refreshRealtimeIncremental:error', {
590+
account: String(selectedAccount.value || '').trim(),
591+
username: String(username || '').trim(),
592+
error
593+
})
579594
}
580-
updateJumpToBottomState()
581595
}
582596

583597
let realtimeRefreshFuture = null

frontend/composables/chat/useChatSessions.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,11 @@ export const useChatSessions = ({ chatAccounts, selectedAccount, realtimeEnabled
250250
isLoadingContacts.value = true
251251
contactsError.value = ''
252252
try {
253+
const hadLoadedAccountSnapshot = !!chatAccounts.loaded
253254
await chatAccounts.ensureLoaded()
255+
if (!selectedAccount.value && hadLoadedAccountSnapshot) {
256+
await chatAccounts.ensureLoaded({ force: true })
257+
}
254258

255259
if (!selectedAccount.value) {
256260
clearContactsState(chatAccounts.error || '未检测到已解密账号,请先解密数据库。')

frontend/lib/chat/message-normalizer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,10 @@ export const createMessageNormalizer = ({ apiBase, getSelectedAccount, getSelect
178178

179179
return {
180180
id: msg.id,
181+
localId: Number(msg.localId || 0),
181182
serverId: msg.serverId || 0,
182183
serverIdStr,
184+
type: Number(msg.type || 0),
183185
sender,
184186
senderUsername: msg.senderUsername || '',
185187
senderDisplayName: msg.senderDisplayName || '',
@@ -188,7 +190,6 @@ export const createMessageNormalizer = ({ apiBase, getSelectedAccount, getSelect
188190
fullTime: formatMessageFullTime(msg.createTime),
189191
createTime: Number(msg.createTime || 0),
190192
isSent,
191-
type: 'text',
192193
renderType: msg.renderType || 'text',
193194
voipType: msg.voipType || '',
194195
title: msg.title || '',

0 commit comments

Comments
 (0)