diff --git a/App.vue b/App.vue index 7ce68f6..265d0ca 100644 --- a/App.vue +++ b/App.vue @@ -14,18 +14,6 @@ globalData: {}, onLaunch: function() { console.log('App Launch') - // #ifdef H5 - this.mqtt = new mqttUtil(); - // #endif - - // 监听mqtt连接命令 - uni.$on("mqtt-link", (userId) => { - this.mqttLink(userId); - }); - // 监听mqtt连接订阅命令 - uni.$on("mqtt-subscribes", topics => { - this.mqttSubscribes(); - }); }, onShow: function() { console.log('App Show') @@ -45,9 +33,25 @@ this.dc.setStations(this.indexData.device.stations); this.dc.setFlapValves(this.indexData.device.flapValves); this.dc.setValves(this.indexData.selectItem.id, this.indexData.device.waterOutlets); + + const obj = this.dc.topic; + if (obj.newV && obj.newV.length) { + this.mqttSubscribes(obj.newV); //订阅主题 + } + if (obj.oldV && obj.oldV.length) { + this.mqttUnsubscribes(obj.oldV); //取消订阅主题 + } + + this.indexData.setStorage(3, { + userId: this.indexData.userId, + landId: this.indexData.selectItem.id, + stations: this.dc.stations, + flapValves: this.dc.flapValves, + waterOutlets: this.dc.valves, + }); } uni.hideLoading(); - // console.error("indexDataHandler_2:", { + // console.error("indexDataHandler:", { // e: e, // data: data, // indexData: this.indexData, @@ -58,14 +62,18 @@ mqttLink(userId) { this.mqtt.link(userId); }, - //Mqtt连接 + //Mqtt订阅 mqttSubscribes(topics) { this.mqtt.subscribes(topics); }, - //Mqtt连接 + //Mqtt取消订阅 mqttUnsubscribes(topics) { this.mqtt.unsubscribes(topics); }, + //Mqtt断开连接 + mqttEnd() { + this.mqtt.over(); + }, //Mqtt消息回调 mqttMsgHandler(topic, data) { console.error("mqttMsgHandler:", { @@ -73,11 +81,6 @@ data: data, }); }, - //Mqtt断开连接 - mqttEnd() { - this.mqtt.over(); - }, - } } diff --git a/pages/index.vue b/pages/index.vue index 449aa5e..429aebe 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -12,6 +12,7 @@ + @@ -98,7 +99,7 @@ + :class="[ mqtt.connected ? 'font-green':'font-red' ]"> @@ -150,6 +151,11 @@ + + + @@ -246,15 +252,14 @@ - 全开 - + @@ -381,14 +386,7 @@ - mqttLink - mqttEnd - 获取信息 - - connected:{{mqtt.connected}} - - +
connected:{{mqtt.connected}}