chore:可以看见历史记录了
This commit is contained in:
@@ -837,58 +837,64 @@ export default {
|
|||||||
},
|
},
|
||||||
|
|
||||||
// 加载更多历史消息
|
// 加载更多历史消息
|
||||||
async loadMoreHistory() {
|
async loadMoreHistory() {
|
||||||
if (!this.showLoadMore || !this.currentConversationId) {
|
if (!this.showLoadMore || !this.currentConversationId || this.isLoadingHistory) {
|
||||||
this.hasMoreHistory = false
|
return
|
||||||
return
|
}
|
||||||
}
|
|
||||||
|
|
||||||
this.hasMoreHistory = true
|
this.isLoadingHistory = true
|
||||||
this.loadingText = '加载历史消息中...'
|
this.loadingText = '加载历史消息中...'
|
||||||
|
this.hasMoreHistory = true
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await aiService.getConversationHistory({
|
const response = await aiService.getConversationHistory({
|
||||||
conversation_id: this.currentConversationId,
|
conversation_id: this.currentConversationId,
|
||||||
limit: 20,
|
limit: 20,
|
||||||
offset: this.messages.length // 简单分页:已加载多少条,就跳过多少
|
offset: this.messages.length
|
||||||
})
|
})
|
||||||
|
|
||||||
if (res.success && Array.isArray(res.messages)) {
|
if (response.success && Array.isArray(response.messages)) {
|
||||||
// 转换后端消息格式 → 前端组件格式
|
const historyMessages = response.messages.map(msg => ({
|
||||||
const historyMessages = res.messages.map(msg => ({
|
id: msg.id,
|
||||||
id: msg.id, // 直接用后端的字符串 ID(比自增数字更可靠)
|
role: msg.role === 'user' ? 'user' : 'ai',
|
||||||
role: msg.role === 'user' ? 'user' : 'ai', // 关键:'assistant' → 'ai'
|
type: 'text',
|
||||||
type: 'text',
|
content: msg.content,
|
||||||
content: msg.content,
|
timestamp: msg.create_time * 1000
|
||||||
timestamp: msg.create_time * 1000 // 秒 → 毫秒
|
}))
|
||||||
})).reverse() // 后端按时间正序(旧→新),但我们要插到顶部,所以 reverse 成 新→旧?不!
|
|
||||||
|
|
||||||
// ⚠️ 注意:getHistory 返回的是从旧到新(offset=0 是最早的消息)
|
if (historyMessages.length > 0) {
|
||||||
// 而 this.messages 是 [旧, ..., 新]
|
this.messages = [...historyMessages, ...this.messages]
|
||||||
// 所以新加载的历史应该放在现有消息的前面,且保持“旧→新”顺序
|
|
||||||
// 因此 **不要 reverse**!直接拼接即可(但确保 offset 正确)
|
|
||||||
|
|
||||||
// 更安全的做法:按时间排序(可选)
|
// 👇 注意:这里加了 async
|
||||||
// historyMessages.sort((a, b) => a.timestamp - b.timestamp)
|
this.$nextTick(async () => {
|
||||||
|
const query = uni.createSelectorQuery().in(this)
|
||||||
|
query.select('.chat-messages').boundingClientRect()
|
||||||
|
query.select('.chat-messages').scrollOffset()
|
||||||
|
|
||||||
// 插入到顶部(历史在上,最新在下)
|
const res = await new Promise((resolve) => {
|
||||||
this.messages = [...historyMessages, ...this.messages]
|
query.exec(resolve)
|
||||||
|
})
|
||||||
|
|
||||||
// 判断是否还有更多历史
|
const currentScrollTop = res[1]?.scrollTop || 0
|
||||||
this.hasMoreHistory = res.total > this.messages.length
|
const avgMessageHeight = 80
|
||||||
this.showLoadMore = res.total > this.messages.length
|
const addedHeight = historyMessages.length * avgMessageHeight
|
||||||
} else {
|
|
||||||
this.hasMoreHistory = false
|
|
||||||
this.showLoadMore = false
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('加载历史失败:', error)
|
|
||||||
uni.showToast({ title: '加载历史失败', icon: 'none' })
|
|
||||||
this.hasMoreHistory = false
|
|
||||||
this.showLoadMore = false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
|
this.scrollTop = currentScrollTop - addedHeight
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.showLoadMore = false
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('加载历史失败:', error)
|
||||||
|
uni.showToast({ title: '加载历史失败', icon: 'none' })
|
||||||
|
this.showLoadMore = false
|
||||||
|
} finally {
|
||||||
|
this.isLoadingHistory = false
|
||||||
|
this.hasMoreHistory = false
|
||||||
|
this.loadingText = '加载更多历史消息'
|
||||||
|
}
|
||||||
|
},
|
||||||
// 显示更多工具
|
// 显示更多工具
|
||||||
showMoreTools() {
|
showMoreTools() {
|
||||||
this.showToolsPanel = true
|
this.showToolsPanel = true
|
||||||
|
|||||||
Reference in New Issue
Block a user