ai对话分页
This commit is contained in:
@@ -122,7 +122,11 @@
|
||||
|
||||
<view class="main-chat">
|
||||
<scroll-view class="chat-messages" direction="vertical" scroll-y :scroll-into-view="scrollToView"
|
||||
:upper-threshold="0" :scroll-with-animation="!isThinking" @scroll="onScroll">
|
||||
:upper-threshold="50" :scroll-with-animation="!isThinking" @scroll="onScroll" @scrolltoupper="onScrollToUpper">
|
||||
<!-- 加载更多提示(AI 会话) -->
|
||||
<view v-if="ChatType === 0 && isAILoadingMore" class="load-more-tip">
|
||||
<text>加载更多消息...</text>
|
||||
</view>
|
||||
<!-- 消息加载中弹窗(仅会话变化时显示) -->
|
||||
<view v-if="isLoadingMessages" class="messages-loading-overlay">
|
||||
<view class="messages-loading-card">
|
||||
@@ -1685,7 +1689,11 @@ import { openFile as openFileNative, downloadAndOpen } from '@/utils/fileOpener.
|
||||
const bottomToggle = ref(false)
|
||||
// 加载状态和分页相关变量
|
||||
const isLoadingMore = ref(false) // 是否正在加载更多
|
||||
const isAILoadingMore = ref(false) // AI 会话是否正在加载更多(滚动到顶部触发)
|
||||
const aiPageNumber = ref(0) // AI 会话当前服务端页码(0-based)
|
||||
const hasMoreAIMessages = ref(true) // AI 会话是否还有更多消息可加载
|
||||
const currentPage = ref(1) // 当前页码(如果后端支持分页)
|
||||
const pageSize = 200;
|
||||
// 计算属性:自动根据 allmessages 和 currentPage 计算显示消息
|
||||
const currentMessages = computed(() => {
|
||||
// return allmessages.value.slice(-pageInfoNumber);
|
||||
@@ -1696,8 +1704,12 @@ import { openFile as openFileNative, downloadAndOpen } from '@/utils/fileOpener.
|
||||
// 获取对话消息
|
||||
const takeConversationMessages = async () => {
|
||||
try {
|
||||
allmessages.value = await getConversationMessages(userToken.value, currentSessionId.value) || [];
|
||||
// console.log("获取到的所有消息:", JSON.stringify(allmessages.value) );
|
||||
aiPageNumber.value = 0
|
||||
hasMoreAIMessages.value = true
|
||||
const rawMessages = await getConversationMessages(userToken.value, currentSessionId.value, pageSize, 0) || [];
|
||||
// API 返回的数据已被过滤,无法通过数量判断是否有更多,始终尝试加载更多
|
||||
allmessages.value = rawMessages
|
||||
console.log("获取到的所有消息:", JSON.stringify(allmessages.value) );
|
||||
// console.log("获取到的所有消息:", allmessages.value);
|
||||
// currentMessages.value = allmessages.value.slice(-pageInfoNumber);
|
||||
// 数据获取后执行滚动
|
||||
@@ -1766,7 +1778,39 @@ import { openFile as openFileNative, downloadAndOpen } from '@/utils/fileOpener.
|
||||
// // currentMessages.value = allmessages.value.slice(-pageInfoNumber);
|
||||
// }
|
||||
|
||||
// 滚动到顶部时加载更多 AI 消息
|
||||
const onScrollToUpper = async () => {
|
||||
if (ChatType.value === 0) {
|
||||
await loadMoreAIMessages()
|
||||
}
|
||||
}
|
||||
|
||||
// 加载更多 AI 历史消息(上一页),并定位到原第一条消息
|
||||
const loadMoreAIMessages = async () => {
|
||||
if (isAILoadingMore.value || !hasMoreAIMessages.value || !currentSessionId.value) return
|
||||
isAILoadingMore.value = true
|
||||
try {
|
||||
const nextPage = aiPageNumber.value + 1
|
||||
const olderMessages = await getConversationMessages(userToken.value, currentSessionId.value, pageSize, nextPage)
|
||||
|
||||
if (olderMessages && olderMessages.length > 0) {
|
||||
aiPageNumber.value = nextPage
|
||||
allmessages.value = [...olderMessages, ...allmessages.value]
|
||||
|
||||
// 定位滚动:原本第一条消息(old index 0)现在在 index = olderMessages.length 处
|
||||
await nextTick()
|
||||
scrollToView.value = 'msg-' + olderMessages.length
|
||||
} else {
|
||||
// 只有 API 返回空时才确认没有更多
|
||||
hasMoreAIMessages.value = false
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('加载更多消息失败:', e)
|
||||
uni.showToast({ title: '加载更多失败', icon: 'none' })
|
||||
} finally {
|
||||
isAILoadingMore.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// 监听滚动
|
||||
const onScroll = (e) => {}
|
||||
|
||||
Reference in New Issue
Block a user