chore:退出聊天界面后,在进入聊天页面后没有多余的气泡

This commit is contained in:
2026-04-08 09:14:57 +08:00
parent 0ca0fe3b0d
commit 962ea124fa

View File

@@ -15,7 +15,7 @@
<!-- 消息列表 --> <!-- 消息列表 -->
<view <view
v-for="(message, index) in messages" v-for="(message, index) in messages.filter(msg => msg.content && msg.content.trim() !== '')"
:key="`msg-${message.id || message.timestamp}-${index}`" :key="`msg-${message.id || message.timestamp}-${index}`"
class="message-item" class="message-item"
:class="[message.role, { 'first-message': index === 0 }]"> :class="[message.role, { 'first-message': index === 0 }]">
@@ -418,22 +418,50 @@ export default {
isFetchingHistory: false isFetchingHistory: false
} }
}, },
created() { onShow() {
this.messages = [...this.initialMessages] // 如果不是 AI 客服,立即退出并跳转
this.messageId = this.messages.length if (customerServiceType !== 'ai') {
this.initAudioContext() uni.showToast({ title: '当前客服类型不支持此页面', icon: 'none' });
// 初始化用户头像(缓存优先) setTimeout(() => {
this.initUserAvatarData() uni.navigateBack({ delta: 1 }); // 或 redirectTo 首页
// 初始化用户昵称(缓存优先) }, 1000);
this.initUserNicknameData() return; // ⚠️ 关键:阻止后续初始化
}
// 👇 关键:在这里加载历史记录,只执行一次 // 优先读取本地缓存的会话 ID
this.loadChatHistoryIfExist().then(() => { const localConvId = this.getConversationIdFromLocal();
this.historyOffset = this.messages.length; if (localConvId) {
}); this.currentConversationId = localConvId;
aiService.setConversationId(localConvId);
this.currentConversationId = aiService.getConversationId(); } else {
}, this.currentConversationId = aiService.getConversationId();
}
// 加载初始历史后,同步historyOffset
this.loadChatHistoryIfExist().then(() => {
// 初始历史条数作为偏移量
this.historyOffset = this.messages.length;
});
// 移除:this.historyOffset = 0;(避免覆盖初始历史的偏移量)
},
// 新增:组件销毁时清除防抖定时器
beforeDestroy() {
if (this.scrollTimer) clearTimeout(this.scrollTimer);
},
created() {
this.messages = [...this.initialMessages]
this.messageId = this.messages.length
this.initAudioContext()
// 初始化用户头像(缓存优先)
this.initUserAvatarData()
// 初始化用户昵称(缓存优先)
this.initUserNicknameData()
this.currentConversationId = aiService.getConversationId();
},
mounted() {
// 初始加载时滚底(仅一次)
this.$nextTick(() => {
this.scrollToBottom()
})
},
watch: { watch: {
messages: { messages: {
handler() { handler() {
@@ -536,7 +564,7 @@ async loadChatHistoryIfExist() {
{ id: 2, text: '没帮助', type: 'dislike' } { id: 2, text: '没帮助', type: 'dislike' }
] : [] ] : []
}; };
}); }).filter(msg => msg.content.trim() !== ''); // 过滤掉内容为空的消息
} catch (error) { } catch (error) {
console.error('加载历史记录失败:', error); console.error('加载历史记录失败:', error);
// 只需调用服务层的清除方法,它会处理本地存储 // 只需调用服务层的清除方法,它会处理本地存储
@@ -865,6 +893,12 @@ async loadChatHistoryIfExist() {
// 标记流式结束 // 标记流式结束
streamMessage.isStreaming = false; streamMessage.isStreaming = false;
// 检查内容是否为空,如果为空则移除该消息
if (!streamMessage.content || streamMessage.content.trim() === '') {
this.messages = this.messages.filter(msg => msg.id !== streamMessage.id);
return;
}
// 添加操作按钮 // 添加操作按钮
streamMessage.actions = [ streamMessage.actions = [
{ id: 1, text: '有帮助', type: 'like' }, { id: 1, text: '有帮助', type: 'like' },
@@ -900,6 +934,12 @@ async loadChatHistoryIfExist() {
// 标记流式结束 // 标记流式结束
streamMessage.isStreaming = false; streamMessage.isStreaming = false;
// 检查内容是否为空,如果为空则移除该消息
if (!streamMessage.content || streamMessage.content.trim() === '') {
this.messages = this.messages.filter(msg => msg.id !== streamMessage.id);
return;
}
// 添加操作按钮 // 添加操作按钮
streamMessage.actions = [ streamMessage.actions = [
{ id: 1, text: '有帮助', type: 'like' }, { id: 1, text: '有帮助', type: 'like' },