Sfoglia il codice sorgente

feature: 处理数据列表

Damon 7 mesi fa
parent
commit
d2f42c87e8
2 ha cambiato i file con 25 aggiunte e 30 eliminazioni
  1. 20 29
      pages/index/index.js
  2. 5 1
      utils/store.js

+ 20 - 29
pages/index/index.js

@@ -132,10 +132,7 @@ Page({
     // BtHelper.getInstance().initBluetoothAdapter();
     var l = store.getStore("deviceList");
     if (!strings.isEmpty(l)) {
-      try {
-        that.updateDeviceList(l, true);
-      } catch (e) {}
-      // that.tryConnectBle()
+      that.updateDeviceList(l, true);
     }
 
     ///监听蓝牙设备
@@ -152,15 +149,15 @@ Page({
 
   async compareList() {
     var that = this;
-    var tempList = that.data.deviceList;
-    if (tempList.length > 0) {
+    var deviceList = that.data.deviceList;
+    if (deviceList.length > 0) {
       if (BtHelper.getInstance().getCallBackConnect() == null) {
         var compareList = BtHelper.getInstance().getCompareList();
         if (compareList.length > 0) {
           ///对比在线的蓝牙设备
           var isChanged = false;
-          for (var i = 0; i < tempList.length; i++) {
-            var tempItem = tempList[i];
+          for (var i = 0; i < deviceList.length; i++) {
+            var tempItem = deviceList[i];
             if (tempItem.connectType != 3) {
               var has = false;
               for (var j = 0; j < compareList.length; j++) {
@@ -185,7 +182,7 @@ Page({
             }
           }
           if (isChanged) {
-            that.updateDeviceList(tempList, false);
+            that.updateDeviceList(deviceList, false);
           }
 
           ///没有连接则连接第一个在线的蓝牙
@@ -445,7 +442,6 @@ Page({
     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);
         }
@@ -470,12 +466,12 @@ Page({
       return
     };
 
-    var tempDevice = that.data.deviceList[deviceListIndex];
-    var tempDeviceId = tempDevice.deviceId;
+    var device = that.data.deviceList[deviceListIndex];
+    var tempDeviceId = device.deviceId;
     ///删除当前选中这个
     deviceList.splice(deviceListIndex, 1);
     ///添加到第一个去
-    deviceList.unshift(tempDevice);
+    deviceList.unshift(device);
     that.setData({
       deviceListIndex: 0,
       autoConnected: true,
@@ -619,9 +615,9 @@ Page({
     ///有设备在线被选中,则让它不被选择
     var deviceListIndex = that.data.deviceListIndex;
     if (deviceListIndex != null) {
-      var list = that.data.deviceList;
-      if (list.length > deviceListIndex) {
-        if (list[deviceListIndex].connectType == 3) {
+      var deviceList = that.data.deviceList;
+      if (deviceList.length > deviceListIndex) {
+        if (deviceList[deviceListIndex].connectType == 3) {
           if (that.data.thisDeviceMac !== null) {
             app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
             that.setData({
@@ -926,7 +922,6 @@ Page({
 
     ///在线排序前面,wifi设备排序前面
     that.updateDeviceList(deviceList, false);
-
     var indexPage = that.data.indexPage;
     if (indexPage != 0) {
       that.setData({
@@ -981,8 +976,6 @@ Page({
 
   ///更新列表排序
   updateDeviceList(deviceList, isInit) {
-    console.log("更新设备列表0:", deviceList);
-
     if (deviceList.length <= 0) {
       return;
     }
@@ -996,17 +989,15 @@ Page({
     var onNoLineList = [];
 
     ///区分在线和离线
-    for (var i = 0; i < deviceList.length; i++) {
+    for (var i = 1; i < deviceList.length; i++) {
+      var device = deviceList[i];
       if (isInit) {
-        deviceList[i].state = "offline";
+        device.state = "offline";
+      }
+      if (device.state == "online") {
+        onLineList.push(device)
       } else {
-        if (i > 0) {
-          if (deviceList[i].state == "online") {
-            onLineList.push(deviceList[i])
-          } else {
-            onNoLineList.push(deviceList[i])
-          }
-        }
+        onNoLineList.push(device)
       }
     }
 
@@ -1041,7 +1032,7 @@ Page({
     finalList = finalList.concat(onNoLineWifiList);
     finalList = finalList.concat(onNoLineBlueList);
 
-    store.setStore("deviceList", deviceList);
+    store.setStore("deviceList", finalList);
     that.setData({
       deviceList: finalList
     });

+ 5 - 1
utils/store.js

@@ -8,8 +8,11 @@ module.exports = {
 //同步储存缓存数据
 function setStore(key, value) {
   try {
+    // console.log("gasdfasfqwerqwerqr===0000==" + JSON.stringify(value));
     wx.setStorageSync(key, value);
-  } catch (e) {}
+  } catch (e) {
+    // console.log("gasdfasfqwerqwerqr===111==" + JSON.stringify(e));
+  }
 };
 
 //同步取出缓存数据
@@ -17,6 +20,7 @@ function getStore(key) {
   try {
     // 使用同步方法获取数据
     const list = wx.getStorageSync(key);
+    // console.log("gasdfasfqwerqwerqr===222==" + JSON.stringify(list));
     if (list) {
       return list;
     } else {