From 328e470c27b63f1936e25a3158cb035d16a8055b Mon Sep 17 00:00:00 2001 From: JayJiaJun Date: Tue, 27 May 2025 15:23:19 +0800 Subject: [PATCH] 5.27 --- src/views/home/home.vue | 132 +++++++++++++++++++-------------- src/views/system-info/info.vue | 43 +++++++---- 2 files changed, 103 insertions(+), 72 deletions(-) diff --git a/src/views/home/home.vue b/src/views/home/home.vue index 3103f9e..0a5d3c2 100644 --- a/src/views/home/home.vue +++ b/src/views/home/home.vue @@ -6,31 +6,14 @@ 信安智能 - -
- - - - - - - - - - - + +
@@ -40,64 +23,102 @@ + \ No newline at end of file diff --git a/src/views/system-info/info.vue b/src/views/system-info/info.vue index d5f3d81..fdbdf7d 100644 --- a/src/views/system-info/info.vue +++ b/src/views/system-info/info.vue @@ -112,16 +112,16 @@ export default { // 检查环境是否为本地 const isLocal = window.location.hostname === '192.168.4.1'; - const MQTT_send = (send_string) => { - console.log(" 向父页面发送消息:", JSON.stringify(send_string)); - // 使用 postMessage 发送数据给父页面 - window.parent.postMessage( - { - data: send_string - }, - '*' // 目标 origin,生产环境应替换为具体的父页面域名 - ); - }; + const MQTT_send = (send_string) => { + console.log(" 向父页面发送消息:", JSON.stringify(send_string)); + // 使用 postMessage 发送数据给父页面 + window.parent.postMessage( + { + data: send_string + }, + '*' // 目标 origin,生产环境应替换为具体的父页面域名 + ); + }; const handleMQTTMessage = (string) => { console.log("MQTT 接收:" + string); @@ -245,8 +245,13 @@ export default { // MQTT 接收函数 const MQTT_recv = (string) => { - console.log("MQTT 接收的json:" + string); - handleMQTTMessage(string); + // console.log("MQTT 接收的json:" + string); + // 先解析一次字符串化的 JSON + const parsedString = JSON.parse(string); + // 如果解析后的结果仍然是字符串,需要再次解析 + const data = typeof parsedString === 'string' ? JSON.parse(parsedString) : parsedString; + console.log("MQTTRECV 接收的json:" + JSON.stringify(data)); + handleMQTTMessage(JSON.stringify(data)); }; // 下拉刷新事件处理函数 const onRefresh = async () => { @@ -278,12 +283,20 @@ export default { // 在组件挂载时建立 MQTT 连接 onMounted(() => { console.log(isLocal); - onRefresh(); - window.addEventListener('message', function (event) { - console.log("接受的原始数据", JSON.stringify(event.data)) + const messageHandler = function (event) { + console.log("111接受的原始数据", JSON.stringify(event.data)) if (event.data) { MQTT_recv(JSON.stringify(event.data)); // 保持与原 MQTT_recv 兼容 } + }; + window.addEventListener('message', messageHandler); + // onRefresh(); + + // 在组件卸载时移除事件监听器 + onBeforeUnmount(() => { + window.removeEventListener('message', messageHandler); + console.log('移除事件监听器'); + }); });