From ffe35c048d1a20cbfc51a3b09d8d397e967371f1 Mon Sep 17 00:00:00 2001 From: lkddi Date: Sat, 28 Feb 2026 11:54:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E5=8E=86=E5=8F=B2?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E6=9C=8D=E5=8A=A1=E7=AB=AF=E8=BF=87=E6=BB=A4?= =?UTF-8?q?=EF=BC=8C=E5=8F=AA=E5=8A=A0=E8=BD=BD=E4=B8=8E=E5=BD=93=E5=89=8D?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=9B=B8=E5=85=B3=E7=9A=84=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=E4=BB=96=E4=BA=BA=E7=A7=81=E8=81=8A?= =?UTF-8?q?=E5=92=8C=E7=B3=BB=E7=BB=9F=E9=80=9A=E7=9F=A5=E6=B7=B7=E5=85=A5?= =?UTF-8?q?=E5=8C=85=E5=8E=A2=E7=AA=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/ChatController.php | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/ChatController.php b/app/Http/Controllers/ChatController.php index 533ea12..a07de08 100644 --- a/app/Http/Controllers/ChatController.php +++ b/app/Http/Controllers/ChatController.php @@ -121,9 +121,28 @@ class ChatController extends Controller broadcast(new MessageSent($id, $welcomeMsg)); } - // 5. 获取历史消息用于初次渲染 - // 获取最近的所有消息(由于 Redis list 已限制保留条数,传 0 为拉取所有缓存的记录) - $historyMessages = $this->chatState->getNewMessages($id, 0); + // 5. 获取历史消息并过滤,只保留和当前用户相关的消息用于初次渲染 + // 规则:公众发言(to_user=大家 或空)保留;私聊/系统通知只保留涉及本人的 + $allHistory = $this->chatState->getNewMessages($id, 0); + $username = $user->username; + $historyMessages = array_values(array_filter($allHistory, function ($msg) use ($username) { + $toUser = $msg['to_user'] ?? ''; + $fromUser = $msg['from_user'] ?? ''; + $isSecret = !empty($msg['is_secret']); + + // 公众发言(对大家说):所有人都可以看到 + if ($toUser === '大家' || $toUser === '') { + return true; + } + + // 私信 / 悄悄话:只显示发给自己或自己发出的 + if ($isSecret) { + return $fromUser === $username || $toUser === $username; + } + + // 对特定人说话:只显示发给自己或自己发出的(含系统通知) + return $fromUser === $username || $toUser === $username; + })); // 渲染主聊天框架视图 return view('chat.frame', [