迁移聊天室初始状态脚本

This commit is contained in:
2026-04-25 14:04:42 +08:00
parent 43d613bfb1
commit f8fb849714
3 changed files with 158 additions and 85 deletions
+11 -85
View File
@@ -124,7 +124,16 @@
envelopeStatusUrl: (id) => `/wedding/${id}/envelope-status`,
},
earnRewardUrl: "{{ route('earn.video_reward') }}",
chatImageRetentionDays: 3
chatImageRetentionDays: 3,
initialState: {
historyMessages: @json($historyMessages ?? []),
localClearStorageKey: "local_clear_msg_id_{{ $room->id }}",
welcomeMessage: @json($initialWelcomeMessage ?? null),
entryEffect: @json($newbieEffect ?: ($initialPresenceTheme['presence_effect'] ?? ($weekEffect ?? null))),
presenceTheme: @json($initialPresenceTheme ?? null),
pendingProposal: @json($pendingProposal ?? null),
pendingDivorce: @json($pendingDivorce ?? null),
}
};
</script>
@vite(['resources/css/app.css', 'resources/js/app.js', 'resources/js/chat.js'])
@@ -236,90 +245,7 @@
{{-- 辅助与全局事件组件 --}}
@include('chat.partials.ai-chatbot')
@include('chat.partials.system-events')
{{-- 页面初始加载时,渲染自带的历史记录(解决入场欢迎语错过断网的问题) --}}
@if (!empty($historyMessages))
<script>
document.addEventListener('DOMContentLoaded', () => {
const historyMsgs = @json($historyMessages);
const clearId = parseInt(localStorage.getItem(`local_clear_msg_id_{{ $room->id }}`) || '0', 10);
if (historyMsgs && historyMsgs.length > 0) {
// 全局函数 appendMessage 在 scripts.blade.php 中定义
historyMsgs.forEach(msg => {
// 如果开启了本地清屏,之前的历史记录不再显示
if (msg.id > clearId) {
if (typeof window.appendMessage === 'function') {
window.appendMessage(msg);
}
}
});
}
});
</script>
@endif
@if (!empty($initialWelcomeMessage))
<script>
document.addEventListener('DOMContentLoaded', () => {
setTimeout(() => {
if (typeof window.appendMessage === 'function') {
window.appendMessage(@json($initialWelcomeMessage));
}
}, 220);
});
</script>
@endif
@if (!empty($newbieEffect) || !empty($weekEffect) || !empty($initialPresenceTheme['presence_effect']))
<script>
/**
* 延迟1秒待页面完成初始化后,自动播放进房附带的特效。
* 优先级:新人礼包特效 -> 会员专属进场特效 -> 周卡特效。
*/
setTimeout(() => {
if (typeof EffectManager !== 'undefined') {
@if (!empty($newbieEffect))
EffectManager.play('{{ $newbieEffect }}');
@elseif (!empty($initialPresenceTheme['presence_effect']))
EffectManager.play('{{ $initialPresenceTheme['presence_effect'] }}');
@elseif (!empty($weekEffect))
EffectManager.play('{{ $weekEffect }}');
@endif
}
}, 1000);
</script>
@endif
@if (!empty($initialPresenceTheme))
<script>
document.addEventListener('DOMContentLoaded', () => {
setTimeout(() => {
if (typeof window.showVipPresenceBanner === 'function') {
window.showVipPresenceBanner(@json($initialPresenceTheme));
}
}, 700);
});
</script>
@endif
{{-- 页面初始加载时,若存在挂起的求婚 / 离婚请求,则弹窗 --}}
@if (!empty($pendingProposal) || !empty($pendingDivorce))
<script>
document.addEventListener('DOMContentLoaded', () => {
// 等待短暂延迟以确保 Alpine 和 window.chatDialog 初始化完成
setTimeout(() => {
@if (!empty($pendingProposal))
window.dispatchEvent(new CustomEvent('chat:marriage-proposed', {
detail: @json($pendingProposal)
}));
@endif
@if (!empty($pendingDivorce))
window.dispatchEvent(new CustomEvent('chat:divorce-requested', {
detail: @json($pendingDivorce)
}));
@endif
}, 800);
});
</script>
@endif
{{-- 初始历史消息、入场欢迎、进场特效、会员横幅和挂起婚姻事件已迁移到 resources/js/chat-room/initial-state.js --}}
</body>