فهرست منبع

提交全局弹窗优化代码

wangming 1 ماه پیش
والد
کامیت
39b3f9f86d
3فایلهای تغییر یافته به همراه114 افزوده شده و 54 حذف شده
  1. 95 40
      src/components/component/alarModel.vue
  2. 18 4
      src/main.js
  3. 1 10
      src/utils/globalMqtt.js

+ 95 - 40
src/components/component/alarModel.vue

@@ -645,47 +645,102 @@ export default {
     //     console.log("开始接受消息", "99999");
     // },
     mounted() {
-        console.log("开始接受消息", "8870");
-        // 通过全局 MQTT 客户端订阅主题或监听消息
-        this.$mqttClient.on("message", (topic, message) => {
-            console.log("接收到消息CMD:", JSON.parse(message.toString()));
-            console.log(JSON.parse(message.toString()), "8870");
-            // 处理点位消息
-            let userId = uni.getStorageSync("userId");
-            const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
-            const match = topic.match(noticeMatch);
-            if (!match) return;
-            this.alarmModel = true;
-            const data = JSON.parse(message.toString());
-            this.clientId = data.clientId;
-            this.devName = data.devName;
-            this.sn = data.clientId;
-            this.eventListId = data.eventListId;
-            this.getVoipAuthor(this.clientId, userId);
-            console.log(data, topic, "接收到消息222:");
-            console.log(
-                "接收到消息:",
-                this.clientId,
-                this.devName,
-                this.sn,
-                this.eventListId,
-                data
-            );
-            const now = new Date();
-            const year = now.getFullYear();
-            const month = (now.getMonth() + 1).toString().padStart(2, "0");
-            const day = now.getDate().toString().padStart(2, "0");
-            const hours = now.getHours().toString().padStart(2, "0");
-            const minutes = now.getMinutes().toString().padStart(2, "0");
-            const formattedTime = `${year}-${month}-${day} ${hours}:${minutes}`;
-            this.nowTime = formattedTime;
-            console.log("isWmpf:", isWmpf);
-            if (!isWmpf) {
+        if (this.$mqttClient) {
+            console.log("开始接受mqtt消息111");
+            this.$mqttClient.on("message", (topic, message) => {
+                console.log(
+                    "接收到消息CMD11111:",
+                    JSON.parse(message.toString())
+                );
+                console.log(JSON.parse(message.toString()), "8870");
+                // 处理点位消息
+                let userId = uni.getStorageSync("userId");
+                const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
+                const match = topic.match(noticeMatch);
+                if (!match) return;
+                this.alarmModel = true;
+                const data = JSON.parse(message.toString());
+                this.clientId = data.clientId;
+                this.devName = data.devName;
+                this.sn = data.clientId;
+                this.eventListId = data.eventListId;
+                this.getVoipAuthor(this.clientId, userId);
+                console.log(data, topic, "接收到消息222:");
+                console.log(
+                    "接收到消息:",
+                    this.clientId,
+                    this.devName,
+                    this.sn,
+                    this.eventListId,
+                    data
+                );
+                const now = new Date();
+                const year = now.getFullYear();
+                const month = (now.getMonth() + 1).toString().padStart(2, "0");
+                const day = now.getDate().toString().padStart(2, "0");
+                const hours = now.getHours().toString().padStart(2, "0");
+                const minutes = now.getMinutes().toString().padStart(2, "0");
+                const formattedTime = `${year}-${month}-${day} ${hours}:${minutes}`;
+                this.nowTime = formattedTime;
                 console.log("isWmpf:", isWmpf);
-                this.getVoipDevices();
-            }
-            console.log("接收到消息:", topic, data);
-        });
+                if (!isWmpf) {
+                    console.log("isWmpf:", isWmpf);
+                    this.getVoipDevices();
+                }
+                console.log("接收到消息:", topic, data);
+            });
+        } else {
+            uni.$once("mqtt-ready", () => {
+                console.log("开始接受mqtt消息222");
+                this.$mqttClient.on("message", (topic, message) => {
+                    console.log(
+                        "接收到消息CMD222:",
+                        JSON.parse(message.toString())
+                    );
+                    console.log(JSON.parse(message.toString()), "8870");
+                    // 处理点位消息
+                    let userId = uni.getStorageSync("userId");
+                    const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
+                    const match = topic.match(noticeMatch);
+                    if (!match) return;
+                    this.alarmModel = true;
+                    const data = JSON.parse(message.toString());
+                    this.clientId = data.clientId;
+                    this.devName = data.devName;
+                    this.sn = data.clientId;
+                    this.eventListId = data.eventListId;
+                    this.getVoipAuthor(this.clientId, userId);
+                    console.log(data, topic, "接收到消息222:");
+                    console.log(
+                        "接收到消息:",
+                        this.clientId,
+                        this.devName,
+                        this.sn,
+                        this.eventListId,
+                        data
+                    );
+                    const now = new Date();
+                    const year = now.getFullYear();
+                    const month = (now.getMonth() + 1)
+                        .toString()
+                        .padStart(2, "0");
+                    const day = now.getDate().toString().padStart(2, "0");
+                    const hours = now.getHours().toString().padStart(2, "0");
+                    const minutes = now
+                        .getMinutes()
+                        .toString()
+                        .padStart(2, "0");
+                    const formattedTime = `${year}-${month}-${day} ${hours}:${minutes}`;
+                    this.nowTime = formattedTime;
+                    console.log("isWmpf:", isWmpf);
+                    if (!isWmpf) {
+                        console.log("isWmpf:", isWmpf);
+                        this.getVoipDevices();
+                    }
+                    console.log("接收到消息:", topic, data);
+                });
+            });
+        }
     },
 
     beforeDestroy() {

+ 18 - 4
src/main.js

@@ -17,10 +17,24 @@ Vue.component('tenMinutes', tenMinutes);
 
 Vue.prototype.$http = http;
 
-// 挂载mqtt
-const mqttClient = createMqttClient();
-// 挂载mqtt
-Vue.prototype.$mqttClient = mqttClient;
+// 等待 userId
+function waitForUserId(callback) {
+    let timer = setInterval(() => {
+        const userId = uni.getStorageSync("userId");
+        if (userId) {
+            clearInterval(timer);
+            callback(userId);
+        }
+    }, 2000);
+}
+
+// 初始化 MQTT
+waitForUserId((userId) => {
+    const mqttClient = createMqttClient();
+    Vue.prototype.$mqttClient = mqttClient;
+    uni.$emit("mqtt-ready", mqttClient);
+});
+
 
 // 挂载处理时间的方法
 function timestampToTime(value, type = 0) {

+ 1 - 10
src/utils/globalMqtt.js

@@ -35,16 +35,7 @@ export function createMqttClient() {
         },
     };
     let client = "";
-    // const selectedService = uni.getStorageSync("sercviceChoice");
-    // if (!selectedService || selectedService == "aloneServe") {
-    //     if (__wxConfig.envVersion == "develop") {
-    //         client = mqtt.connect("wxs://cmd.radar-power.cn/mqtt/", params);
-    //     }
-    //     if (__wxConfig.envVersion == "trial") {
-    //         client = mqtt.connect("wxs://cmd.radar-power.cn/mqtt/", params);
-    //     }
-    //     client = mqtt.connect("wxs://cmd.radar-power.cn/mqtt/", params);
-    // }
+
     client = mqtt.connect("wxs://cmd.radar-power.cn/mqtt/", params);
     client.on("connect", () => {
         mqttCmdConnected = true;