From 9cd36adf7bc67d9b4b7ac9f916f2c1d5c9385a20 Mon Sep 17 00:00:00 2001 From: yangzhe Date: Thu, 27 Nov 2025 13:32:10 +0800 Subject: [PATCH] =?UTF-8?q?refactor(home):=20=E6=8F=90=E5=8F=96=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E6=8E=92=E5=BA=8F=E9=80=BB=E8=BE=91=E5=B9=B6=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=88=86=E9=A1=B5=E5=9B=9E=E9=80=80=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/home/index/index.vue | 76 ++++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 24 deletions(-) diff --git a/pages/home/index/index.vue b/pages/home/index/index.vue index 79ea30b..102c81b 100644 --- a/pages/home/index/index.vue +++ b/pages/home/index/index.vue @@ -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(); }); }, },