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: '', inputValue: '',
inputFocus: false, inputFocus: false,
isTyping: false, isTyping: false,
// 登录态:先判完再渲染提示条,避免静默登录成功前闪一下「未登录」
authChecked: false,
isLoggedIn: false,
scrollToMessage: '', scrollToMessage: '',
quickQuestions: [ quickQuestions: [
{ id: '陪诊服务的流程是什么?', text: '陪诊服务的流程是什么?' }, { id: '陪诊服务的流程是什么?', text: '陪诊服务的流程是什么?' },
@@ -33,6 +36,7 @@ Page({
onShow() { onShow() {
this.getAiQuickQuestions() this.getAiQuickQuestions()
this.syncLoginState()
// 同步自定义 tabBar 选中态(AI客服为第 1 项) // 同步自定义 tabBar 选中态(AI客服为第 1 项)
if (typeof this.getTabBar === 'function' && this.getTabBar()) { if (typeof this.getTabBar === 'function' && this.getTabBar()) {
this.getTabBar().setData({ 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() { async getAiQuickQuestions() {
try { try {
@@ -107,7 +135,14 @@ Page({
case 'error': case 'error':
case 'system': case 'system':
if (this.isCurrentRequest(data.id)) { 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 break
case 'connected': case 'connected':
@@ -325,14 +360,22 @@ Page({
async sendToAI(content, type, requestId) { async sendToAI(content, type, requestId) {
const app = getApp() const app = getApp()
// 登录是发消息的前置条件,不是网络/服务故障:单独判定,
// 否则会落进 handleAIError,让用户误以为是「发送失败,点击重试」能解决的问题
let authenticated = false
try { try {
const authenticated = await app.waitForAuth() authenticated = await app.waitForAuth()
if (!authenticated) throw new Error('请先登录') } catch (e) {
authenticated = false
}
const user = app.globalData.user const token = app.globalData.user?.security?.token || ''
const token = user?.security?.token || '' if (!authenticated || !app.globalData.user?._id || !token) {
if (!token) throw new Error('请先登录') this.handleLoginRequired()
return false
}
try {
const socket = app.initChatSocket() const socket = app.initChatSocket()
await socket.connect() await socket.connect()
await socket.send({ 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) { handleAIError(errorMessage) {
if (!this.activeRequestId) return if (!this.activeRequestId) return
+18 -2
View File
@@ -1,4 +1,11 @@
<view class="chat-container"> <view class="chat-container">
<!-- 未登录提示条:提前说明「能不能聊」,把登录入口放在这里,
用户不必等打完字被拦下来才知道要先登录 -->
<view class="login-notice" wx:if="{{authChecked && !isLoggedIn}}">
<text class="login-notice-text">尚未登录,暂时无法与 AI 客服对话</text>
<text class="login-notice-link" bindtap="goLogin" aria-role="link">去登录</text>
</view>
<!-- 消息列表区域 --> <!-- 消息列表区域 -->
<scroll-view <scroll-view
class="message-list" class="message-list"
@@ -35,8 +42,17 @@
wx:key="id" wx:key="id"
id="msg-{{item.id}}" id="msg-{{item.id}}"
> >
<!-- 连接或服务错误是系统状态,不使用用户/AI 头像,也不带 AI 内容声明。 --> <!-- 连接或服务错误是系统状态,不使用用户/AI 头像,也不带 AI 内容声明。
<text wx:if="{{item.type === 'system'}}" class="system-message">{{item.content}}</text> 未登录提示额外带一个登录入口,避免只给一句「请先登录」的死胡同 -->
<view wx:if="{{item.type === 'system'}}" class="system-message">
<text>{{item.content}}</text>
<text
wx:if="{{item.contentType === 'login'}}"
class="system-login-link"
bindtap="goLogin"
aria-role="link"
>去登录</text>
</view>
<block wx:else> <block wx:else>
<view class="message-head"> <view class="message-head">
<!-- AI:品牌标记(暖墨线稿);底色与用户侧同构,身份只由图形区分 --> <!-- AI:品牌标记(暖墨线稿);底色与用户侧同构,身份只由图形区分 -->
+53 -1
View File
@@ -18,6 +18,43 @@
background-color: var(--paper); background-color: var(--paper);
} }
/* ===== 未登录提示条 ===== */
/* 浅橙衬底 + 2rpx --brand-line 描边托形(浅橙块落在纸面上没有边界感),
与欢迎区图标、工具条同属浅橙家族,不额外引入新色。
右侧「去登录」是裸文字链,与消息区的白卡、气泡区分开 */
.login-notice {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16rpx;
flex-shrink: 0;
margin: 20rpx 24rpx 0;
padding: 18rpx 24rpx;
border-radius: 20rpx;
background: var(--brand-soft);
border: 2rpx solid var(--brand-line);
}
.login-notice-text {
font-size: 28rpx;
line-height: 1.5;
color: var(--ink-2);
}
/* 热区靠上下 padding 撑到约 60rpx;「去登录」比正文再大一档,
它是这条提示里唯一可点的动作,字号必须压得住 */
.login-notice-link {
flex-shrink: 0;
padding: 10rpx 4rpx;
font-size: 30rpx;
font-weight: 600;
color: var(--brand-strong);
}
.login-notice-link:active {
opacity: 0.6;
}
/* ===== 辅助入口(在输入区下方:常见问题 + 清空聊天)===== */ /* ===== 辅助入口(在输入区下方:常见问题 + 清空聊天)===== */
/* 空心描边小按钮:2rpx 暖橙描边、无填充。 /* 空心描边小按钮:2rpx 暖橙描边、无填充。
两者都是低频辅助动作,用"只描边"与输入区右侧那个浅橙底发送键区分开; 两者都是低频辅助动作,用"只描边"与输入区右侧那个浅橙底发送键区分开;
@@ -169,12 +206,27 @@
.system-message { .system-message {
max-width: 90%; max-width: 90%;
padding: 8rpx 20rpx; padding: 8rpx 20rpx;
font-size: 24rpx; font-size: 26rpx;
line-height: 1.5; line-height: 1.5;
color: var(--muted); color: var(--muted);
text-align: center; text-align: center;
} }
/* 未登录提示里的登录入口:同一行居中的裸文字链,热区由上下 padding 撑起;
字号比系统提示正文大一档,保证它是这条提示里最醒目的动作 */
.system-login-link {
display: inline-block;
margin-left: 8rpx;
padding: 10rpx 8rpx;
font-size: 30rpx;
font-weight: 600;
color: var(--brand-strong);
}
.system-login-link:active {
opacity: 0.6;
}
.message-retry { .message-retry {
padding: 12rpx 0; padding: 12rpx 0;
font-size: 24rpx; font-size: 24rpx;