diff --git a/resources/js/chat-room/context.js b/resources/js/chat-room/context.js new file mode 100644 index 0000000..d968436 --- /dev/null +++ b/resources/js/chat-room/context.js @@ -0,0 +1,75 @@ +// 聊天室上下文初始化模块,从 Blade 输出的 JSON 数据节点恢复 window.chatContext。 + +const CHAT_CONTEXT_ELEMENT_ID = "chat-context-data"; + +/** + * 读取并解析 Blade 注入的聊天室上下文 JSON。 + * + * @returns {Record} + */ +function readChatContextPayload() { + const element = document.getElementById(CHAT_CONTEXT_ELEMENT_ID); + + if (!element) { + return {}; + } + + try { + return JSON.parse(element.textContent || "{}"); + } catch (error) { + console.error("[chatContext] 初始化数据解析失败", error); + return {}; + } +} + +/** + * 使用模板生成带 ID 的 URL。 + * + * @param {string} template URL 模板 + * @param {number|string} id 资源 ID + * @returns {string} + */ +function fillIdTemplate(template, id) { + return String(template || "").replace("__ID__", encodeURIComponent(id)); +} + +/** + * 恢复婚姻系统 URL 工厂函数,兼容存量调用方式。 + * + * @param {Record} context 聊天室上下文 + * @returns {void} + */ +function hydrateMarriageUrlFactories(context) { + const marriage = context.marriage || {}; + + marriage.acceptUrl = (id) => fillIdTemplate(marriage.acceptUrlTemplate, id); + marriage.rejectUrl = (id) => fillIdTemplate(marriage.rejectUrlTemplate, id); + marriage.divorceUrl = (id) => fillIdTemplate(marriage.divorceUrlTemplate, id); + marriage.confirmDivorceUrl = (id) => fillIdTemplate(marriage.confirmDivorceUrlTemplate, id); + marriage.rejectDivorceUrl = (id) => fillIdTemplate(marriage.rejectDivorceUrlTemplate, id); + marriage.weddingSetupUrl = (id) => fillIdTemplate(marriage.weddingSetupUrlTemplate, id); + marriage.claimEnvelopeUrl = (id, ceremonyId) => fillIdTemplate( + fillIdTemplate(marriage.claimEnvelopeUrlTemplate, id), + ceremonyId, + ); + marriage.envelopeStatusUrl = (id) => fillIdTemplate(marriage.envelopeStatusUrlTemplate, id); + + context.marriage = marriage; +} + +/** + * 初始化 window.chatContext,供后续聊天室模块和存量 Blade 脚本读取。 + * + * @returns {Record} + */ +export function initializeChatContext() { + // Blade 底部仍有同步脚本会立即读取 chatContext;若兼容桥已初始化,这里只做函数补齐。 + const context = window.chatContext || readChatContextPayload(); + hydrateMarriageUrlFactories(context); + + window.chatContext = context; + + return context; +} + +initializeChatContext(); diff --git a/resources/js/chat.js b/resources/js/chat.js index 47b6278..d1fd464 100644 --- a/resources/js/chat.js +++ b/resources/js/chat.js @@ -1,4 +1,5 @@ import "./bootstrap"; +import "./chat-room/context.js"; import "./chat-room.js"; // 这个文件负责处理浏览器与 Laravel Reverb WebSocket 服务器的通信。 diff --git a/resources/views/chat/frame.blade.php b/resources/views/chat/frame.blade.php index 1ce7c7d..1007af6 100644 --- a/resources/views/chat/frame.blade.php +++ b/resources/views/chat/frame.blade.php @@ -36,114 +36,112 @@ $fishingCooldownSeconds = \Illuminate\Support\Facades\Redis::ttl($fishingCooldownKey); $fishingCooldownSeconds = $fishingCooldownSeconds > 0 ? $fishingCooldownSeconds : 0; @endphp - + @vite(['resources/css/app.css', 'resources/js/app.js', 'resources/js/chat.js'])