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>
<text class="message-text"
>Hi~ 我是源小新你们的AI校园助手非常高兴认识您我可以为你答疑解惑</text
>Hi~
我是源小新你们的AI校园助手非常高兴认识您我可以为你答疑解惑</text
>
</view>
@ -85,6 +86,7 @@
:scroll-top="scrollTop"
:show-scrollbar="true"
@scrolltolower="onScrollToLower"
@scrolltoupper="onScrollToUpper"
>
<!-- 头部卡片 -->
<!-- 后端让先注释 -->
@ -112,6 +114,16 @@
<!-- 对话内容区域 -->
<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
v-for="(message, index) in messageGroups"
@ -261,6 +273,14 @@ export default {
currentConversationId: "",
advicePhoneShow: false,
perfectInfoShow: false,
isLoading: false, // loadingMore
isLoadingMore: false, //
noMoreData: false, //
pageQuery: {
PageIndex: 1,
PageSize: 20,
},
questionList: [
"学习哪些专业比较好?",
@ -360,7 +380,10 @@ export default {
//
messageGroups: {
handler() {
this.scrollToBottom();
//
if (!this.isLoadingMore) {
this.scrollToBottom();
}
},
deep: true,
},
@ -376,18 +399,7 @@ export default {
deep: true,
},
},
onLoad() {
//
// uni.getImageInfo({
// src: '/static/common/images/images_bg.png',
// success: () => {
// 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;
// 使nextTickDOM
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);
// scrollTopscrollToView
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();
// scrollTopscrollToView
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();
// interactMode01
if (timeA === timeB) {
return a.interactMode - b.interactMode;
}
//
return timeA - timeB;
});
this.messageGroups = [
...nextPageMessageGroups,
...this.messageGroups,
];
})
.finally(() => {
setTimeout(() => {
this.isLoading = false;
}, 300);
});
},
},
};
</script>
@ -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;