diff --git a/app/Http/Controllers/ChatController.php b/app/Http/Controllers/ChatController.php index 31fc088..4218242 100644 --- a/app/Http/Controllers/ChatController.php +++ b/app/Http/Controllers/ChatController.php @@ -118,6 +118,7 @@ class ChatController extends Controller $newbieEffect = null; $initialPresenceTheme = null; $initialWelcomeMessage = null; + $initialWelcomeMessages = []; if (! $isAlreadyInRoom) { // 广播 UserJoined 事件,通知房间内的其他人 @@ -154,6 +155,7 @@ class ChatController extends Controller $this->chatState->pushMessage($id, $newbieMsg); broadcast(new MessageSent($id, $newbieMsg)); SaveMessageJob::dispatch($newbieMsg); + $initialWelcomeMessages[] = $newbieMsg; // 广播烟花特效给此时已在房间的其他用户 broadcast(new \App\Events\EffectBroadcast($id, 'fireworks', $user->username))->toOthers(); @@ -186,6 +188,7 @@ class ChatController extends Controller $this->chatState->pushMessage($id, $aiWelcomeMsg); broadcast(new MessageSent($id, $aiWelcomeMsg)); SaveMessageJob::dispatch($aiWelcomeMsg); + $initialWelcomeMessages[] = $aiWelcomeMsg; } // 统一走通用进场播报逻辑,管理员不再发送单独的特殊登录提示。 @@ -214,6 +217,7 @@ class ChatController extends Controller // 把当前这次进房生成的欢迎消息带回前端,确保用户自己也一定能看到。 $initialWelcomeMessage = $generalWelcomeMsg; + $initialWelcomeMessages[] = $generalWelcomeMsg; $this->chatState->pushMessage($id, $generalWelcomeMsg); // 修复:之前使用了 ->toOthers() 导致自己看不到自己的进场提示 @@ -312,6 +316,7 @@ class ChatController extends Controller 'newbieEffect' => $newbieEffect, 'initialPresenceTheme' => $initialPresenceTheme, 'initialWelcomeMessage' => $initialWelcomeMessage, + 'initialWelcomeMessages' => $initialWelcomeMessages, 'historyMessages' => $historyMessages, 'pendingProposal' => $pendingProposalData, 'pendingDivorce' => $pendingDivorceData, diff --git a/resources/js/chat-room/initial-state.js b/resources/js/chat-room/initial-state.js index 7890a6e..73c65a5 100644 --- a/resources/js/chat-room/initial-state.js +++ b/resources/js/chat-room/initial-state.js @@ -56,11 +56,17 @@ function restoreHistoryMessages(initialState) { * @returns {void} */ function appendWelcomeMessage(initialState) { - if (!initialState.welcomeMessage) { + const messages = Array.isArray(initialState.welcomeMessages) && initialState.welcomeMessages.length > 0 + ? initialState.welcomeMessages + : (initialState.welcomeMessage ? [initialState.welcomeMessage] : []); + if (messages.length === 0) { return; } - window.setTimeout(() => appendInitialMessage(initialState.welcomeMessage), 220); + window.setTimeout(() => { + // 本次进房欢迎绕过历史清屏过滤,确保新人礼包和 AI 欢迎能在当前屏看到。 + messages.forEach((message) => appendInitialMessage(message)); + }, 220); } /** diff --git a/resources/views/chat/frame.blade.php b/resources/views/chat/frame.blade.php index d478349..baddd87 100644 --- a/resources/views/chat/frame.blade.php +++ b/resources/views/chat/frame.blade.php @@ -122,6 +122,7 @@ 'historyMessages' => $historyMessages ?? [], 'localClearStorageKey' => "local_clear_msg_id_{$room->id}", 'welcomeMessage' => $initialWelcomeMessage ?? null, + 'welcomeMessages' => $initialWelcomeMessages ?? [], 'entryEffect' => $newbieEffect ?: ($initialPresenceTheme['presence_effect'] ?? ($weekEffect ?? null)), 'presenceTheme' => $initialPresenceTheme ?? null, 'pendingProposal' => $pendingProposal ?? null, diff --git a/tests/Feature/ChatControllerTest.php b/tests/Feature/ChatControllerTest.php index 926b951..3b0e363 100644 --- a/tests/Feature/ChatControllerTest.php +++ b/tests/Feature/ChatControllerTest.php @@ -1009,6 +1009,7 @@ class ChatControllerTest extends TestCase $response->assertOk(); $history = collect($response->viewData('historyMessages')); + $initialWelcomeMessages = collect($response->viewData('initialWelcomeMessages')); $newbieBonusMessage = $history->first(fn (array $message): bool => ($message['welcome_kind'] ?? '') === 'newbie_bonus'); $aiWelcomeMessage = $history->first(fn (array $message): bool => ($message['welcome_kind'] ?? '') === 'ai_newbie_welcome'); @@ -1024,6 +1025,10 @@ class ChatControllerTest extends TestCase $this->assertSame($user->username, $entryMessage['welcome_user']); $this->assertTrue($user->fresh()->has_received_new_gift); $this->assertSame(6666, (int) $user->fresh()->jjb); + $this->assertSame( + ['newbie_bonus', 'ai_newbie_welcome', 'entry_broadcast'], + $initialWelcomeMessages->pluck('welcome_kind')->all(), + ); } /**