feat: 添加上拉刷新功能,优化聊天历史加载逻辑及状态管理

This commit is contained in:
yangzhe 2025-07-11 17:20:45 +08:00
parent a4a9a6c566
commit 71345b7bf4
1 changed files with 142 additions and 42 deletions

View File

@ -21,7 +21,8 @@
></image> ></image>
<text class="message-text" <text class="message-text"
>Hi~ 我是源小新你们的AI校园助手非常高兴认识您我可以为你答疑解惑</text >Hi~
我是源小新你们的AI校园助手非常高兴认识您我可以为你答疑解惑</text
> >
</view> </view>
@ -85,6 +86,7 @@
:scroll-top="scrollTop" :scroll-top="scrollTop"
:show-scrollbar="true" :show-scrollbar="true"
@scrolltolower="onScrollToLower" @scrolltolower="onScrollToLower"
@scrolltoupper="onScrollToUpper"
> >
<!-- 头部卡片 --> <!-- 头部卡片 -->
<!-- 后端让先注释 --> <!-- 后端让先注释 -->
@ -112,6 +114,16 @@
<!-- 对话内容区域 --> <!-- 对话内容区域 -->
<view class="chat-content"> <view class="chat-content">
<!-- 上拉刷新loading -->
<view class="loading-more" v-if="isLoading">
<u-loading mode="circle" color="#4370fe"></u-loading>
</view>
<!-- 到顶部提示 -->
<view class="no-more-data" v-if="noMoreData">
<text>已经到顶了</text>
</view>
<!-- 消息循环渲染 --> <!-- 消息循环渲染 -->
<block <block
v-for="(message, index) in messageGroups" v-for="(message, index) in messageGroups"
@ -261,6 +273,14 @@ export default {
currentConversationId: "", currentConversationId: "",
advicePhoneShow: false, advicePhoneShow: false,
perfectInfoShow: false, perfectInfoShow: false,
isLoading: false, // loadingMore
isLoadingMore: false, //
noMoreData: false, //
pageQuery: {
PageIndex: 1,
PageSize: 20,
},
questionList: [ questionList: [
"学习哪些专业比较好?", "学习哪些专业比较好?",
@ -360,7 +380,10 @@ export default {
// //
messageGroups: { messageGroups: {
handler() { handler() {
this.scrollToBottom(); //
if (!this.isLoadingMore) {
this.scrollToBottom();
}
}, },
deep: true, deep: true,
}, },
@ -376,18 +399,7 @@ export default {
deep: true, deep: true,
}, },
}, },
onLoad() { onLoad() {},
//
// uni.getImageInfo({
// src: '/static/common/images/images_bg.png',
// success: () => {
// console.log('');
// },
// fail: (err) => {
// console.error('', err);
// }
// });
},
methods: { methods: {
handleLeftClick() { handleLeftClick() {
this.$u.api.GetConversationPage().then((res) => { this.$u.api.GetConversationPage().then((res) => {
@ -414,32 +426,30 @@ export default {
}, },
scrollToBottom() { scrollToBottom() {
//
if (this.isLoadingMore) return;
// 使nextTickDOM // 使nextTickDOM
this.$nextTick(() => { this.$nextTick(() => {
// setTimeout(() => { // setTimeout(() => {
const query = uni.createSelectorQuery().in(this); const query = uni.createSelectorQuery().in(this);
query query
.select(".chat-content") .select(".chat-content")
.boundingClientRect((data) => { .boundingClientRect((data) => {
if (data) { if (data) {
console.log("chat-content高度:", data.height); console.log("chat-content高度:", data.height);
// 使pageScrollTo
// uni.pageScrollTo({
// scrollTop: 99999,
// duration: 100,
// });
// scrollTopscrollToView // scrollTopscrollToView
this.scrollTop = data.height + 200; this.scrollTop = data.height + 200;
// if (this.messageGroups.length > 0) { // if (this.messageGroups.length > 0) {
// const lastMessage = // const lastMessage =
// this.messageGroups[this.messageGroups.length - 1]; // this.messageGroups[this.messageGroups.length - 1];
// this.scrollToView = "msg-" + lastMessage.id; // this.scrollToView = "msg-" + lastMessage.id;
// console.log("scrollToView:", this.scrollToView); // console.log("scrollToView:", this.scrollToView);
// } // }
} }
}) })
.exec(); .exec();
// }, 300); // }, 300);
}); });
}, },
@ -463,6 +473,9 @@ export default {
return; return;
} }
//
this.isLoadingMore = false;
const sendMessage = this.messageValue; const sendMessage = this.messageValue;
console.log("发送消息", sendMessage); console.log("发送消息", sendMessage);
@ -528,19 +541,21 @@ export default {
console.log("选中的对话:", data); console.log("选中的对话:", data);
// conversationId // conversationId
const conversationId = data.conversationId; this.currentConversationId = data.conversationId;
this.currentConversationId = conversationId;
this.isChat = true; this.isChat = true;
//
this.messageGroups = []; this.messageGroups = [];
this.pageQuery.PageIndex = 1;
this.isLoadingMore = false;
this.noMoreData = false;
this.$u.api this.$u.api
.GetConversationDetail({ .GetConversationDetail({
"Item1.Id": conversationId, "Item1.Id": this.currentConversationId,
PageIndex: "1", PageIndex: this.pageQuery.PageIndex,
PageSize: "20", PageSize: this.pageQuery.PageSize,
}) })
.then((res) => { .then((res) => {
console.log("GetConversationDetail.....", res.item2); console.log("GetConversationDetail.....", res.item2);
@ -561,24 +576,96 @@ export default {
}); });
}, },
//
handleCreateConversation() { handleCreateConversation() {
// //
this.popupShow = false; this.popupShow = false;
this.isChat = true; this.isChat = true;
this.currentConversationId = ""; this.currentConversationId = "";
this.messageGroups = []; this.messageGroups = [];
//
this.pageQuery.PageIndex = 1;
this.isLoadingMore = false;
this.noMoreData = false;
}, },
//
handleStartChat() { handleStartChat() {
this.isChat = true; this.isChat = true;
this.currentConversationId = ""; this.currentConversationId = "";
this.messageGroups = []; this.messageGroups = [];
//
this.pageQuery.PageIndex = 1;
this.isLoadingMore = false;
this.noMoreData = false;
}, },
// //
onScrollToLower() { onScrollToLower() {
console.log("已滚动到底部"); 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();
// interactMode01
if (timeA === timeB) {
return a.interactMode - b.interactMode;
}
//
return timeA - timeB;
});
this.messageGroups = [
...nextPageMessageGroups,
...this.messageGroups,
];
})
.finally(() => {
setTimeout(() => {
this.isLoading = false;
}, 300);
});
},
}, },
}; };
</script> </script>
@ -836,6 +923,19 @@ export default {
padding: 20rpx 0; padding: 20rpx 0;
margin-bottom: 150rpx; 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 { .message-time {
text-align: center; text-align: center;
font-size: 24rpx; font-size: 24rpx;