refactor(home): 提取消息排序逻辑并添加分页回退功能
This commit is contained in:
parent
ead74baecb
commit
9cd36adf7b
|
|
@ -658,6 +658,17 @@ export default {
|
|||
this.handleGetConversationDetail();
|
||||
},
|
||||
|
||||
|
||||
// 公共排序:按时间升序;时间相同,用户消息(interactMode=0)在前
|
||||
sortMessages(list = []) {
|
||||
return (list || []).sort((a, b) => {
|
||||
const timeA = new Date(a.sendDate).getTime();
|
||||
const timeB = new Date(b.sendDate).getTime();
|
||||
if (timeA === timeB) return a.interactMode - b.interactMode;
|
||||
return timeA - timeB;
|
||||
});
|
||||
},
|
||||
|
||||
// 刷新当前对话的消息详情
|
||||
handleGetConversationDetail() {
|
||||
this.$u.api
|
||||
|
|
@ -669,19 +680,9 @@ export default {
|
|||
.then((res) => {
|
||||
console.log("GetConversationDetail.....", res.item2);
|
||||
|
||||
const currentList = res.item2 || [];
|
||||
// 将消息按sendDate升序排列,时间相同时用户消息(interactMode=0)排在前面
|
||||
this.messageGroups = 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 = this.sortMessages(currentList);
|
||||
})
|
||||
.finally(() => {
|
||||
// 延迟重置切换对话标志位,确保滚动事件处理完成
|
||||
|
|
@ -691,6 +692,39 @@ export default {
|
|||
});
|
||||
},
|
||||
|
||||
|
||||
// 刷新当前页数据;若当前页为空且页码>1,则自动回退上一页
|
||||
refreshPageWithFallback() {
|
||||
const currentIndex = this.pageQuery.PageIndex || 1;
|
||||
const pageSize = this.pageQuery.PageSize;
|
||||
|
||||
return this.$u.api
|
||||
.GetConversationDetail({
|
||||
"Item1.Id": this.currentConversationId,
|
||||
PageIndex: currentIndex,
|
||||
PageSize: pageSize,
|
||||
})
|
||||
.then((res2) => {
|
||||
const currentList = res2?.item2 || [];
|
||||
if ((!currentList || currentList.length === 0) && currentIndex > 1) {
|
||||
const prevIndex = currentIndex - 1;
|
||||
return this.$u.api
|
||||
.GetConversationDetail({
|
||||
"Item1.Id": this.currentConversationId,
|
||||
PageIndex: prevIndex,
|
||||
PageSize: pageSize,
|
||||
})
|
||||
.then((res3) => {
|
||||
const prevList = res3?.item2 || [];
|
||||
this.messageGroups = this.sortMessages(prevList);
|
||||
this.pageQuery.PageIndex = prevIndex;
|
||||
});
|
||||
} else {
|
||||
this.messageGroups = this.sortMessages(currentList);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// 在开始新对话或选择对话时重置相关状态
|
||||
handleCreateConversation() {
|
||||
// 关闭弹窗
|
||||
|
|
@ -815,21 +849,15 @@ export default {
|
|||
});
|
||||
},
|
||||
|
||||
// 回答反馈:点赞/点踩
|
||||
// 回答反馈:点赞/点踩(统一调用刷新&上一页回退逻辑)
|
||||
handleFeedback(message, isHelp) {
|
||||
// 触发反馈事件,外部可根据id处理
|
||||
console.log("thumb-up", message.id);
|
||||
this.$u.api
|
||||
.ModifyStatus({
|
||||
id: message.id,
|
||||
isHelp: isHelp,
|
||||
})
|
||||
.ModifyStatus({ id: message.id, isHelp })
|
||||
.then((res) => {
|
||||
if (res.succeed) {
|
||||
this.$u.toast("操作成功");
|
||||
// 刷新当前对话的消息详情
|
||||
this.handleGetConversationDetail();
|
||||
}
|
||||
if (!res.succeed) return;
|
||||
this.$u.toast("操作成功");
|
||||
// 刷新当前页;若空则自动回退上一页并刷新
|
||||
this.refreshPageWithFallback();
|
||||
});
|
||||
},
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in New Issue