修复:聊天室初次加载时附带历史消息,解决因网络延迟错失入场欢迎语的问题

This commit is contained in:
2026-02-28 11:17:09 +08:00
parent cb2e962116
commit 9a98bdfbe6
2 changed files with 23 additions and 5 deletions

View File

@@ -122,14 +122,16 @@ class ChatController extends Controller
}
// 5. 获取历史消息用于初次渲染
// TODO: 可在前端通过请求另外的接口拉取历史记录,或者直接在这里 attach
// 获取最近的所有消息(由于 Redis list 已限制保留条数,传 0 为拉取所有缓存的记录)
$historyMessages = $this->chatState->getNewMessages($id, 0);
// 渲染主聊天框架视图
return view('chat.frame', [
'room' => $room,
'user' => $user,
'weekEffect' => $this->shopService->getActiveWeekEffect($user), // 周卡特效(登录自动播放)
'newbieEffect' => $newbieEffect, // 新人入场专属特效
'room' => $room,
'user' => $user,
'weekEffect' => $this->shopService->getActiveWeekEffect($user), // 周卡特效(登录自动播放)
'newbieEffect' => $newbieEffect, // 新人入场专属特效
'historyMessages' => $historyMessages, // 把历史消息附带给前端
]);
}

View File

@@ -103,6 +103,22 @@
@include('chat.partials.scripts')
{{-- 页面初始加载时,渲染自带的历史记录(解决入场欢迎语错过断网的问题) --}}
@if (!empty($historyMessages))
<script>
document.addEventListener('DOMContentLoaded', () => {
const historyMsgs = @json($historyMessages);
if (historyMsgs && historyMsgs.length > 0) {
// 全局函数 appendMessage 在 scripts.blade.php 中定义
historyMsgs.forEach(msg => {
if (typeof window.appendMessage === 'function') {
window.appendMessage(msg);
}
});
}
});
</script>
@endif
{{-- 进房特效自动播放:新人烟花礼包 / 周卡特效 --}}
@if (!empty($newbieEffect) || !empty($weekEffect))
<script>