fix: 聊天页面默认在最底部

This commit is contained in:
2025-12-23 11:04:45 +08:00
parent 23a7af0426
commit 8e64675fdd

View File

@@ -54,6 +54,12 @@
@scroll="onScroll"
>
<view class="message-list-container" id="msglist">
<!-- 加载指示器 (移到顶部避免滚动到底部时显示在底部) -->
<view v-if="loading" class="loading-spinner">
<view class="spinner"></view>
<text style="font-size: 24rpx; color: #999; margin-left: 16rpx;">加载历史消息...</text>
</view>
<!-- 无更多消息提示 -->
<view v-if="isEmpty" class="empty-state">
<text>没有更多历史消息了</text>
@@ -420,11 +426,6 @@
<view id="chat-bottom-anchor" style="height: 1px; width: 100%;"></view>
</view>
<!-- 加载指示器 (Fix: 移除 u-loading-icon使用纯 CSS 动画替代避免 easycom 文件查找失败) -->
<view v-if="loading" class="loading-spinner">
<view class="spinner"></view>
<text style="font-size: 24rpx; color: #999; margin-left: 16rpx;">加载历史消息...</text>
</view>
<!-- 底部占位防止被输入框遮挡 -->
<!-- Fix 1: 增加底部占位高度至 320rpx确保消息不被悬浮工具栏遮挡 -->
<!-- Fix for blocking issue: Increased to 360rpx just to be super safe -->
@@ -605,6 +606,7 @@ export default {
hasMore: true,
loading: false,
isEmpty: false,
isLoadHistory: false, // 标记是否为触顶加载(而非初始化加载)
// 预置数据结构 (用于发送)
prescriptionData: {
@@ -827,15 +829,25 @@ export default {
/**
* 历史消息加载成功回调
* type: 1=滚动到底部, 0=保持位置
* 修复:确保触顶加载时 type 始终为 0只在初始化时传 type=1
*/
handLoadMessageList(type = 0) {
this.loading = false;
if (typeof ChatManager !== 'undefined') {
this.messages = ChatManager.roomMessages[this.doctorUserInfo.room_id] || [];
}
if (type === 1) {
// 修复如果是触顶加载isLoadHistory=true强制 type 为 0保持滚动位置
// 只有在初始化加载时才滚动到底部type=1 且 isLoadHistory=false
const isHistoryLoad = this.isLoadHistory;
this.isLoadHistory = false; // 重置标记
const shouldScrollToBottom = type === 1 && !isHistoryLoad;
if (shouldScrollToBottom) {
this.scrollToBottom();
} else {
// 触顶加载时保持位置
this.scrollToOldLastMessage();
}
},
@@ -1087,6 +1099,9 @@ export default {
// Fix: Optimistic UI update - Add message locally immediately
this.addMessage(fullMessage);
// 发送消息后立即滚动到底部
this.scrollToBottom();
// 注意:不调用 ChatManager.addMessageToRoom避免触发 new-room-message 事件导致消息重复
// 服务器返回的消息会通过 WebSocket 触发 handleNewRoomMessage由去重逻辑处理
@@ -1273,7 +1288,7 @@ export default {
/**
* 滚动到底部
* 优化:先清空 scroll-into-view,使用 $nextTick 和适当延时,再设置 scroll-into-view
* 修复:优化滚动逻辑,确保 scroll-into-view 正确设置,增加延时确保 DOM 完全更新
* 根据文档scroll-into-view 优先级高于 scroll-top需要避免同时设置
*/
scrollToBottom() {
@@ -1286,7 +1301,14 @@ export default {
setTimeout(() => {
// 设置 scroll-into-view 为底部锚点,触发滚动
this.scrollIntoView = 'chat-bottom-anchor';
}, 200); // 增加延时,等待图片等资源加载完成
// 如果第一次设置失败,再次尝试(确保滚动成功)
setTimeout(() => {
if (this.scrollIntoView !== 'chat-bottom-anchor') {
this.scrollIntoView = 'chat-bottom-anchor';
}
}, 100);
}, 300); // 增加延时,确保 DOM 完全更新和图片等资源加载完成
});
},
@@ -1322,6 +1344,9 @@ export default {
// 在加载历史消息前,记录当前滚动高度
this.getScrollHeight();
// 标记为触顶加载
this.isLoadHistory = true;
this.loading = true;
try {
if (typeof ChatManager !== 'undefined') {