在聊天发送信息
data: {
return {
form: {
page: 0
},
}
}
/** 加载最新的数据以及向上滚动加载历史更多数据**/
async getChatDataOlds() {
this.isLoadingMore = true;
const res = await getChatDataOld(this.form.id, this.form.page)
if (res && res.length > 0) {
let list = res.reverse();
list = list.concat(this.messagescontent);
this.messagescontent = list;
this.form.page = this.messagescontent[0].id;
this.splitArrayByTimestampDiff(this.messagescontent);
// 加载新消息后稍微向下滚动一点
this.$nextTick(() => {
const scrollPositionBefore = this.$refs.messagesContainer.scrollTop;
const newScrollHeight = this.$refs.messagesContainer.scrollHeight;
let scrollToPosition = scrollPositionBefore + Math.min(400, newScrollHeight - scrollPositionBefore - this.$refs.messagesContainer.clientHeight);
if (scrollToPosition < newScrollHeight) {
this.$refs.messagesContainer.scrollTop = scrollToPosition;
}
});
} else {
this.hasMore = false;
}
this.isLoadingMore = false;
},
/** 点击切换用户 **/
handleBgColor(val, item) {
this.inputMessage = ""
this.leftList[val].unreadNumber = 0;
this.userName = item.trueName
this.highlightedIndex = val
this.form.userId = 'PC_' + item.userId
this.form.id = item.userReceiveId
this.form.chatType = item.chatType
this.form.page = 0
this.messagescontent = []
if (this.form.id) {
this.getChatDataOlds()
}
},
/** 向上滚动加载**/
handleScroll() {
const scrollPosition = this.$refs.messagesContainer.scrollTop;
if (scrollPosition < 50 && this.hasMore && !this.isLoadingMore) {
// this.getChatDataOlds(this.form.id, this.form.page);
this.getChatDataOlds();
}
},
this.form.id是对方的聊天用户id,this.form.page初始化为0,是拿到getChatDataOld最新的数据,在这些代码中向上加载拿到this.messagescontent[0].id下标第一次的id加载更多历史数据,现在遇到的问题是第一次点击切换用户,往上加载更多历史数据没有问题,当切换用户再切换回来,向上滚动不会再加载历史更多数据了,大佬们,怎么处理这个呢