diff --git a/pages/home/index/index.vue b/pages/home/index/index.vue index a0a9f14..6a6aae5 100644 --- a/pages/home/index/index.vue +++ b/pages/home/index/index.vue @@ -21,7 +21,8 @@ > Hi~ 我是源小新,你们的AI校园助手,非常高兴认识您,我可以为你答疑解惑。Hi~ + 我是源小新,你们的AI校园助手,非常高兴认识您,我可以为你答疑解惑。 @@ -85,6 +86,7 @@ :scroll-top="scrollTop" :show-scrollbar="true" @scrolltolower="onScrollToLower" + @scrolltoupper="onScrollToUpper" > @@ -112,6 +114,16 @@ + + + + + + + + 已经到顶了 + + { - // console.log('背景图预加载成功'); - // }, - // fail: (err) => { - // console.error('背景图预加载失败', err); - // } - // }); - }, + onLoad() {}, methods: { handleLeftClick() { this.$u.api.GetConversationPage().then((res) => { @@ -414,32 +426,30 @@ export default { }, scrollToBottom() { + // 如果正在加载更多,不执行滚动 + if (this.isLoadingMore) return; + // 使用nextTick确保DOM已更新 this.$nextTick(() => { // setTimeout(() => { - const query = uni.createSelectorQuery().in(this); - query - .select(".chat-content") - .boundingClientRect((data) => { - if (data) { - console.log("chat-content高度:", data.height); - // 使用pageScrollTo滚动到底部 - // uni.pageScrollTo({ - // scrollTop: 99999, - // duration: 100, - // }); + const query = uni.createSelectorQuery().in(this); + query + .select(".chat-content") + .boundingClientRect((data) => { + if (data) { + console.log("chat-content高度:", data.height); - // 同时设置scrollTop和scrollToView - this.scrollTop = data.height + 200; - // if (this.messageGroups.length > 0) { - // const lastMessage = - // this.messageGroups[this.messageGroups.length - 1]; - // this.scrollToView = "msg-" + lastMessage.id; - // console.log("设置scrollToView:", this.scrollToView); - // } - } - }) - .exec(); + // 同时设置scrollTop和scrollToView + this.scrollTop = data.height + 200; + // if (this.messageGroups.length > 0) { + // const lastMessage = + // this.messageGroups[this.messageGroups.length - 1]; + // this.scrollToView = "msg-" + lastMessage.id; + // console.log("设置scrollToView:", this.scrollToView); + // } + } + }) + .exec(); // }, 300); }); }, @@ -463,6 +473,9 @@ export default { return; } + // 重置加载更多标志位 + this.isLoadingMore = false; + const sendMessage = this.messageValue; console.log("发送消息", sendMessage); @@ -528,19 +541,21 @@ export default { console.log("选中的对话:", data); // 根据conversationId加载对应的对话内容 - const conversationId = data.conversationId; - - this.currentConversationId = conversationId; + this.currentConversationId = data.conversationId; this.isChat = true; + // 重置消息列表和分页相关状态 this.messageGroups = []; + this.pageQuery.PageIndex = 1; + this.isLoadingMore = false; + this.noMoreData = false; this.$u.api .GetConversationDetail({ - "Item1.Id": conversationId, - PageIndex: "1", - PageSize: "20", + "Item1.Id": this.currentConversationId, + PageIndex: this.pageQuery.PageIndex, + PageSize: this.pageQuery.PageSize, }) .then((res) => { console.log("GetConversationDetail.....", res.item2); @@ -561,24 +576,96 @@ export default { }); }, + // 在开始新对话或选择对话时重置相关状态 handleCreateConversation() { // 关闭弹窗 this.popupShow = false; this.isChat = true; this.currentConversationId = ""; this.messageGroups = []; + + // 重置分页和加载状态 + this.pageQuery.PageIndex = 1; + this.isLoadingMore = false; + this.noMoreData = false; }, + // 开始新对话 handleStartChat() { this.isChat = true; this.currentConversationId = ""; this.messageGroups = []; + + // 重置分页和加载状态 + this.pageQuery.PageIndex = 1; + this.isLoadingMore = false; + this.noMoreData = false; }, // 滚动到底部事件处理 onScrollToLower() { console.log("已滚动到底部"); }, + + // 滚动到顶部事件处理 + onScrollToUpper() { + console.log("触发上拉刷新"); + + // 如果已经没有更多数据,不再触发上拉刷新 + if (this.noMoreData) { + return; + } + + // 设置加载标志位为true,防止触发滚动到底部 + this.isLoadingMore = true; + this.isLoading = true; + + this.pageQuery.PageIndex++; + this.$u.api + .GetConversationDetail({ + "Item1.Id": this.currentConversationId, + PageIndex: this.pageQuery.PageIndex, + PageSize: this.pageQuery.PageSize, + }) + .then((res) => { + console.log("GetConversationDetail.....", res.item2); + + // 如果返回的数据为空数组,说明没有更多历史消息了 + if (!res.item2 || res.item2.length === 0) { + this.noMoreData = true; // 设置标记,不再触发上拉刷新 + this.$refs.uToast.show({ + title: "已经到顶了", + type: "warning", + duration: 1500 + }); + return; + } + + // 将消息按sendDate升序排列,时间相同时用户消息(interactMode=0)排在前面 + const nextPageMessageGroups = res.item2.sort((a, b) => { + const timeA = new Date(a.sendDate).getTime(); + const timeB = new Date(b.sendDate).getTime(); + + // 如果时间相同,按interactMode排序(0排在前,1排在后) + if (timeA === timeB) { + return a.interactMode - b.interactMode; + } + + // 否则按时间升序排列 + return timeA - timeB; + }); + + this.messageGroups = [ + ...nextPageMessageGroups, + ...this.messageGroups, + ]; + }) + .finally(() => { + setTimeout(() => { + this.isLoading = false; + }, 300); + }); + }, }, }; @@ -836,6 +923,19 @@ export default { padding: 20rpx 0; margin-bottom: 150rpx; + .loading-more { + text-align: center; + margin-bottom: 32rpx; + } + + .no-more-data { + text-align: center; + font-size: 24rpx; + color: #999; + margin-bottom: 32rpx; + padding: 10rpx 0; + } + .message-time { text-align: center; font-size: 24rpx;