Pārlūkot izejas kodu

feature: 频繁切换登录问题

Damon 8 mēneši atpakaļ
vecāks
revīzija
ecf20830e8
1 mainītis faili ar 103 papildinājumiem un 100 dzēšanām
  1. 103 100
      pages/index/index.js

+ 103 - 100
pages/index/index.js

@@ -49,6 +49,7 @@ Page({
 
   onLoad(options) {
     var that = this;
+    that.onDeviceLoad();
     that.onUserInfoLoad();
     that.onBannerLoad();
   },
@@ -65,64 +66,65 @@ Page({
     if (!strings.isEmpty(userInfo)) {
       // 获取设备本地数据
       if (app.globalData.newDeviceId) {
-        that.getDeviceData();
+        that.addNewDeviceId();
       }
       // 更新
       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`);
-      };
-    }
   },
 
   ///开始时home的
-  getDeviceData() {
+  onDeviceLoad() {
     var that = this;
     var devicelist = wx.getStorageSync("devicelist") || "";
     if (!strings.isEmpty(devicelist)) {
-      const resData = JSON.parse(devicelist);
-      that.setData({
-        deviceList: resData
-      });
-
-      // 连接mqtt
-      if (app.globalData.client === null) {
-        app.connect();
-      } else if (app.globalData.newDeviceId) {
-        that.setData({
-          actionIndex: null,
-          deviceListIndex: null,
-        });
+      var list = JSON.parse(devicelist);
+      that.updateDeviceList(list, true);
+    }
+  },
 
-        // 监听
-        let topic = `/AIrSMArT_${resData[0].name.split("BLUFI_")[1]}/status/onoffline`;
-        app.subscribe(topic);
-        const Timeout = setTimeout(() => {
-          clearTimeout(Timeout);
-          that.actionDevice(0);
-        }, 1000);
+  addNewDeviceId() {
+    // 连接mqtt
+    var that = this;
+    if (app.globalData.client === null) {
+      app.connect();
+    } else if (app.globalData.newDeviceId) {
+      //监听
+      var deviceList = that.data.deviceList;
+      if (deviceList.length == 0) {
+        return;
       }
 
-      // // 有新设备
-      // if(app.globalData.newDeviceId) {
-      //   that.setData({
-      //     actionIndex: null,
-      //     deviceListIndex: null,
-      //   });
-      //   let topic = `/AIrSMArT_${resData[0].name.split("BLUFI_")[1]}/status/onoffline`;
-      //   app.subscribe(topic);
-      // }
+      ///连接新添加的设备
+      var deviceListIndex = that.data.deviceListIndex;
+      if (deviceListIndex != null) {
+        if (deviceList.length > deviceListIndex) {
+          if (deviceList[deviceListIndex].deviceId != deviceList[0].deviceId) {
+            that.setData({
+              actionIndex: null,
+              deviceListIndex: null,
+            });
+          }
+        } else {
+          that.setData({
+            actionIndex: null,
+            deviceListIndex: null,
+          });
+        }
+      }
+      var topic = `/AIrSMArT_${deviceList[0].deviceId.split("BLUFI_")[1]}/status/onoffline`;
+      app.subscribe(topic);
+      const Timeout = setTimeout(() => {
+        clearTimeout(Timeout);
+        that.actionDevice(0);
+      }, 500);
     }
   },
 
   // 回调
   mqttCallback(type, option) {
-    // console.log("gadsfadsfqwerq===" + type + "===" + option);
     var that = this;
     let payloads = null;
     if (option) {
@@ -130,18 +132,17 @@ Page({
     };
 
     switch (type) {
+      ///连接成功订阅
       case "connect":
-        that.connectSuccess();
+        that.subscribeDevicesStatus();
         break;
       case "message_onoffline":
         that.online(payloads);
         break;
-
       case "message":
         // 接收设备播放信息
         if (payloads.type === "get_position" && payloads.other) {
           let actionIndex = null;
-
           that.data.channelData.map((v, index) => {
             if (v.channelNum === payloads.other.channel) {
               actionIndex = index;
@@ -183,8 +184,6 @@ Page({
           // 更新信息
           ///连上就调用2次
           that.data.deviceList.map((v, index) => {
-            // console.log("gadsfadsfadsfa==444===" + payloads.SrcDeviceName);
-            // console.log("gadsfadsfadsfa==555===" + JSON.stringify(payloads.other));
 
             // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
             /// payloads.other  
@@ -230,12 +229,6 @@ Page({
     return _battery
   },
 
-  connectSuccess() {
-    // 订阅设备在线信息
-    var that = this;
-    that.subscribeDevicesStatus();
-  },
-
   // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
 
   ///连上就调用2次 处理离线在线问题 wifi设备 BLUFI_
@@ -270,10 +263,7 @@ Page({
 
     ///数据有更新
     if (isUpdate) {
-      var finalList = that.updateDeviceList(deviceList);
-      that.setData({
-        deviceList: finalList,
-      });
+      that.updateDeviceList(deviceList, false);
     }
 
     ///当前没有连接设备,则去连接第一个wifi设备
@@ -300,10 +290,15 @@ Page({
   // 订阅设备在线状态
   subscribeDevicesStatus() {
     var that = this;
-    that.data.deviceList.forEach((value) => {
-      let topic = `/AIrSMArT_${value.name.split("BLUFI_")[1]}/status/onoffline`;
-      app.subscribe(topic);
-    })
+    var deviceList = that.data.deviceList;
+    if (!strings.isEmpty(deviceList)) {
+      for (var i = 0; i < deviceList.length; i++) {
+        if (deviceList[i].connectType == 3) {
+          let topic = `/AIrSMArT_${deviceList[i].deviceId.split("BLUFI_")[1]}/status/onoffline`;
+          app.subscribe(topic);
+        }
+      }
+    }
   },
 
   subscribeCurrDevice() {
@@ -399,24 +394,6 @@ Page({
       if (app.globalData.newDeviceId) {
         app.globalData.newDeviceId = null;
         return;
-        // 去掉此功能,先留着吧
-        // const other= {
-        //   "url": "",
-        //   "media_data": "",
-        //   "user_id": `${app.globalData.userInfo.deviceUid}`,
-        //   "timestamp": `${Math.round(new Date() / 1000)}`,
-        //   "channel_id": `${res[1].channelNum}`,
-        //   "order": "",
-        //   "resource_from": "",
-        //   "songAlbumID":"",
-        //   "version":3,
-        //   "is_debug": app.globalData.is_debug
-        // };
-        // app.PubMsg({
-        //   type: "play",
-        //   DstDeviceName: that.getThisDeviceID(),
-        //   other
-        // });
       }
     })
   },
@@ -524,27 +501,33 @@ Page({
   ///点击item
   // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
   onTapItem(e) {
+    if (!that.data.isLogin) {
+      wx.navigateTo({
+        url: './../login/login'
+      });
+      return;
+    }
+
     var that = this;
     var item = e.currentTarget.dataset.item;
     var connectType = item.connectType;
+    console.log("gadfadfqwwerq===00===" + connectType);
     if (connectType == 3) {
+      console.log("gadfadfqwwerq===11===" + connectType);
       if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
+        console.log("gadfadfqwwerq===22===" + connectType);
         that.goWake();
       } else {
+        console.log("gadfadfqwwerq===33===" + connectType);
         that.actionDevice(e.currentTarget.dataset.index)
       }
       return;
     }
-    console.log(e)
 
     ///去蓝牙连接处理
-    // if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
-    //   that.goWake();
-    // } else {
-    //   that.actionDevice(e.currentTarget.dataset.index)
-    // }
-
-    if (item.state !== "online") {
+    if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
+      route_util.jumpParam('/pages/deviceDetail/detail', JSON.stringify(item))
+    } else {
       that.addConnectBlueDevice({
         deviceId: item.deviceId,
         name: item.devName,
@@ -552,8 +535,6 @@ Page({
         clientType: item.ProdModel,
         mac: item.mac,
       });
-    } else {
-      route_util.jumpParam('/pages/deviceDetail/detail', JSON.stringify(item))
     }
   },
 
@@ -711,18 +692,16 @@ Page({
     });
 
     ///在线排序前面,wifi设备排序前面
-    var finalList = that.updateDeviceList(deviceList);
+    that.updateDeviceList(deviceList, false);
     var indexPage = that.data.indexPage;
     if (indexPage != 0) {
       that.setData({
         indexPage: 0,
         deviceListIndex: 0,
-        deviceList: finalList,
       });
     } else {
       that.setData({
         deviceListIndex: 0,
-        deviceList: finalList,
       });
     }
   },
@@ -730,26 +709,25 @@ Page({
   addConnectWifiDevice(deviceList) {
     var that = this;
     ///在线排序前面,wifi设备排序前面
-    var finalList = that.updateDeviceList(deviceList);
+    that.updateDeviceList(deviceList, false);
     var indexPage = that.data.indexPage;
     if (indexPage != 0) {
       that.setData({
         indexPage: 0,
         deviceListIndex: 0,
-        deviceList: finalList,
       });
     } else {
       that.setData({
         deviceListIndex: 0,
-        deviceList: finalList,
       });
     }
   },
 
   ///更新列表排序
-  updateDeviceList(deviceList) {
+  updateDeviceList(deviceList, isInit) {
     if (deviceList.length == 0) return [];
 
+    var that = this;
     var finalList = [];
     finalList.push(deviceList[0]);
 
@@ -758,11 +736,15 @@ Page({
 
     ///区分在线和离线
     for (var i = 0; i < deviceList.length; i++) {
-      if (i > 0) {
-        if (deviceList[i].state == "online") {
-          onLineList.push(deviceList[i])
-        } else {
-          onNoLineList.push(deviceList[i])
+      if (isInit) {
+        deviceList[i].state = "offline";
+      } else {
+        if (i > 0) {
+          if (deviceList[i].state == "online") {
+            onLineList.push(deviceList[i])
+          } else {
+            onNoLineList.push(deviceList[i])
+          }
         }
       }
     }
@@ -794,7 +776,10 @@ Page({
     finalList = finalList.concat(onNoLineNoWifiList);
     // 更新缓存
     wx.setStorageSync("devicelist", JSON.stringify(finalList));
-    return finalList;
+
+    that.setData({
+      deviceList: finalList
+    });
   },
 
   ///******************************* 可折叠 ********************************************///
@@ -827,7 +812,6 @@ Page({
           isLogin: true,
         });
       }
-      that.getDeviceData();
     } else {
       var greeting = that.getGreetBuNickName("");
       that.setData({
@@ -863,4 +847,23 @@ Page({
       wx.setStorageSync("homeBanner", res);
     })
   },
-})
+})
+
+// 去掉此功能,先留着吧
+// const other= {
+//   "url": "",
+//   "media_data": "",
+//   "user_id": `${app.globalData.userInfo.deviceUid}`,
+//   "timestamp": `${Math.round(new Date() / 1000)}`,
+//   "channel_id": `${res[1].channelNum}`,
+//   "order": "",
+//   "resource_from": "",
+//   "songAlbumID":"",
+//   "version":3,
+//   "is_debug": app.globalData.is_debug
+// };
+// app.PubMsg({
+//   type: "play",
+//   DstDeviceName: that.getThisDeviceID(),
+//   other
+// });