Bläddra i källkod

优化弹窗事件

wangming 2 månader sedan
förälder
incheckning
61813da070

+ 28 - 17
src/components/component/alarModel.vue

@@ -411,20 +411,20 @@ export default {
         },
         // mqtt相关方法
         connectMQTTwo() {
-            if (this.mqttClientTwo && this.mqttClientTwo.connected) {
-                console.log("MQTT连接已存在,无需重新创建", this.mqttClientTwo);
-                return;
-            }
+            // if (this.mqttClientTwo && this.mqttClientTwo.connected) {
+            //     console.log("MQTT连接已存在,无需重新创建", this.mqttClientTwo);
+            //     return;
+            // }
             // 清理之前的连接
-            if (this.mqttClientTwo) {
-                try {
-                    this.mqttClientTwo.end();
-                    this.mqttClientTwo = null;
-                    console.log("清理旧MQTT连接");
-                } catch (e) {
-                    console.error("清理连接时出错:", e);
-                }
-            }
+            // if (this.mqttClientTwo) {
+            //     try {
+            //         this.mqttClientTwo.end();
+            //         this.mqttClientTwo = null;
+            //         console.log("清理旧MQTT连接");
+            //     } catch (e) {
+            //         console.error("清理连接时出错:", e);
+            //     }
+            // }
 
             const THRESHOLD = 2;
             const params = {
@@ -486,7 +486,7 @@ export default {
             client.on("error", (err) => {
                 setTimeout(() => {
                     this.connectMQTTwo();
-                }, 5000);
+                }, 1000);
             });
 
             client.on("reconnect", () => {});
@@ -556,9 +556,20 @@ export default {
             }
         },
     },
-    created() {
-        // 在组件挂载后连接MQTT
-        this.connectMQTTwo();
+    mounted() {
+        setTimeout(() => {
+            this.connectMQTTwo();
+            console.log("组件挂载,连接MQTT");
+        }, 1000);
+    },
+    // beforeUpdate() {
+    //     this.connectMQTTwo();
+    //     console.log("组件更新2222,断开MQTT连接");
+    // },
+    beforeDestroy() {
+        // 在组件销毁前断开MQTT连接
+        this.closemqtt();
+        console.log("组件销毁3333,断开MQTT连接");
     },
 };
 </script>

+ 3 - 1
src/pages/home/home.vue

@@ -316,7 +316,9 @@ export default {
     },
     onHide() {
         this.isInitAlarm = false;
-        console.log("离开首页,销毁报警组件");
+    },
+    onUnload() {
+        this.isInitAlarm = false;
     },
 
     onShareAppMessage() {

+ 7 - 20
src/pagesA/deviceDetail/deviceDetail.vue

@@ -264,7 +264,7 @@
                 </view>
             </view>
         </view>
-        <!-- <alarModel ref="alarModel" /> -->
+        <alarModel v-if="isInitAlarm" />
     </view>
 </template>
 <script>
@@ -298,20 +298,6 @@ export default {
             endArr: [],
             // mqtt相关
             mqttClient: "",
-            // voip相关
-            // isWmpf: isWmpf,
-            name: "用户",
-            voipDevices: [],
-            authorizeFlag: "",
-            sn: "",
-            isShowPopUp: false,
-            contactList: [],
-            // isWmpf,
-            // envVersion,
-            // apiTypesValid: (isWmpf ? apiTypesWMPF : apiTypesWechat).map(
-            //     (id) => apiTypes[id]
-            // ),
-            apiTypeIndex: 0,
             currentIndex: 0,
             modules: [],
             autoPlayinterval: "",
@@ -324,6 +310,7 @@ export default {
             serviceNumberFlag: true,
             voipFlag: true,
             inactivityTimer: "",
+            isInitAlarm: "",
         };
     },
     computed: {},
@@ -795,22 +782,22 @@ export default {
             this.autoSwipe();
         }, 3000);
         this.getCurrentDate();
+        this.isInitAlarm = true;
     },
     onUnload() {
+        this.isInitAlarm = false;
+        clearInterval(this.autoPlayinterval);
+        clearInterval(this.inactivityTimer);
         if (this.mqttClient) {
             this.mqttClient.end();
         }
-        clearInterval(this.autoPlayinterval);
-        clearInterval(this.inactivityTimer);
     },
     onHide() {
+        this.isInitAlarm = false;
         if (this.mqttClient) {
             this.mqttClient.end();
         }
     },
-    onShow() {
-        // this.$refs.alarModel.connectMQTTwo();
-    },
 };
 </script>
 <style lang="less" scoped>

+ 10 - 3
src/pagesA/deviceSetting/deviceSetting.vue

@@ -38,7 +38,7 @@
                 <view class="btn2" @click="unbindDevice">解绑设备</view>
             </view>
         </view>
-        <!-- <alarModel ref="alarModel" /> -->
+        <alarModel v-if="isInitAlarm" />
     </view>
 </template>
 <script>
@@ -48,6 +48,7 @@ export default {
             devInfo: "",
             devName: "",
             position: "",
+            isInitAlarm: "",
         };
     },
     methods: {
@@ -109,12 +110,18 @@ export default {
     },
     onLoad(options) {
         this.devInfo = JSON.parse(options.devInfo);
-        console.log(this.devInfo);
+        this.isInitAlarm = true;
     },
     onShow() {
-        // this.$refs.alarModel.connectMQTTwo();
+        this.isInitAlarm = true;
         this.getPosition(this.devInfo.installPosition);
     },
+    onUnload() {
+        this.isInitAlarm = false;
+    },
+    onHide() {
+        this.isInitAlarm = false;
+    },
 };
 </script>
 <style lang="less">

+ 7 - 4
src/pagesA/devices/devices.vue

@@ -59,7 +59,7 @@
                     <image src="../../static/ln_small.png" mode="" />
                 </view>
             </view>
-            <alarModel v-if="isInitAlarmTwo" />
+            <alarModel v-if="isInitAlarm" />
         </view>
     </view>
 </template>
@@ -74,6 +74,7 @@ export default {
             option: ["全部", "离线", "在线", "报警"],
             selectValue: 0,
             devs: [],
+            isInitAlarm: false,
         };
     },
     methods: {
@@ -215,12 +216,14 @@ export default {
     },
     onLoad() {},
     onShow() {
-        this.isInitAlarmTwo = true;
+        this.isInitAlarm = true;
         this.queryList();
     },
     onHide() {
-        this.isInitAlarmTwo = false;
-        console.log("离开了设备列表,销毁报警组件");
+        this.isInitAlarm = false;
+    },
+    onUnload() {
+        this.isInitAlarm = false;
     },
 
     onPullDownRefresh() {

+ 9 - 2
src/pagesA/my/my.vue

@@ -48,7 +48,7 @@
         </view>
 
         <view class="logoutBtn" @click="goLogout">退出登录</view>
-        <alarModel ref="alarModel" />
+        <alarModel v-if="isInitAlarm" />
     </view>
 </template>
 
@@ -58,6 +58,7 @@ export default {
         return {
             headerImg: "../../static/headerInfo.png",
             phone: uni.getStorageSync("phone"),
+            isInitAlarm: "",
         };
     },
     methods: {
@@ -133,7 +134,13 @@ export default {
         },
     },
     onLoad() {
-        this.$refs.alarModel.connectMQTTwo();
+        this.isInitAlarm = true;
+    },
+    onHide() {
+        this.isInitAlarm = false;
+    },
+    onUnload() {
+        this.isInitAlarm = false;
     },
 };
 </script>