Procházet zdrojové kódy

feature: 修复多次加载数据,出现新连接数据排序问题

Damon před 7 měsíci
rodič
revize
f86a3fc9d5
3 změnil soubory, kde provedl 47 přidání a 15 odebrání
  1. 3 1
      pages/index/index.js
  2. 8 7
      utils/lexin/add.js
  3. 36 7
      utils/lexin/subscribe.js

+ 3 - 1
pages/index/index.js

@@ -526,11 +526,12 @@ Page({
     var deviceId = device.deviceId;
     var addDeviceList = lexin_add.addWifiDevice(device);
     that.updateDeviceList(addDeviceList, false, false);
+
     that.setData({
       deviceListSelect: null,
       connectDeviceIding: deviceId,
     });
-    lexin_subscribe.subscribeSingleDevice(deviceId);
+    lexin_subscribe.subscribeSingleDevice(device);
   },
 
   refreshCurrentDevice(device) {
@@ -543,6 +544,7 @@ Page({
         break;
       }
     }
+
     if (selectIndex != null) {
       var addDeviceList = lexin_add.addWifiDevice(device);
       that.updateDeviceList(addDeviceList, false, false);

+ 8 - 7
utils/lexin/add.js

@@ -11,11 +11,12 @@ module.exports = {
 ///新添加蓝牙设备
 function addBlueDevice(device) {
   // 同一个设备处理
-  var deviceList = getCurrentPages()[0].getDeviceList();
+  var pages = getCurrentPages();
+  var deviceList = pages[0].getDeviceList();
   var tempList = deviceList.filter((v) => v.deviceId === device.deviceId);
   const strings = require('../strings');
   if (!strings.isEmpty(tempList)) {
-    deviceList = deviceList.filter((v) => v.deviceId !== device.deviceId);
+    deviceList = tempList.filter((v) => v.deviceId !== device.deviceId);
   }
   /// 添加到第一个
   deviceList.unshift({
@@ -27,7 +28,7 @@ function addBlueDevice(device) {
     mac: device.mac,
     ProdModel: device.ProdModel || device.clientType,
     clientType: device.clientType || device.ProdModel,
-    image: device.image,
+    imageUrl: device.imageUrl,
   });
   return deviceList;
 };
@@ -36,11 +37,11 @@ function addBlueDevice(device) {
 // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
 function addWifiDevice(device) {
   // 同一个设备处理
-  var deviceList = getCurrentPages()[0].getDeviceList();
-  var tempList = deviceList.filter((v) => v.deviceId === device.deviceId);
+  var pages = getCurrentPages();
+  var deviceList = pages[0].getDeviceList();
   const strings = require('../strings');
-  if (!strings.isEmpty(tempList)) {
-    deviceList = deviceList.filter((v) => v.deviceId !== device.deviceId);
+  if (!strings.isEmpty(deviceList)) {
+    deviceList = deviceList.filter((v) => v.deviceId != device.deviceId);
   }
   /// 添加到第一个
   deviceList.unshift(device);

+ 36 - 7
utils/lexin/subscribe.js

@@ -4,15 +4,44 @@ module.exports = {
 }
 
 // 订阅在线单个设备
-function subscribeSingleDevice(deviceId) {
+function subscribeSingleDevice(device) {
   //  /AIrSMArT_210052355545/status/onoffline 魅族款 APP
   // /AIrSMArT_861210052355545/status/onoffline 魅族款 小程序
-  const lexin_util = require('../lexin/util');
-  var mac = lexin_util.getMacByDeviceId(deviceId);
-  var topic = `/AIrSMArT_${mac}/status/onoffline`;
-  const app = getApp();
-  app.unsubscribe(topic);
-  app.subscribe(topic);
+  var deviceId = device.deviceId;
+  const strings = require('../strings');
+  var deviceList = pages[0].getDeviceList();
+
+  var isOnline = false;
+  if (!strings.isEmpty(deviceList)) {
+    var deviceListSelect = pages[0].getDeviceListSelect();
+    ///当前已连接
+    if (deviceListSelect != null && deviceList.length > deviceListSelect) {
+      var d = deviceList[deviceListSelect].deviceId;
+      if (d == deviceId) {
+        return;
+      }
+    }
+
+    for (var i = 0; i < deviceList.length; i++) {
+      var tempDevice = deviceList[i];
+      if (deviceId == tempDevice.deviceId && tempDevice.state == "online") {
+        isOnline = true;
+        break;
+      }
+    }
+  }
+
+  ///在线处理
+  if (isOnline) {
+    var pages = getCurrentPages();
+    pages[0].actionDevice(tempDevice);
+  } else {
+    const lexin_util = require('../lexin/util');
+    var mac = lexin_util.getMacByDeviceId(deviceId);
+    var topic = `/AIrSMArT_${mac}/status/onoffline`;
+    const app = getApp();
+    app.subscribe(topic);
+  }
 };
 
 // 订阅在线设备