AI 客服未登录给出明确提示与登录入口

This commit is contained in:
lik
2026-10-24 21:15:05 +08:00
committed by trae-solo-agent
parent 69a726f84d
commit 177c1159a9
3 changed files with 144 additions and 9 deletions
+73 -6
View File
@@ -6,6 +6,9 @@ Page({
inputValue: '',
inputFocus: false,
isTyping: false,
// 登录态:先判完再渲染提示条,避免静默登录成功前闪一下「未登录」
authChecked: false,
isLoggedIn: false,
scrollToMessage: '',
quickQuestions: [
{ id: '陪诊服务的流程是什么?', text: '陪诊服务的流程是什么?' },
@@ -33,6 +36,7 @@ Page({
onShow() {
this.getAiQuickQuestions()
this.syncLoginState()
// 同步自定义 tabBar 选中态(AI客服为第 1 项)
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
this.getTabBar().setData({
@@ -41,6 +45,30 @@ Page({
}
},
// 每次回到本页都重新对齐登录态:「我的」页登录成功、退出登录都会改变它
async syncLoginState() {
const app = getApp()
if (app.globalData.user?._id) {
this.setData({ authChecked: true, isLoggedIn: true })
return
}
try {
await app.waitForAuth()
} catch (e) {
// 静默登录失败按未登录处理,由提示条给出登录入口
}
this.setData({
authChecked: true,
isLoggedIn: !!app.globalData.user?._id
})
},
// 登录入口在「我的」页(手机号授权按钮),这里只负责把用户送过去
goLogin() {
wx.switchTab({ url: '/pages/mine/mine' })
},
// 获取陪诊服务问题
async getAiQuickQuestions() {
try {
@@ -107,7 +135,14 @@ Page({
case 'error':
case 'system':
if (this.isCurrentRequest(data.id)) {
this.handleAIError(data.content || '服务处理失败,请稍后重试')
const content = data.content || '服务处理失败,请稍后重试'
// 服务端只认 token:回「请先登录」说明本地这枚 token 已不可用,
// 此时「点击重试」必然再失败一次,改成带登录入口的提示
if (content.includes('请先登录')) {
this.handleLoginRequired('登录状态已失效,请重新登录后再试', false)
} else {
this.handleAIError(content)
}
}
break
case 'connected':
@@ -325,14 +360,22 @@ Page({
async sendToAI(content, type, requestId) {
const app = getApp()
// 登录是发消息的前置条件,不是网络/服务故障:单独判定,
// 否则会落进 handleAIError,让用户误以为是「发送失败,点击重试」能解决的问题
let authenticated = false
try {
const authenticated = await app.waitForAuth()
if (!authenticated) throw new Error('请先登录')
authenticated = await app.waitForAuth()
} catch (e) {
authenticated = false
}
const user = app.globalData.user
const token = user?.security?.token || ''
if (!token) throw new Error('请先登录')
const token = app.globalData.user?.security?.token || ''
if (!authenticated || !app.globalData.user?._id || !token) {
this.handleLoginRequired()
return false
}
try {
const socket = app.initChatSocket()
await socket.connect()
await socket.send({
@@ -350,6 +393,30 @@ Page({
}
},
// 未登录/登录失效:给一条说得清的提示并附登录入口,原消息不标成 failed
// (标 failed 会挂上「点击重试」,而这种失败重试必然再失败一次)
// markLoggedOut:本地已确认未登录时同步顶部提示条;服务端判定失效时不动本地会话,
// 避免把网络抖动导致的「查不到人」误当成退出登录
handleLoginRequired(content = '尚未登录,无法与 AI 客服对话', markLoggedOut = true) {
const messages = this.data.messages.map(item => (
item.id === this.activeUserMessageId ? { ...item, status: 'blocked' } : item
))
messages.push({
id: this.createMessageId(),
type: 'system',
contentType: 'login',
content,
time: this.formatTime(new Date())
})
this.resetActiveRequest()
this.setData(markLoggedOut
? { messages, isTyping: false, authChecked: true, isLoggedIn: false }
: { messages, isTyping: false })
this.flushChatHistory()
this.scrollToBottom()
},
handleAIError(errorMessage) {
if (!this.activeRequestId) return