Ver código fonte

feature: 处理数据失控的问题

Damon 8 meses atrás
pai
commit
2c0b25e010
4 arquivos alterados com 60 adições e 69 exclusões
  1. 6 0
      app.js
  2. 49 63
      pages/index/index.js
  3. 2 2
      pages/login/login.js
  4. 3 4
      utils/util.js

+ 6 - 0
app.js

@@ -2,6 +2,7 @@
 import btHelper from './devices/bt_helper';
 import mqtt from './utils/mqtt';
 import update from './utils/update';
+import strings from './utils/strings';
 //连接的服务器域名
 // const host = 'wxs://mqtt.test.radio1964.com';
 const host = 'wxs://mqtt.ssl.keepradioon.net:8884'
@@ -43,6 +44,11 @@ App({
 
   onLaunch() {
     var that = this;
+    var userInfo = wx.getStorageSync("userInfo") || "";
+    if (!strings.isEmpty(userInfo)) {
+      that.globalData.userInfo = userInfo;
+    }
+
     wx.onAppHide(() => {
       if (that.globalData.client && that.globalData.client.connected) {
         that.globalData.client.end(true);

+ 49 - 63
pages/index/index.js

@@ -73,79 +73,65 @@ Page({
   onHomeShow() {
     var that = this;
     if ((!that.data.isOneLoading)) {
-      wx.getStorage({
-        key: 'userInfo',
-        fail(res) {
-          // 取消订阅
-          if (that.data.thisDeviceMac !== null) {
-            app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
-          };
-        },
-        success() {
-          if (app.globalData.newDeviceId) {
-            // 获取设备本地数据
-            that.getDeviceData();
-          } else if (that.data.deviceListIndex !== null) {
-            // 更新
-            that.actionDevice(that.data.deviceListIndex);
-          }
+      var userInfo = wx.getStorageSync("userInfo") || "";
+      if (!strings.isEmpty(userInfo)) {
+        // 获取设备本地数据
+        if (app.globalData.newDeviceId) {
+          that.getDeviceData();
         }
-      });
+        // 更新
+        else if (that.data.deviceListIndex !== null) {
+          that.actionDevice(that.data.deviceListIndex);
+        }
+      }
+      // 取消订阅
+      else {
+        if (that.data.thisDeviceMac !== null) {
+          app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
+        };
+      }
     };
   },
 
-  ////登录界面登录成功回调用
-  loginSucceed() {
-
-  },
-
   ///开始时home的
   getDeviceData() {
     var that = this;
-    // 获取设备数据
-    wx.getStorage({
-      key: 'devicelist',
-      success(res) {
-        // wx.hideLoading();
-        if (res.data) {
-          const resData = JSON.parse(res.data);
-          console.log("gadsfadsfadsfa==333===" + JSON.stringify(resData));
-          that.setData({
-            deviceList: resData
-          });
-          // 连接mqtt
-          if (app.globalData.client === null) {
-            app.connect();
-          } else if (app.globalData.newDeviceId) {
-            that.setData({
-              actionIndex: null,
-              deviceListIndex: null,
-            });
+    var devicelist = wx.getStorageSync("devicelist") || "";
+    if (!strings.isEmpty(devicelist)) {
+      const resData = JSON.parse(devicelist);
+      that.setData({
+        deviceList: resData
+      });
 
-            // 监听
-            let topic = `/AIrSMArT_${resData[0].name.split("BLUFI_")[1]}/status/onoffline`;
-            app.subscribe(topic);
-            const Timeout = setTimeout(() => {
-              clearTimeout(Timeout);
-              that.actionDevice(0);
-            }, 1000);
-          }
+      // 连接mqtt
+      if (app.globalData.client === null) {
+        app.connect();
+      } else if (app.globalData.newDeviceId) {
+        that.setData({
+          actionIndex: null,
+          deviceListIndex: null,
+        });
 
-          // // 有新设备
-          // if(app.globalData.newDeviceId) {
-          //   that.setData({
-          //     actionIndex: null,
-          //     deviceListIndex: null,
-          //   });
-          //   let topic = `/AIrSMArT_${resData[0].name.split("BLUFI_")[1]}/status/onoffline`;
-          //   app.subscribe(topic);
-          // }
-        }
-      },
-      fail(e) {
-        // wx.hideLoading();
+        // 监听
+        let topic = `/AIrSMArT_${resData[0].name.split("BLUFI_")[1]}/status/onoffline`;
+        app.subscribe(topic);
+        const Timeout = setTimeout(() => {
+          clearTimeout(Timeout);
+          that.actionDevice(0);
+        }, 1000);
       }
-    });
+
+      // // 有新设备
+      // if(app.globalData.newDeviceId) {
+      //   that.setData({
+      //     actionIndex: null,
+      //     deviceListIndex: null,
+      //   });
+      //   let topic = `/AIrSMArT_${resData[0].name.split("BLUFI_")[1]}/status/onoffline`;
+      //   app.subscribe(topic);
+      // }
+    }
+
   },
 
   // 回调

+ 2 - 2
pages/login/login.js

@@ -71,10 +71,10 @@ Page({
                     });
                     app.globalData.userInfo = res5;
                     wx.setStorageSync("userInfo", res5);
-                    wx.setStorageSync("token", JSON.stringify({
+                    wx.setStorageSync("token", {
                       id: res5.userId,
                       token: res5.accessToken,
-                    }));
+                    });
                     getCurrentPages()[0].onUserInfoLoad();
                     setTimeout(() => {
                       wx.redirectTo({

+ 3 - 4
utils/util.js

@@ -58,10 +58,9 @@ function _show_error(error_code) {
 function request(url, data = {}, method = "GET", header = urlencodeType, isToken = true) {
   if (isToken) {
     try {
-      var value = wx.getStorageSync('token')
-      if (value) {
-        const resData = JSON.parse(value);
-        Authorization = resData.token;
+      var token = wx.getStorageSync("token") || "";
+      if (!strings.isEmpty(token)) {
+        Authorization = value.token;
       }
     } catch (e) {
       Authorization = "";