From 0a7f6afbb1601eca88fd4e9d759168b307afe13f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E9=A3=9E=E6=B4=8B?= <15778543+xufeiyang6017@user.noreply.gitee.com> Date: Tue, 13 Jan 2026 16:25:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8E=A5=E6=94=B6=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E6=97=A0=E6=B3=95=E8=87=AA=E5=8A=A8=E6=BB=9A=E5=8A=A8?= =?UTF-8?q?=E5=88=B0=E6=9C=80=E6=96=B0=E6=B6=88=E6=81=AF=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/chat-detail/index.js | 68 +++++++++++++++++++++++++++++++++----- 1 file changed, 59 insertions(+), 9 deletions(-) diff --git a/pages/chat-detail/index.js b/pages/chat-detail/index.js index 3ddf3b5..05924de 100644 --- a/pages/chat-detail/index.js +++ b/pages/chat-detail/index.js @@ -190,17 +190,67 @@ Page({ this.setData({ messages: newMessages }, () => { - // 检查是否需要滚动到底部 - if (this.data.isAtBottom) { - this.scrollToBottom(true); - } else { - // 显示新消息提示 - this.setData({ - hasNewMessages: true, - newMessageCount: this.data.newMessageCount + 1 - }); + // 检查页面是否已卸载 + if (this.isUnloaded) { + console.log('页面已卸载,忽略滚动和新消息检测'); + return; } + // 重新计算是否在底部 + wx.createSelectorQuery().in(this) + .select('#chatScrollView') + .boundingClientRect() + .exec((rects) => { + if (rects && rects[0]) { + const scrollViewRect = rects[0]; + + // 获取scroll-view的滚动信息 + wx.createSelectorQuery().in(this) + .select('#chatScrollView') + .scrollOffset() + .exec((scrollOffsets) => { + if (scrollOffsets && scrollOffsets[0]) { + const { scrollTop } = scrollOffsets[0]; + + // 计算滚动内容的总高度 + wx.createSelectorQuery().in(this) + .select('.chat-messages') + .boundingClientRect() + .exec((messageRects) => { + if (messageRects && messageRects[0]) { + const messagesHeight = messageRects[0].height; + const clientHeight = scrollViewRect.height; + + // 计算滚动位置与底部的距离 + const distanceToBottom = messagesHeight - scrollTop - clientHeight; + // 当距离底部小于100rpx(约50px)时,视为在底部 + const isActuallyAtBottom = distanceToBottom < 50; + + console.log('接收消息时的滚动检测:', { + scrollTop, + messagesHeight, + clientHeight, + distanceToBottom, + isActuallyAtBottom + }); + + // 如果在底部,自动滚动到最新消息 + if (isActuallyAtBottom) { + this.scrollToBottom(true); + } else { + // 否则显示新消息提示 + this.setData({ + hasNewMessages: true, + newMessageCount: this.data.newMessageCount + 1 + }); + } + } + }); + } + }); + } + }); + // 短暂延迟后移除isNew标记 setTimeout(() => { if (this.isUnloaded) return;