fix(ai): 登录失效后静默重登并重发,清理历史登录提示

This commit is contained in:
lik
2026-11-08 14:35:09 +08:00
parent c8434c87b5
commit a132ce3da7
+72 -7
View File
@@ -74,6 +74,9 @@ Page({
this.voiceFrames = []
this.voiceStopFile = ''
this.voiceBaseText = ''
// 本轮请求现场与「已为重登重试过」的请求 id:服务端回「请先登录」时原样重发
this.activePayload = null
this.authRetriedRequestId = ''
this.loadChatHistory()
this.initSocketListeners()
this.initRecorder()
@@ -116,6 +119,7 @@ Page({
const app = getApp()
if (app.globalData.user?._id) {
this.setData({ authChecked: true, isLoggedIn: true })
this.clearStaleLoginMessages()
return
}
@@ -124,10 +128,22 @@ Page({
} catch (e) {
// 静默登录失败按未登录处理,由提示条给出登录入口
}
const loggedIn = !!app.globalData.user?._id
this.setData({
authChecked: true,
isLoggedIn: !!app.globalData.user?._id
isLoggedIn: loggedIn
})
if (loggedIn) this.clearStaleLoginMessages()
},
// 登录恢复后清掉历史里残留的「登录状态已失效」系统提示:
// 它会随聊天记录一起持久化,再次进入时回放出来,看起来像现在仍要重新登录
clearStaleLoginMessages() {
const messages = this.data.messages || []
const next = messages.filter(item => !(item.type === 'system' && item.contentType === 'login'))
if (next.length === messages.length) return
this.setData({ messages: next })
this.saveChatHistory(next)
},
// 登录入口在「我的」页(手机号授权按钮),这里只负责把用户送过去
@@ -225,9 +241,10 @@ Page({
if (this.isCurrentRequest(data.id)) {
const content = data.content || '服务处理失败,请稍后重试'
// 服务端只认 token:回「请先登录」说明本地这枚 token 已不可用,
// 此时「点击重试」必然再失败一次,改成带登录入口的提示
// 先静默重登并原样重发(后端重启后旧 token 作废是常见场景),
// 重登失败才落带登录入口的提示
if (content.includes('请先登录')) {
this.handleLoginRequired('登录状态已失效,请重新登录后再试', false)
this.handleAuthExpired()
} else {
this.handleAIError(content)
}
@@ -310,6 +327,8 @@ Page({
this.activeUserMessageId = ''
this.activeServerId = ''
this.activeRequestType = ''
// 请求已结束,不再保留提问现场(图片原文可能很大)
this.activePayload = null
},
beginRequest(requestId, userMessageId = '', type = 'chat') {
@@ -989,19 +1008,23 @@ Page({
},
// 流式失败(配置缺失 / 供应商异常 / 断流):不再中途弹错,悄悄降级
handleAsrError(data) {
async handleAsrError(data) {
const stream = this.voiceStream
if (!stream || stream.settled || !data || data.id !== stream.id) return
this.clearVoiceFinishTimer()
const message = String(data.content || '语音识别失败,请稍后重试')
// 登录失效不再走兜底:整段接口同样会 401,只会多花一次上传
// 登录失效不再走兜底:整段接口同样会 401,只会多花一次上传。
// 先静默重登(后端重启后旧 token 会整体作废),恢复后提示用户重新录音
if (/请先登录/.test(message)) {
this.stopRecorderSilently()
this.resetVoiceState(true)
this.setData({ isLoggedIn: false })
wx.showToast({ title: '登录状态已失效,请重新登录', icon: 'none' })
if (await this.reauthSilently()) {
wx.showToast({ title: '登录已恢复,请重新录音', icon: 'none' })
} else {
wx.showToast({ title: '登录状态已失效,请重新登录', icon: 'none' })
}
return
}
@@ -1388,6 +1411,8 @@ Page({
async sendToAI(content, type, requestId) {
const app = getApp()
// 留一份本轮请求现场:服务端回「请先登录」时用它换新 token 原样重发
this.activePayload = { content, type, requestId }
// 登录是发消息的前置条件,不是网络/服务故障:单独判定,
// 否则会落进 handleAIError,让用户误以为是「发送失败,点击重试」能解决的问题
@@ -1422,6 +1447,46 @@ Page({
}
},
// 服务端只认 token(api/health/websocket.js):本地 globalData 还是登录态、
// 但这枚 token 已被服务端作废时(后端重启/换库后旧 token 全部失效),
// app.onShow 因 user 仍在而不会重签,WS 也没有 HTTP 那层 401 兜底。
// 这里统一补一次静默重登,成功返回 true 并同步顶部提示条
async reauthSilently() {
const app = getApp()
try {
const ok = await app.silentSignin()
if (ok && app.globalData.user?.security?.token) {
this.setData({ authChecked: true, isLoggedIn: true })
this.clearStaleLoginMessages()
return true
}
} catch (err) {
console.log('静默重登失败', err)
}
this.setData({ isLoggedIn: false })
return false
},
// 聊天 WS 回「请先登录」:先静默重登换新 token,再把同一问原样重发,
// 用户无感;重登失败(或重发后仍被拒)才落登录提示。
// 同一条请求只重试一次,避免服务端持续判未登录时死循环
async handleAuthExpired() {
const payload = this.activePayload
if (!payload || this.authRetriedRequestId === payload.requestId) {
this.handleLoginRequired('登录状态已失效,请重新登录后再试', false)
return
}
this.authRetriedRequestId = payload.requestId
if (!(await this.reauthSilently())) {
this.handleLoginRequired('登录状态已失效,请重新登录后再试', false)
return
}
// requestId 保持不变:重发的流式回复仍落在这条提问下
await this.sendToAI(payload.content, payload.type, payload.requestId)
},
// 未登录/登录失效:给一条说得清的提示并附登录入口,原消息不标成 failed
// (标 failed 会挂上「点击重试」,而这种失败重试必然再失败一次)
// markLoggedOut:本地已确认未登录时同步顶部提示条;服务端判定失效时不动本地会话,