Forráskód Böngészése

feature: 修复报错问题

Damon 7 hónapja
szülő
commit
b5521691f6
2 módosított fájl, 64 hozzáadás és 77 törlés
  1. 63 75
      app.js
  2. 1 2
      pages/index/index.js

+ 63 - 75
app.js

@@ -84,81 +84,80 @@ App({
 
   onHide: function () {},
 
-  /**
-   * 连接设备
-   * mqttCallback:mqtt回调统一方法
-   */
+  ///连接设备
   connect() {
-    this.globalData.client = mqtt.connect(host, this.globalData.options);
-    // 连接成功
-    this.globalData.client.on('connect', () => {
-      const pageinfo = getCurrentPages()[getCurrentPages().length - 1];
-      if (pageinfo.mqttCallback) {
-        pageinfo.mqttCallback("connect")
+    var that = this;
+    that.globalData.client = mqtt.connect(host, that.globalData.options);
+
+    // 设备连接
+    that.globalData.client.on('connect', () => {
+      var page = getCurrentPages()[getCurrentPages().length - 1];
+      if (page.mqttCallback) {
+        page.mqttCallback("connect")
       };
     });
 
-    // 接收消息
-    this.globalData.client.on("message", function (topic, payload) {
-      // wx.hideLoading();
-      const pageinfo = getCurrentPages()[getCurrentPages().length - 1];
-      const thisPageIsIndex = getCurrentPages().length === 1;
-      if (pageinfo.mqttCallback) {
-        if (topic.indexOf("status/onoffline") !== -1) {
-          pageinfo.mqttCallback("message_onoffline", {
+    // 消息回调 wx.hideLoading();
+    that.globalData.client.on("message", function (topic, payload) {
+      var page = getCurrentPages()[getCurrentPages().length - 1];
+      if (topic.indexOf("status/onoffline") !== -1) {
+        ///当前界面回调
+        if (page.mqttCallback) {
+          page.mqttCallback("message_onoffline", {
             topic,
             payload
           });
-          if (!thisPageIsIndex) {
-            getCurrentPages()[0].mqttCallback("message_onoffline", {
-              topic,
-              payload
-            })
-          }
-        } else if (topic.indexOf("user/pub_response") !== -1) {
-          pageinfo.mqttCallback("message", {
+        }
+        ///多个界面回调首页
+        if (getCurrentPages().length != 1) {
+          getCurrentPages()[0].mqttCallback("message_onoffline", {
             topic,
             payload
           });
         }
-      };
+      } else if (topic.indexOf("user/pub_response") !== -1) {
+        page.mqttCallback("message", {
+          topic,
+          payload
+        });
+      }
     });
 
-    //服务器连接异常的回调
-    this.globalData.client.on("error", function (error) {
-      console.log(" 服务器 error 的回调" + error)
-      const pageinfo = getCurrentPages()[getCurrentPages().length - 1];
-      if (pageinfo.mqttCallback) {
-        pageinfo.mqttCallback("error")
-      };
-    })
-
-    // 服务器重连
-    this.globalData.client.on("reconnect", function (errr) {
+    // 重连
+    that.globalData.client.on("reconnect", function (errr) {
       console.log(" 服务器 reconnect的回调", errr);
-      const pageinfo = getCurrentPages()[getCurrentPages().length - 1];
-      if (pageinfo.mqttCallback) {
-        pageinfo.mqttCallback("reconnect")
+      var page = getCurrentPages()[getCurrentPages().length - 1];
+      if (page.mqttCallback) {
+        page.mqttCallback("reconnect")
       };
-    })
-    //服务器断开连接
-    this.globalData.client.on("offline", function (errr) {
+    });
+
+    // 离线回调
+    that.globalData.client.on("offline", function (errr) {
       console.log(" 服务器 offline的回调", errr)
-      const pageinfo = getCurrentPages()[getCurrentPages().length - 1];
-      if (pageinfo.mqttCallback) {
-        pageinfo.mqttCallback("offline")
+      var page = getCurrentPages()[getCurrentPages().length - 1];
+      if (page.mqttCallback) {
+        page.mqttCallback("offline")
       };
-    })
-  },
-
+    });
 
+    // 错误回调
+    that.globalData.client.on("error", function (error) {
+      console.log(" 服务器 error 的回调" + error)
+      var page = getCurrentPages()[getCurrentPages().length - 1];
+      if (page.mqttCallback) {
+        page.mqttCallback("error", {})
+      };
+    });
+  },
 
   // 订阅主题
   subscribe: function (topic, callback) {
-
-    if (this.globalData.client && this.globalData.client.connected) {
+    var that = this;
+    console.log("gasdfqwerqweqr===00==" + topic);
+    if (that.globalData.client && that.globalData.client.connected) {
       //订阅主题
-      this.globalData.client.subscribe(topic, function (err, granted) {
+      that.globalData.client.subscribe(topic, function (err, granted) {
         if (!err) {
           if (callback) {
             callback();
@@ -170,24 +169,16 @@ App({
       })
     } else {
       console.log("服务器已断开");
-      // wx.showToast({
-      //   title: '服务器已断开',
-      //   icon: 'error',
-      //   duration: 2000
-      // })
     }
   },
+
   // 取消订阅
-  unsubscribe: function (Topic) {
-    if (this.globalData.client && this.globalData.client.connected) {
-      this.globalData.client.unsubscribe(Topic);
+  unsubscribe: function (topic) {
+    var that = this;
+    if (that.globalData.client && that.globalData.client.connected) {
+      that.globalData.client.unsubscribe(topic);
     } else {
       console.log('请先连接服务器');
-      // wx.showToast({
-      //   title: '请先连接服务器',
-      //   icon: 'none',
-      //   duration: 2000
-      // })
     }
   },
 
@@ -205,14 +196,16 @@ App({
     if (strings.isEmpty(that.globalData.userInfo)) {
       return;
     }
+
+    console.log("gasdfqwerqweqr===11==" + JSON.stringify(option));
+    console.log("gasdfqwerqweqr===22==" + option.type);
+    console.log("gasdfqwerqweqr===33==" + option.DstDeviceName);
+    console.log("gasdfqwerqweqr===44==" + `ALY_${that.globalData.userInfo.userId}`);
     if (that.globalData.client && that.globalData.client.connected) {
-      // wx.showLoading({
-      //   title: '请稍后',
-      // });
       const data = {
+        "type": option.type,
         "DstDeviceName": option.DstDeviceName,
         "SrcDeviceName": `ALY_${that.globalData.userInfo.userId}`,
-        "type": option.type
       };
       if (option.other) {
         data.other = option.other;
@@ -224,11 +217,6 @@ App({
       });
     } else {
       console.log("服务器已断开");
-      // wx.showToast({
-      //   title: '服务器已断开',
-      //   icon: 'error',
-      //   duration: 2000
-      // })
     }
   },
 

+ 1 - 2
pages/index/index.js

@@ -191,7 +191,6 @@ Page({
   mqttCallback(type, option) {
     // console.log("gadsfadsfadsfa==888===" + type);
     var that = this;
-
     let payloads = null;
     if (option) {
       payloads = JSON.parse(option.payload);
@@ -414,7 +413,7 @@ Page({
       deviceList: deviceList,
     });
 
-    let topic = `/AIrSMArT_${tempDeviceId.split("BLUFI_")[1]}/user/pub_response`;
+    var topic = `/AIrSMArT_${tempDeviceId.split("BLUFI_")[1]}/user/pub_response`;
     app.subscribe(topic);
 
     const obj = {