chore:往上翻历史记录,不在乱窜了
This commit is contained in:
@@ -438,7 +438,7 @@ export default {
|
||||
isAIServiceAvailable: true,
|
||||
aiServiceError: null,
|
||||
isLoadingHistory: false, // 新增:标记历史加载状态
|
||||
|
||||
shouldScrollToBottom: false
|
||||
}
|
||||
},
|
||||
onShow() {
|
||||
@@ -464,16 +464,24 @@ export default {
|
||||
this.initUserNicknameData()
|
||||
this.currentConversationId = aiService.getConversationId();
|
||||
},
|
||||
mounted() {
|
||||
this.scrollToBottom()
|
||||
},
|
||||
mounted() {
|
||||
// 初始加载时滚底(仅一次)
|
||||
this.$nextTick(() => {
|
||||
this.scrollToBottom()
|
||||
})
|
||||
},
|
||||
watch: {
|
||||
messages: {
|
||||
handler() {
|
||||
this.$nextTick(() => {
|
||||
this.scrollToBottom()
|
||||
})
|
||||
},
|
||||
messages: {
|
||||
handler() {
|
||||
this.$nextTick(() => {
|
||||
// 仅当不是加载历史、且需要滚底时,才执行滚底
|
||||
if (!this.isLoadingHistory && this.shouldScrollToBottom) {
|
||||
this.scrollToBottom()
|
||||
// 滚底后重置状态,避免重复触发
|
||||
this.shouldScrollToBottom = false
|
||||
}
|
||||
})
|
||||
},
|
||||
deep: true
|
||||
}
|
||||
},
|
||||
@@ -519,38 +527,42 @@ export default {
|
||||
}
|
||||
},
|
||||
// 👇 新增:加载历史记录
|
||||
async loadChatHistoryIfExist() {
|
||||
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'
|
||||
});
|
||||
|
||||
if (history.success && history.messages?.length > 0) {
|
||||
// 格式化消息
|
||||
this.messages = history.messages.map(msg => ({
|
||||
id: msg.message_id || (Date.now() + Math.random() * 10000),
|
||||
role: msg.role === 'user' ? 'user' : 'assistant',
|
||||
content: msg.content || msg.answer || msg.text || '',
|
||||
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;
|
||||
}
|
||||
}
|
||||
},
|
||||
async loadChatHistoryIfExist() {
|
||||
const convId = aiService.getConversationId();
|
||||
if (convId) {
|
||||
try {
|
||||
const history = await aiService.getChatHistory();
|
||||
if (!history || !Array.isArray(history.messages)) {
|
||||
console.warn('历史记录为空或格式无效');
|
||||
return;
|
||||
}
|
||||
|
||||
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: content,
|
||||
timestamp: msg.created_at,
|
||||
actions: msg.role !== 'user' ? [
|
||||
{ id: 1, text: '有帮助', type: 'like' },
|
||||
{ id: 2, text: '没帮助', type: 'dislike' }
|
||||
] : []
|
||||
};
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('加载历史记录失败:', error);
|
||||
aiService.clearConversationId();
|
||||
this.clearConversationIdFromLocal();
|
||||
this.currentConversationId = null;
|
||||
}
|
||||
}
|
||||
},
|
||||
// 新增:初始化用户昵称(支持缓存持久化)
|
||||
initUserNicknameData() {
|
||||
// 1. 优先读取本地缓存的昵称
|
||||
@@ -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)
|
||||
|
||||
// 开始流式响应
|
||||
|
||||
Reference in New Issue
Block a user