Selaa lähdekoodia

feature: 构建乐鑫服务相关帮助类

Damon 7 kuukautta sitten
vanhempi
commit
f544753ec4
3 muutettua tiedostoa jossa 134 lisäystä ja 111 poistoa
  1. 1 1
      devices/ble_manager.js
  2. 21 110
      pages/index/index.js
  3. 112 0
      utils/lexin/service.js

+ 1 - 1
devices/ble_manager.js

@@ -45,7 +45,7 @@ class bleManager {
     wx.onBluetoothAdapterStateChange(function (res) {
       var isAvailable = res.available;
       if (!isAvailable) {
-        getCurrentPages()[0].updateDeviceList([], false, true);
+        getCurrentPages()[0].closeBlueResetOffline(false, true);
       }
     })
   }

+ 21 - 110
pages/index/index.js

@@ -9,8 +9,8 @@ import strings from '../../utils/strings.js';
 import store from '../../utils/store.js';
 import route_constant from '../../utils/route_constant.js';
 import route_util from '../../utils/route_util.js';
-import lexinUtil from '../../utils/lexin/util.js';
-import lexinJump from '../../utils/lexin/jump.js';
+import lexin_util from '../../utils/lexin/util.js';
+import lexin_jump from '../../utils/lexin/jump.js';
 import {
   BtHelper
 } from '../../devices/bt_helper.js';
@@ -284,7 +284,7 @@ Page({
 
             var deviceId = deviceList[deviceListSelect].deviceId;
             // 接收设备当前播放状态
-            var deviceMacId = lexinUtil.getDeviceMacId(deviceId);
+            var deviceMacId = lexin_util.getDeviceMacId(deviceId);
             const obj = {
               DstDeviceName: deviceMacId
             }
@@ -395,12 +395,12 @@ Page({
     // 取消订阅
     var deviceMacId = that.data.deviceMacId;
     if (!strings.isEmpty(deviceMacId)) {
-      var pubResponse = lexinUtil.getResponseByDeviceMacId(deviceMacId);
+      var pubResponse = lexin_util.getResponseByDeviceMacId(deviceMacId);
       app.unsubscribe(pubResponse);
     };
 
     var deviceId = device.deviceId;
-    deviceMacId = lexinUtil.getDeviceMacId(deviceId);
+    deviceMacId = lexin_util.getDeviceMacId(deviceId);
     that.setData({
       deviceListSelect: index,
       deviceMacId: deviceMacId,
@@ -441,7 +441,7 @@ Page({
       deviceList: deviceList,
     });
 
-    var deviceMacId = lexinUtil.getDeviceMacId(deviceId);
+    var deviceMacId = lexin_util.getDeviceMacId(deviceId);
     const obj = {
       DstDeviceName: deviceMacId
     };
@@ -511,7 +511,7 @@ Page({
       "is_debug": app.globalData.is_debug
     };
 
-    var deviceMacId = lexinUtil.getDeviceMacId(deviceId);
+    var deviceMacId = lexin_util.getDeviceMacId(deviceId);
     app.PubMsg({
       type: "play",
       DstDeviceName: deviceMacId,
@@ -608,7 +608,7 @@ Page({
     if (deviceList[deviceListSelect].connectType == 3) {
       var deviceMacId = that.data.deviceMacId;
       if (!strings.isEmpty(deviceMacId)) {
-        var pubResponse = lexinUtil.getResponseByDeviceMacId(deviceMacId);
+        var pubResponse = lexin_util.getResponseByDeviceMacId(deviceMacId);
         app.unsubscribe(pubResponse);
         that.setData({
           actionIndex: null,
@@ -781,7 +781,7 @@ Page({
 
     var deviceId = deviceList[index].deviceId;
     // 取消订阅
-    var pubResponse = lexinUtil.getResponseByDeviceId(deviceId);
+    var pubResponse = lexin_util.getResponseByDeviceId(deviceId);
     app.unsubscribe(pubResponse);
     if (onlyCancel) {
       return;
@@ -886,115 +886,26 @@ Page({
     that.subscribeDevicesStatus();
   },
 
-  ///更新列表排序
-  updateDeviceList(deviceList, isInit, closeAllBlue) {
+  /// 手机关闭蓝牙,所有蓝牙设备离线
+  closeBlueResetOffline(isInit, closeAllBlue) {
     var that = this;
-    if (strings.isEmpty(deviceList)) {
-      deviceList = that.data.deviceList;
-    }
-
-    if (deviceList.length <= 0) {
-      return;
-    }
-
-    var finalList = [];
-    if (isInit) {
-      deviceList[0].state = "offline";
-    }
+    var deviceList = that.data.deviceList;
+    updateDeviceList(deviceList, isInit, closeAllBlue);
+  },
 
-    /// 让所有蓝牙设备离线
-    if (closeAllBlue) {
-      deviceList.forEach(element => {
-        if (element.connectType != 3) {
-          element.state = "offline";
-        }
+  /// 更新列表排序
+  updateDeviceList(deviceList, isInit, closeAllBlue) {
+    var finalList = store.updateDeviceList(deviceList, isInit, closeAllBlue);
+    if (!strings.isEmpty(deviceList) || !strings.isEmpty(finalList)) {
+      that.setData({
+        deviceList: finalList,
       });
     }
-
-    var isFirstOnline = false;
-    if (deviceList[0].state == "online") {
-      isFirstOnline = true;
-      finalList.push(deviceList[0]);
-    }
-
-    ///区分在线和离线
-    for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
-      if (isInit) {
-        deviceList[i].state = "offline";
-      }
-    }
-
-    var onLineList = [];
-    var onNoLineList = [];
-    ///添加在线的
-    for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
-      var device = deviceList[i];
-      if (device.state == "online") {
-        onLineList.push(device)
-      }
-    }
-
-    ///添加离线的
-    for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
-      var device = deviceList[i];
-      if (device.state != "online") {
-        onNoLineList.push(device)
-      }
-    }
-
-    // 区分在线wifi和蓝牙 wifi在前 离线在后
-    var onLineWifiList = [];
-    var onLineBlueList = [];
-    ///添加在线wifi
-    onLineList.forEach(element => {
-      if (element.connectType == 3) {
-        onLineWifiList.push(element);
-      }
-    });
-    ///添加在线蓝牙
-    onLineList.forEach(element => {
-      if (element.connectType != 3) {
-        onLineBlueList.push(element)
-      }
-    });
-
-    finalList = finalList.concat(onLineWifiList);
-    finalList = finalList.concat(onLineBlueList);
-
-    ///只需要蓝牙和wifi在线的
-    let mDeviceList = []
-    mDeviceList = mDeviceList.concat(onLineWifiList);
-    mDeviceList = mDeviceList.concat(onLineBlueList);
-    getApp().globalData.mDeviceList = mDeviceList;
-
-    ///区分离线wifi和蓝牙 wifi在前 离线在后
-    var onNoLineWifiList = [];
-    var onNoLineBlueList = [];
-    ///添加离线wifi
-    onNoLineList.forEach(element => {
-      if (element.connectType == 3) {
-        onNoLineWifiList.push(element)
-      }
-    });
-    ///添加离线蓝牙
-    onNoLineList.forEach(element => {
-      if (element.connectType != 3) {
-        onNoLineBlueList.push(element)
-      }
-    });
-
-    finalList = finalList.concat(onNoLineWifiList);
-    finalList = finalList.concat(onNoLineBlueList);
-
-    store.setStore("deviceList", finalList);
-    that.setData({
-      deviceList: finalList
-    });
   },
 
   // 关于我们
   jumpToAboutUs() {
-    lexinJump.toAboutUs();
+    lexin_jump.toAboutUs();
   },
 
   ///******************************* 可折叠 ********************************************///

+ 112 - 0
utils/lexin/service.js

@@ -0,0 +1,112 @@
+module.exports = {
+  updateDeviceList: updateDeviceList,
+}
+
+///更新列表排序
+function updateDeviceList(deviceList, isInit, closeAllBlue) {
+  const strings = require('../strings');
+  if (strings.isEmpty(deviceList)) {
+    return [];
+  }
+
+  const app = getApp();
+  const store = require('../store');
+
+  var finalList = [];
+  if (isInit) {
+    deviceList[0].state = "offline";
+  }
+
+  /// 让所有蓝牙设备离线
+  if (closeAllBlue) {
+    deviceList.forEach(element => {
+      if (element.connectType != 3) {
+        element.state = "offline";
+      }
+    });
+  }
+
+  ///挑选出第一个在线设备
+  var isFirstOnline = false;
+  if (deviceList[0].state == "online") {
+    isFirstOnline = true;
+    finalList.push(deviceList[0]);
+  }
+
+  ///区分在线和离线
+  for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
+    if (isInit) {
+      deviceList[i].state = "offline";
+    }
+  }
+
+  var onLineList = [];
+  var onNoLineList = [];
+
+  ///添加在线的
+  for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
+    var device = deviceList[i];
+    if (device.state == "online") {
+      onLineList.push(device)
+    }
+  }
+
+  ///添加离线的
+  for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
+    var device = deviceList[i];
+    if (device.state != "online") {
+      onNoLineList.push(device)
+    }
+  }
+
+  // 区分在线wifi和蓝牙 wifi在前 离线在后
+  var onLineWifiList = [];
+  var onLineBlueList = [];
+
+  ///添加在线wifi
+  onLineList.forEach(element => {
+    if (element.connectType == 3) {
+      onLineWifiList.push(element);
+    }
+  });
+
+  ///添加在线蓝牙
+  onLineList.forEach(element => {
+    if (element.connectType != 3) {
+      onLineBlueList.push(element)
+    }
+  });
+
+  finalList = finalList.concat(onLineWifiList);
+  finalList = finalList.concat(onLineBlueList);
+
+  ///只需要蓝牙和wifi在线的
+  var mDeviceList = []
+  mDeviceList = mDeviceList.concat(onLineWifiList);
+  mDeviceList = mDeviceList.concat(onLineBlueList);
+  app.globalData.mDeviceList = mDeviceList;
+
+  ///区分离线wifi和蓝牙 wifi在前 离线在后
+  var onNoLineWifiList = [];
+  var onNoLineBlueList = [];
+
+  ///添加离线wifi
+  onNoLineList.forEach(element => {
+    if (element.connectType == 3) {
+      onNoLineWifiList.push(element)
+    }
+  });
+
+  ///添加离线蓝牙
+  onNoLineList.forEach(element => {
+    if (element.connectType != 3) {
+      onNoLineBlueList.push(element)
+    }
+  });
+
+  finalList = finalList.concat(onNoLineWifiList);
+  finalList = finalList.concat(onNoLineBlueList);
+
+  store.setStore("deviceList", finalList);
+  return finalList;
+};