chore:往上翻历史记录,不在乱窜了

This commit is contained in:
2025-12-16 11:32:40 +08:00
parent c45f3e69da
commit 8d84306747

View File

@@ -438,7 +438,7 @@ export default {
isAIServiceAvailable: true,
aiServiceError: null,
isLoadingHistory: false, // 新增:标记历史加载状态
shouldScrollToBottom: false
}
},
onShow() {
@@ -465,13 +465,21 @@ export default {
this.currentConversationId = aiService.getConversationId();
},
mounted() {
// 初始加载时滚底(仅一次)
this.$nextTick(() => {
this.scrollToBottom()
})
},
watch: {
messages: {
handler() {
this.$nextTick(() => {
// 仅当不是加载历史、且需要滚底时,才执行滚底
if (!this.isLoadingHistory && this.shouldScrollToBottom) {
this.scrollToBottom()
// 滚底后重置状态,避免重复触发
this.shouldScrollToBottom = false
}
})
},
deep: true
@@ -523,28 +531,32 @@ export default {
const convId = aiService.getConversationId();
if (convId) {
try {
const history = await aiService.getConversationHistory({
conversation_id: convId,
uniacid: this.$store.state.uniacid,
user_id: this.$store.state.memberInfo?.id || 'anonymous'
});
const history = await aiService.getChatHistory();
if (!history || !Array.isArray(history.messages)) {
console.warn('历史记录为空或格式无效');
return;
}
if (history.success && history.messages?.length > 0) {
// 格式化消息
this.messages = history.messages.map(msg => ({
this.messages = history.messages.map(msg => {
let content = '';
if (msg.role === 'user') {
content = msg.query || (typeof msg.inputs === 'string' ? msg.inputs : JSON.stringify(msg.inputs || '')) || '';
} else {
content = msg.answer || '';
}
return {
id: msg.message_id || (Date.now() + Math.random() * 10000),
role: msg.role === 'user' ? 'user' : 'assistant',
content: msg.content || msg.answer || msg.text || '',
content: content,
timestamp: msg.created_at,
actions: msg.role !== 'user' ? [
{ id: 1, text: '有帮助', type: 'like' },
{ id: 2, text: '没帮助', type: 'dislike' }
] : []
}));
}
};
});
} catch (error) {
console.error('加载历史记录失败:', error);
// 可选清除无效ID
aiService.clearConversationId();
this.clearConversationIdFromLocal();
this.currentConversationId = null;
@@ -688,7 +700,7 @@ export default {
content: this.inputText.trim(),
timestamp: Date.now()
}
this.shouldScrollToBottom = true
this.messages.push(userMessage)
this.inputText = ''
@@ -700,7 +712,7 @@ export default {
content: '',
timestamp: Date.now()
}
this.shouldScrollToBottom = true
this.messages.push(loadingMessage)
try {
@@ -736,7 +748,7 @@ export default {
{ id: 1, text: '重试', type: 'retry' }
]
}
this.shouldScrollToBottom = true
this.messages.push(errorMessage)
this.$emit('ai-response', errorMessage)
}
@@ -775,7 +787,7 @@ export default {
{ id: 2, text: '没帮助', type: 'dislike' }
]
}
this.shouldScrollToBottom = true
this.messages.push(aiMessage)
this.$emit('ai-response', aiMessage)
},
@@ -794,6 +806,7 @@ export default {
// 移除加载状态,添加流式消息
this.messages = this.messages.filter(msg => msg.type !== 'loading')
this.shouldScrollToBottom = true
this.messages.push(streamMessage)
// 开始流式响应