Parcourir la source

提交优化代码

wangming il y a 2 mois
Parent
commit
056d36f235

+ 7 - 0
src/App.vue

@@ -1,3 +1,9 @@
+<template>
+    <view>
+        <alarModel ref="alarModel" />
+    </view>
+</template>
+
 <script>
 import Vue from "vue";
 import { mapState, mapMutations, mapActions } from "vuex";
@@ -30,6 +36,7 @@ export default {
 
     onLaunch: function () {
         console.log("App Launch");
+        // this.$refs.alarModel.connectMQTTwo();
         // 初始化云开发
         if (wx.cloud) {
             wx.cloud.init({});

+ 16 - 1
src/components/component/alarModel.vue

@@ -411,10 +411,20 @@ export default {
         },
         // mqtt相关方法
         connectMQTTwo() {
-            if (this.mqttClientTwo) {
+            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);
+                }
+            }
 
             const THRESHOLD = 2;
             const params = {
@@ -540,10 +550,15 @@ export default {
         closemqtt() {
             if (this.mqttClientTwo) {
                 this.mqttClientTwo.end();
+                this.mqttClientTwo = null;
                 console.log("MQTT连接已断开");
             }
         },
     },
+    created() {
+        // 在组件挂载后连接MQTT
+        this.connectMQTTwo();
+    },
 };
 </script>
 

+ 4 - 2
src/pages/home/home.vue

@@ -328,7 +328,7 @@ export default {
             this.getShareNum();
             this.getWarmList();
             this.getWeather();
-            this.$refs.alarModel.connectMQTTwo();
+            // this.$refs.alarModel.connectMQTTwo();
         }
         // 配置服务器
         let selectedService = uni.getStorageSync("sercviceChoice");
@@ -336,7 +336,9 @@ export default {
             uni.setStorageSync("sercviceChoice", "aloneServe");
         }
     },
-    onHide() {},
+    onHide() {
+        // this.$refs.alarModel.closemqtt();
+    },
 
     onShareAppMessage() {
         return {

+ 2 - 2
src/pagesA/OTA/OTA.vue

@@ -69,7 +69,7 @@
             v-if="devsClientId.length > 0 && otaList.length > 0"
             >更新OTA</view
         >
-        <alarModel ref="alarModel" />
+        <!-- <alarModel ref="alarModel" /> -->
     </view>
 </template>
 
@@ -186,7 +186,7 @@ export default {
         },
     },
     onLoad() {
-        this.$refs.alarModel.connectMQTTwo();
+        // this.$refs.alarModel.connectMQTTwo();
     },
     onShow() {
         this.queryList();

+ 2 - 2
src/pagesA/abnormalInfo/abnormalInfo.vue

@@ -64,7 +64,7 @@
             </view>
         </template>
         <view style="height: 20rpx"></view>
-        <alarModel ref="alarModel" />
+        <!-- <alarModel ref="alarModel" /> -->
     </view>
 </template>
 
@@ -160,7 +160,7 @@ export default {
         });
     },
     onLoad() {
-        this.$refs.alarModel.connectMQTTwo();
+        // this.$refs.alarModel.connectMQTTwo();
     },
 };
 </script>

+ 2 - 2
src/pagesA/adDevice/adDevice.vue

@@ -222,7 +222,7 @@
                 </view>
             </view>
         </view>
-        <alarModel ref="alarModel" />
+        <!-- <alarModel ref="alarModel" /> -->
     </view>
 </template>
 <script>
@@ -594,7 +594,7 @@ export default {
         } else {
             console.log("没有参数");
         }
-        this.$refs.alarModel.connectMQTTwo();
+        // this.$refs.alarModel.connectMQTTwo();
     },
 
     onUnload(options) {

+ 5 - 13
src/pagesA/deviceDetail/deviceDetail.vue

@@ -779,18 +779,10 @@ export default {
         try {
             const devItem = this.parseDeviceItem(options.devItem);
             const { devId, clientId } = devItem;
-            Promise.all([this.getdevInfo(devId), this.getdevRoomInfo(devId)])
-                .then(() => {
-                    this.connectMQTT(clientId, devId);
-                    this.getFrequency(devId);
-                })
-                .catch((error) => {
-                    // console.error("初始化失败:", error);
-                    uni.showToast({
-                        title: "初始化设备信息失败",
-                        icon: "none",
-                    });
-                });
+            this.connectMQTT(clientId, devId);
+            this.getFrequency(devId);
+            this.getdevInfo(devId);
+            this.getdevRoomInfo(devId);
         } catch (error) {
             uni.showToast({
                 title: "设备信息格式错误",
@@ -815,7 +807,7 @@ export default {
         }
     },
     onShow() {
-        this.$refs.alarModel.connectMQTTwo();
+        // this.$refs.alarModel.connectMQTTwo();
     },
 };
 </script>

+ 2 - 2
src/pagesA/deviceSetting/deviceSetting.vue

@@ -38,7 +38,7 @@
                 <view class="btn2" @click="unbindDevice">解绑设备</view>
             </view>
         </view>
-        <alarModel ref="alarModel" />
+        <!-- <alarModel ref="alarModel" /> -->
     </view>
 </template>
 <script>
@@ -112,7 +112,7 @@ export default {
         console.log(this.devInfo);
     },
     onShow() {
-        this.$refs.alarModel.connectMQTTwo();
+        // this.$refs.alarModel.connectMQTTwo();
         this.getPosition(this.devInfo.installPosition);
     },
 };

+ 1 - 1
src/pagesA/devices/devices.vue

@@ -216,7 +216,7 @@ export default {
     onLoad() {},
     onShow() {
         this.queryList();
-        this.$refs.alarModel.connectMQTTwo();
+        // this.$refs.alarModel.connectMQTTwo();
     },
     onHide() {},
 

+ 2 - 2
src/pagesA/homeDetail/homeDetail.vue

@@ -18,7 +18,7 @@
                 >
             </view>
         </view>
-        <alarModel ref="alarModel" />
+        <!-- <alarModel ref="alarModel" /> -->
     </view>
 </template>
 
@@ -99,7 +99,7 @@ export default {
     },
     onLoad(options) {
         this.groupInfo = JSON.parse(options.groupInfo);
-        this.$refs.alarModel.connectMQTTwo();
+        // this.$refs.alarModel.connectMQTTwo();
         console.log(this.groupInfo, 99999);
     },
     onShow() {

+ 2 - 2
src/pagesA/homeManage/homeManage.vue

@@ -82,7 +82,7 @@
                 </view>
             </view>
         </view>
-        <alarModel ref="alarModel" />
+        <!-- <alarModel ref="alarModel" /> -->
     </view>
 </template>
 
@@ -206,7 +206,7 @@ export default {
     },
     onShow() {
         this.getgroupList();
-        this.$refs.alarModel.connectMQTTwo();
+        // this.$refs.alarModel.connectMQTTwo();
     },
     onLoad() {},
 

+ 2 - 2
src/pagesA/linkShare/linkShare.vue

@@ -73,7 +73,7 @@
             确认分享
         </button>
         <button class="btn1" v-else @click="unShare()">暂不可分享</button>
-        <alarModel ref="alarModel" />
+        <!-- <alarModel ref="alarModel" /> -->
     </view>
 </template>
 <script>
@@ -164,7 +164,7 @@ export default {
         setTimeout(() => {
             this.shareUserLink("分享链接创建成功");
         }, 500);
-        this.$refs.alarModel.connectMQTTwo();
+        // this.$refs.alarModel.connectMQTTwo();
     },
 };
 </script>

+ 2 - 2
src/pagesA/roomSetting/roomSetting.vue

@@ -230,7 +230,7 @@
                 </view>
             </view>
         </view>
-        <alarModel ref="alarModel" />
+        <!-- <alarModel ref="alarModel" /> -->
     </view>
 </template>
 <script>
@@ -705,7 +705,7 @@ export default {
         this.devId = options.devId;
         this.getdevInfo(this.devId);
         this.getRoomInfo(this.devId);
-        this.$refs.alarModel.connectMQTTwo();
+        // this.$refs.alarModel.connectMQTTwo();
     },
     onShow() {},
 };

+ 2 - 2
src/pagesA/serChange/serChange.vue

@@ -21,7 +21,7 @@
                 <button class="btn" @click="confirm">确认</button>
             </view>
         </view>
-        <alarModel ref="alarModel" />
+        <!-- <alarModel ref="alarModel" /> -->
     </view>
 </template>
 
@@ -66,7 +66,7 @@ export default {
         },
     },
     onShow() {
-        this.$refs.alarModel.connectMQTTwo();
+        // this.$refs.alarModel.connectMQTTwo();
         let selectedValue = uni.getStorageSync("sercviceChoice")
             ? uni.getStorageSync("sercviceChoice")
             : "aloneServe";

+ 2 - 2
src/pagesA/shareList/shareList.vue

@@ -60,7 +60,7 @@
                 </view>
             </view>
         </template>
-        <alarModel ref="alarModel" />
+        <!-- <alarModel ref="alarModel" /> -->
     </view>
 </template>
 
@@ -158,7 +158,7 @@ export default {
         },
     },
     onShow() {
-        this.$refs.alarModel.connectMQTTwo();
+        // this.$refs.alarModel.connectMQTTwo();
         this.getShareList();
     },
 

+ 2 - 2
src/pagesA/sharePages/sharePages.vue

@@ -54,7 +54,7 @@
                 <view class="meanRight" @click="refuseDevice(item)">拒绝</view>
             </view>
         </view>
-        <alarModel ref="alarModel" />
+        <!-- <alarModel ref="alarModel" /> -->
     </view>
 </template>
 <script>
@@ -165,7 +165,7 @@ export default {
         this.devInfo = JSON.parse(options.devInfo);
         this.sharePhone = options.phone;
         this.shareId = options.shareId;
-        this.$refs.alarModel.connectMQTTwo();
+        // this.$refs.alarModel.connectMQTTwo();
     },
     onShow() {
         let userId = uni.getStorageSync("userId");