Bladeren bron

提交优化代码

wangming 3 weken geleden
bovenliggende
commit
fd3b3a565a
1 gewijzigde bestanden met toevoegingen van 43 en 31 verwijderingen
  1. 43 31
      src/pagesA/deviceDetail/deviceDetail.vue

+ 43 - 31
src/pagesA/deviceDetail/deviceDetail.vue

@@ -438,11 +438,13 @@ export default {
             },
             index: 0,
             showModle: false,
-            unsubscribeFn: null,
             zoomTimes: 2,
             // 设备分享权限判断
             shareJudge: false,
-            fallingEventChange: false,
+            unsubscribeFn: null,
+            fallingEventChange: null,
+            lnbActionJudean: false,
+            setIntervalVal: null,
         };
     },
     computed: {},
@@ -664,7 +666,7 @@ export default {
 
         autoSwipe() {
             if (this.freQuenceList && this.freQuenceList.length > 0) {
-                setInterval(() => {
+                this.setIntervalVal = setInterval(() => {
                     let nextIndex = this.currentIndex + 1;
                     if (nextIndex >= this.freQuenceList.length) {
                         nextIndex = 0; // 循环到第一个项目
@@ -942,6 +944,7 @@ export default {
             this.zoomTimes = 710 / practical;
             this.normalizeZoomTimes(this.zoomTimes);
             this.zoomTimes = this.zoomTimes - 0.2;
+            this.lnbActionJudean = true;
             this.lnbAction = "action8";
         },
         normalizeZoomTimes(num) {
@@ -994,6 +997,8 @@ export default {
                         );
                         if (dataMatch && dataMatch[1] === this.clientId) {
                             const dataMessage = JSON.parse(message.toString());
+
+                            console.log(dataMessage, 888888);
                             if (dataMessage.falling == 1) {
                                 this.falling = dataMessage.falling;
                                 this.lnbAction = "actionWarn";
@@ -1040,53 +1045,60 @@ export default {
             });
         },
     },
+    onLoad(options) {
+        const devItem = JSON.parse(options.devItem);
+        const { devId, clientId } = devItem;
+
+        // 缓存到页面实例
+        this.clientId = clientId;
+        uni.setStorageSync("currentDevItem", devItem);
+
+        this.getFrequency(devId);
+        this.getdevInfo(devId);
+        this.getdevRoomInfo(devId);
+        this.getCurrentDate();
+    },
+
     onShow() {
         this.showModle = true;
-        this.autoPlayinterval = setTimeout(() => {
-            this.autoSwipe();
-        }, 3000);
-
         this.todayDate = this.$time(new Date(), 2);
-        this.$initMqtt();
 
-        // 初始化订阅
-        this.initSubscriptions();
-        if (MqttService.dataConnected) {
+        // 清理旧定时器
+        clearTimeout(this.autoPlayinterval);
+        this.autoPlayinterval = setTimeout(() => this.autoSwipe(), 3000);
+
+        // MQTT 初始化
+        const userId = uni.getStorageSync("userId");
+        if (!userId) return console.error("MQTT 初始化失败:缺少 userId");
+
+        if (MqttService.dataConnected && MqttService.dataClient) {
+            // 已连接,直接订阅
             this.initSubscriptions();
         } else {
-            const userId = uni.getStorageSync("userId");
+            // 未连接,先 connect
             MqttService.connectData(userId)
                 .then((client) => {
-                    // 触发页面订阅事件
+                    // 页面订阅逻辑,等 connect 成功再执行
                     const handler = () => {
                         this.initSubscriptions();
                         uni.$off("mqttData-ready", handler);
                     };
                     uni.$on("mqttData-ready", handler);
                 })
-                .catch((err) => {
-                    console.error("DATA MQTT 初始化失败", err);
-                });
+                .catch((err) => console.error("DATA MQTT 初始化失败", err));
         }
     },
-    onLoad(options) {
-        const devItem = JSON.parse(options.devItem);
-        const { devId, clientId } = devItem;
-        this.getFrequency(devId);
-        this.getdevInfo(devId);
-        this.getdevRoomInfo(devId);
-        this.getCurrentDate();
-        this.clientId = clientId;
-    },
 
-    onUnload() {
-        // 关闭 DATA 连接
-        // MqttService.disconnectData();
-    },
     onHide() {
-        this.inactivityTimer = "";
+        // 清理定时器
         clearTimeout(this.autoPlayinterval);
-        this.autoPlayinterval = "";
+        // 清理自动滑动定时器
+        clearInterval(this.setIntervalVal);
+        this.setIntervalVal = null;
+
+        this.autoPlayinterval = null;
+
+        // 取消订阅
         ["unsubscribeFn", "fallingEventChange"].forEach((key) => {
             if (this[key]) {
                 this[key]();