chore:往上翻历史记录,不在乱窜了
This commit is contained in:
@@ -438,7 +438,7 @@ export default {
|
|||||||
isAIServiceAvailable: true,
|
isAIServiceAvailable: true,
|
||||||
aiServiceError: null,
|
aiServiceError: null,
|
||||||
isLoadingHistory: false, // 新增:标记历史加载状态
|
isLoadingHistory: false, // 新增:标记历史加载状态
|
||||||
|
shouldScrollToBottom: false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onShow() {
|
onShow() {
|
||||||
@@ -464,16 +464,24 @@ export default {
|
|||||||
this.initUserNicknameData()
|
this.initUserNicknameData()
|
||||||
this.currentConversationId = aiService.getConversationId();
|
this.currentConversationId = aiService.getConversationId();
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
this.scrollToBottom()
|
// 初始加载时滚底(仅一次)
|
||||||
},
|
this.$nextTick(() => {
|
||||||
|
this.scrollToBottom()
|
||||||
|
})
|
||||||
|
},
|
||||||
watch: {
|
watch: {
|
||||||
messages: {
|
messages: {
|
||||||
handler() {
|
handler() {
|
||||||
this.$nextTick(() => {
|
this.$nextTick(() => {
|
||||||
this.scrollToBottom()
|
// 仅当不是加载历史、且需要滚底时,才执行滚底
|
||||||
})
|
if (!this.isLoadingHistory && this.shouldScrollToBottom) {
|
||||||
},
|
this.scrollToBottom()
|
||||||
|
// 滚底后重置状态,避免重复触发
|
||||||
|
this.shouldScrollToBottom = false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
deep: true
|
deep: true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -519,38 +527,42 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
// 👇 新增:加载历史记录
|
// 👇 新增:加载历史记录
|
||||||
async loadChatHistoryIfExist() {
|
async loadChatHistoryIfExist() {
|
||||||
const convId = aiService.getConversationId();
|
const convId = aiService.getConversationId();
|
||||||
if (convId) {
|
if (convId) {
|
||||||
try {
|
try {
|
||||||
const history = await aiService.getConversationHistory({
|
const history = await aiService.getChatHistory();
|
||||||
conversation_id: convId,
|
if (!history || !Array.isArray(history.messages)) {
|
||||||
uniacid: this.$store.state.uniacid,
|
console.warn('历史记录为空或格式无效');
|
||||||
user_id: this.$store.state.memberInfo?.id || 'anonymous'
|
return;
|
||||||
});
|
}
|
||||||
|
|
||||||
if (history.success && history.messages?.length > 0) {
|
this.messages = history.messages.map(msg => {
|
||||||
// 格式化消息
|
let content = '';
|
||||||
this.messages = history.messages.map(msg => ({
|
if (msg.role === 'user') {
|
||||||
id: msg.message_id || (Date.now() + Math.random() * 10000),
|
content = msg.query || (typeof msg.inputs === 'string' ? msg.inputs : JSON.stringify(msg.inputs || '')) || '';
|
||||||
role: msg.role === 'user' ? 'user' : 'assistant',
|
} else {
|
||||||
content: msg.content || msg.answer || msg.text || '',
|
content = msg.answer || '';
|
||||||
timestamp: msg.created_at,
|
}
|
||||||
actions: msg.role !== 'user' ? [
|
return {
|
||||||
{ id: 1, text: '有帮助', type: 'like' },
|
id: msg.message_id || (Date.now() + Math.random() * 10000),
|
||||||
{ id: 2, text: '没帮助', type: 'dislike' }
|
role: msg.role === 'user' ? 'user' : 'assistant',
|
||||||
] : []
|
content: content,
|
||||||
}));
|
timestamp: msg.created_at,
|
||||||
}
|
actions: msg.role !== 'user' ? [
|
||||||
} catch (error) {
|
{ id: 1, text: '有帮助', type: 'like' },
|
||||||
console.error('加载历史记录失败:', error);
|
{ id: 2, text: '没帮助', type: 'dislike' }
|
||||||
// 可选:清除无效ID
|
] : []
|
||||||
aiService.clearConversationId();
|
};
|
||||||
this.clearConversationIdFromLocal();
|
});
|
||||||
this.currentConversationId = null;
|
} catch (error) {
|
||||||
}
|
console.error('加载历史记录失败:', error);
|
||||||
}
|
aiService.clearConversationId();
|
||||||
},
|
this.clearConversationIdFromLocal();
|
||||||
|
this.currentConversationId = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
// 新增:初始化用户昵称(支持缓存持久化)
|
// 新增:初始化用户昵称(支持缓存持久化)
|
||||||
initUserNicknameData() {
|
initUserNicknameData() {
|
||||||
// 1. 优先读取本地缓存的昵称
|
// 1. 优先读取本地缓存的昵称
|
||||||
@@ -688,7 +700,7 @@ export default {
|
|||||||
content: this.inputText.trim(),
|
content: this.inputText.trim(),
|
||||||
timestamp: Date.now()
|
timestamp: Date.now()
|
||||||
}
|
}
|
||||||
|
this.shouldScrollToBottom = true
|
||||||
this.messages.push(userMessage)
|
this.messages.push(userMessage)
|
||||||
this.inputText = ''
|
this.inputText = ''
|
||||||
|
|
||||||
@@ -700,7 +712,7 @@ export default {
|
|||||||
content: '',
|
content: '',
|
||||||
timestamp: Date.now()
|
timestamp: Date.now()
|
||||||
}
|
}
|
||||||
|
this.shouldScrollToBottom = true
|
||||||
this.messages.push(loadingMessage)
|
this.messages.push(loadingMessage)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -736,7 +748,7 @@ export default {
|
|||||||
{ id: 1, text: '重试', type: 'retry' }
|
{ id: 1, text: '重试', type: 'retry' }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
this.shouldScrollToBottom = true
|
||||||
this.messages.push(errorMessage)
|
this.messages.push(errorMessage)
|
||||||
this.$emit('ai-response', errorMessage)
|
this.$emit('ai-response', errorMessage)
|
||||||
}
|
}
|
||||||
@@ -775,7 +787,7 @@ export default {
|
|||||||
{ id: 2, text: '没帮助', type: 'dislike' }
|
{ id: 2, text: '没帮助', type: 'dislike' }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
this.shouldScrollToBottom = true
|
||||||
this.messages.push(aiMessage)
|
this.messages.push(aiMessage)
|
||||||
this.$emit('ai-response', aiMessage)
|
this.$emit('ai-response', aiMessage)
|
||||||
},
|
},
|
||||||
@@ -794,6 +806,7 @@ export default {
|
|||||||
|
|
||||||
// 移除加载状态,添加流式消息
|
// 移除加载状态,添加流式消息
|
||||||
this.messages = this.messages.filter(msg => msg.type !== 'loading')
|
this.messages = this.messages.filter(msg => msg.type !== 'loading')
|
||||||
|
this.shouldScrollToBottom = true
|
||||||
this.messages.push(streamMessage)
|
this.messages.push(streamMessage)
|
||||||
|
|
||||||
// 开始流式响应
|
// 开始流式响应
|
||||||
|
|||||||
Reference in New Issue
Block a user