Ver código fonte

提交mqtt双连接

wangming 1 mês atrás
pai
commit
408d25782c
2 arquivos alterados com 154 adições e 34 exclusões
  1. 153 33
      src/components/component/alarModel.vue
  2. 1 1
      src/main.js

+ 153 - 33
src/components/component/alarModel.vue

@@ -733,42 +733,162 @@ export default {
         //         });
         //     });
         // }
+        // 确保已经连接好再订阅
 
-        let userId = uni.getStorageSync("userId");
+        const userId = uni.getStorageSync("userId");
         const topic = `/mps/wx_${userId}/notice`;
-        this.unsubscribeFn = MqttService.subscribe(
-            "CMD",
-            topic,
-            (topic, message) => {
-                const data = JSON.parse(message);
-                let userId = uni.getStorageSync("userId");
-                const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
-                const match = topic.match(noticeMatch);
-                if (!match) return;
-                // 更新页面数据
-                this.alarmModel = true;
-                this.clientId = data.clientId;
-                this.devName = data.devName;
-                this.sn = data.clientId;
-                this.eventListId = data.eventListId;
-                // VOIP 相关逻辑
-                this.getVoipAuthor(this.clientId, userId);
-                // 格式化时间
-                const now = new Date();
-                this.nowTime = `${now.getFullYear()}-${String(
-                    now.getMonth() + 1
-                ).padStart(2, "0")}-${String(now.getDate()).padStart(
-                    2,
-                    "0"
-                )} ${String(now.getHours()).padStart(2, "0")}:${String(
-                    now.getMinutes()
-                ).padStart(2, "0")}`;
-                if (!isWmpf) {
-                    this.getVoipDevices();
+
+        if (MqttService.cmdConnected) {
+            // 已连接 → 直接订阅
+            this.unsubscribeOther = MqttService.subscribe(
+                "CMD",
+                topic,
+                (message) => {
+                    const data = JSON.parse(message);
+                    let userId = uni.getStorageSync("userId");
+                    const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
+                    const match = topic.match(noticeMatch);
+                    if (!match) return;
+                    // 更新页面数据
+                    this.alarmModel = true;
+                    this.clientId = data.clientId;
+                    this.devName = data.devName;
+                    this.sn = data.clientId;
+                    this.eventListId = data.eventListId;
+                    // VOIP 相关逻辑
+                    this.getVoipAuthor(this.clientId, userId);
+                    // 格式化时间
+                    const now = new Date();
+                    this.nowTime = `${now.getFullYear()}-${String(
+                        now.getMonth() + 1
+                    ).padStart(2, "0")}-${String(now.getDate()).padStart(
+                        2,
+                        "0"
+                    )} ${String(now.getHours()).padStart(2, "0")}:${String(
+                        now.getMinutes()
+                    ).padStart(2, "0")}`;
+                    if (!isWmpf) {
+                        this.getVoipDevices();
+                    }
+                    console.log("处理后的 CMD 数据:", data);
+                },
+                (err) => {
+                    if (!err) console.log(`✅ 页面B 已订阅 ${topic}`);
                 }
-                console.log("处理后的 CMD 数据:", data);
-            }
-        );
+            );
+        } else {
+            // 还没连上 → 等待 mqtt-ready
+            uni.$once("mqtt-ready", () => {
+                this.unsubscribeOther = MqttService.subscribe(
+                    "CMD",
+                    topic,
+                    (message) => {
+                        const data = JSON.parse(message);
+                        let userId = uni.getStorageSync("userId");
+                        const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
+                        const match = topic.match(noticeMatch);
+                        if (!match) return;
+                        // 更新页面数据
+                        this.alarmModel = true;
+                        this.clientId = data.clientId;
+                        this.devName = data.devName;
+                        this.sn = data.clientId;
+                        this.eventListId = data.eventListId;
+                        // VOIP 相关逻辑
+                        this.getVoipAuthor(this.clientId, userId);
+                        // 格式化时间
+                        const now = new Date();
+                        this.nowTime = `${now.getFullYear()}-${String(
+                            now.getMonth() + 1
+                        ).padStart(2, "0")}-${String(now.getDate()).padStart(
+                            2,
+                            "0"
+                        )} ${String(now.getHours()).padStart(2, "0")}:${String(
+                            now.getMinutes()
+                        ).padStart(2, "0")}`;
+                        if (!isWmpf) {
+                            this.getVoipDevices();
+                        }
+                        console.log("处理后的 CMD 数据:", data);
+                    },
+                    (err) => {
+                        if (!err) console.log(`✅ 页面B 已订阅 ${topic}`);
+                    }
+                );
+            });
+        }
+
+        // if (MqttService.cmdConnected) {
+        //     this.unsubscribeFn = MqttService.subscribe(
+        //         "CMD",
+        //         topic,
+        //         (topic, message) => {
+        //             const data = JSON.parse(message);
+        //             let userId = uni.getStorageSync("userId");
+        //             const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
+        //             const match = topic.match(noticeMatch);
+        //             if (!match) return;
+        //             // 更新页面数据
+        //             this.alarmModel = true;
+        //             this.clientId = data.clientId;
+        //             this.devName = data.devName;
+        //             this.sn = data.clientId;
+        //             this.eventListId = data.eventListId;
+        //             // VOIP 相关逻辑
+        //             this.getVoipAuthor(this.clientId, userId);
+        //             // 格式化时间
+        //             const now = new Date();
+        //             this.nowTime = `${now.getFullYear()}-${String(
+        //                 now.getMonth() + 1
+        //             ).padStart(2, "0")}-${String(now.getDate()).padStart(
+        //                 2,
+        //                 "0"
+        //             )} ${String(now.getHours()).padStart(2, "0")}:${String(
+        //                 now.getMinutes()
+        //             ).padStart(2, "0")}`;
+        //             if (!isWmpf) {
+        //                 this.getVoipDevices();
+        //             }
+        //             console.log("处理后的 CMD 数据:", data);
+        //         }
+        //     );
+        // } else {
+        //     uni.$once("mqtt-ready", () => {
+        //         this.unsubscribeFn = MqttService.subscribe(
+        //             "CMD",
+        //             topic,
+        //             (topic, message) => {
+        //                 const data = JSON.parse(message);
+        //                 let userId = uni.getStorageSync("userId");
+        //                 const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
+        //                 const match = topic.match(noticeMatch);
+        //                 if (!match) return;
+        //                 // 更新页面数据
+        //                 this.alarmModel = true;
+        //                 this.clientId = data.clientId;
+        //                 this.devName = data.devName;
+        //                 this.sn = data.clientId;
+        //                 this.eventListId = data.eventListId;
+        //                 // VOIP 相关逻辑
+        //                 this.getVoipAuthor(this.clientId, userId);
+        //                 // 格式化时间
+        //                 const now = new Date();
+        //                 this.nowTime = `${now.getFullYear()}-${String(
+        //                     now.getMonth() + 1
+        //                 ).padStart(2, "0")}-${String(now.getDate()).padStart(
+        //                     2,
+        //                     "0"
+        //                 )} ${String(now.getHours()).padStart(2, "0")}:${String(
+        //                     now.getMinutes()
+        //                 ).padStart(2, "0")}`;
+        //                 if (!isWmpf) {
+        //                     this.getVoipDevices();
+        //                 }
+        //                 console.log("处理后的 CMD 数据:", data);
+        //             }
+        //         );
+        //     });
+        // }
     },
 
     beforeDestroy() {

+ 1 - 1
src/main.js

@@ -19,7 +19,7 @@ Vue.prototype.$http = http;
 Vue.prototype.$initMqtt = function () {
     const userId = uni.getStorageSync("userId");
     if (!userId) {
-        console.warn("MQTT 初始化失败:缺少 userId");
+        console.log("MQTT 初始化失败:缺少 userId");
         return;
     }