This commit is contained in:
2026-04-20 09:52:27 +08:00
parent 88d05048a6
commit 7675e8e04d

View File

@@ -417,15 +417,11 @@ export default {
localUserAvatar: '', localUserAvatar: '',
// 默认头像路径 // 默认头像路径
defaultAvatar: '/static/images/user-avatar.png', defaultAvatar: '/static/images/user-avatar.png',
// 头像选择弹窗控制
showAvatarSelector: false, // 头像选择弹窗控制 showAvatarSelector: false, // 头像选择弹窗控制
// 新增:用户昵称相关
localUserNickname: '', // 当前用户昵称 localUserNickname: '', // 当前用户昵称
defaultNickname: '我', // 默认昵称 defaultNickname: '我', // 默认昵称
showNicknameEditor: false, // 昵称编辑弹窗控制 showNicknameEditor: false, // 昵称编辑弹窗控制
tempNickname: '', // 昵称编辑临时值 tempNickname: '', // 昵称编辑临时值
// AI头像配置(可配置,方便后续更换)
// 支持两种方式:
// 1. 使用图片URL:设置为图片链接字符串 // 1. 使用图片URL:设置为图片链接字符串
// 2. 使用emoji:设置为空字符串或null,将显示默认的🤖 // 2. 使用emoji:设置为空字符串或null,将显示默认的🤖
aiAvatarUrl: '/static/images/image_680328925892268.png', aiAvatarUrl: '/static/images/image_680328925892268.png',
@@ -436,7 +432,7 @@ export default {
currentConversationId: null, currentConversationId: null,
isAIServiceAvailable: true, isAIServiceAvailable: true,
aiServiceError: null, aiServiceError: null,
isLoadingHistory: false, // 新增:标记历史加载状态 isLoadingHistory: false,
shouldScrollToBottom: false, shouldScrollToBottom: false,
scrollTimer: null, scrollTimer: null,
lastScrollTop: 0, lastScrollTop: 0,
@@ -451,7 +447,7 @@ export default {
setTimeout(() => { setTimeout(() => {
uni.navigateBack({ delta: 1 }); // 或 redirectTo 首页 uni.navigateBack({ delta: 1 }); // 或 redirectTo 首页
}, 1000); }, 1000);
return; // ⚠️ 关键:阻止后续初始化 return; // 关键:阻止后续初始化
} }
// 优先读取本地缓存的会话 ID // 优先读取本地缓存的会话 ID
const localConvId = this.getConversationIdFromLocal(); const localConvId = this.getConversationIdFromLocal();
@@ -466,7 +462,6 @@ export default {
// 初始历史条数作为偏移量 // 初始历史条数作为偏移量
this.historyOffset = this.messages.length; this.historyOffset = this.messages.length;
}); });
// 移除:this.historyOffset = 0;(避免覆盖初始历史的偏移量)
}, },
// 新增:组件销毁时清除防抖定时器 // 新增:组件销毁时清除防抖定时器
beforeDestroy() { beforeDestroy() {
@@ -553,13 +548,12 @@ export default {
this.localUserAvatar = this.defaultAvatar this.localUserAvatar = this.defaultAvatar
} }
}, },
// 👇 新增:加载历史记录 // 新增:加载历史记录
// 👇 修正:加载历史记录
async loadChatHistoryIfExist() { async loadChatHistoryIfExist() {
const convId = aiService.getConversationId(); const convId = aiService.getConversationId();
if (convId) { if (convId) {
try { try {
// ✅ 调用正确的方法,并传入会话ID // 调用正确的方法,并传入会话ID
const response = await aiService.getConversationHistory({ const response = await aiService.getConversationHistory({
conversation_id: convId conversation_id: convId
}); });
@@ -568,8 +562,6 @@ async loadChatHistoryIfExist() {
console.warn('历史记录为空或格式无效'); console.warn('历史记录为空或格式无效');
return; return;
} }
// 注意:getConversationHistory 返回的数据结构与 getChatHistory 预期的不同
// 需要根据实际返回的 response.data.messages 来映射 // 需要根据实际返回的 response.data.messages 来映射
console.log('【调试】后端返回的历史消息数据:', response.messages); console.log('【调试】后端返回的历史消息数据:', response.messages);
this.messages = response.messages.map(msg => { this.messages = response.messages.map(msg => {
@@ -888,8 +880,6 @@ async loadChatHistoryIfExist() {
this.messages.push(aiMessage) this.messages.push(aiMessage)
this.$emit('ai-response', aiMessage) this.$emit('ai-response', aiMessage)
}, },
// 发送流式消息
// 发送流式消息(自动适配 H5 / 微信小程序) // 发送流式消息(自动适配 H5 / 微信小程序)
async sendStreamMessage(userMessage) { async sendStreamMessage(userMessage) {
// 创建流式消息对象 // 创建流式消息对象
@@ -901,10 +891,8 @@ async loadChatHistoryIfExist() {
timestamp: Date.now(), timestamp: Date.now(),
isStreaming: true isStreaming: true
}; };
// 标记正在流式响应 // 标记正在流式响应
this.isStreaming = true; this.isStreaming = true;
// 移除加载状态,添加流式消息 // 移除加载状态,添加流式消息
this.messages = this.messages.filter(msg => msg.type !== 'loading'); this.messages = this.messages.filter(msg => msg.type !== 'loading');
this.shouldScrollToBottom = true; this.shouldScrollToBottom = true;
@@ -917,7 +905,6 @@ async loadChatHistoryIfExist() {
try { try {
// #ifdef H5 // #ifdef H5
// ===== H5: 使用 POST + 流式 (fetch readable stream) =====
await aiService.sendHttpStream( await aiService.sendHttpStream(
userMessage, userMessage,
(chunk) => { (chunk) => {