소스 검색

feature: 处理判断空值的问题

Damon 7 달 전
부모
커밋
2e22b172bf
3개의 변경된 파일10개의 추가작업 그리고 7개의 파일을 삭제
  1. 5 3
      devices/ble_manager.js
  2. 4 3
      pages/index/index.js
  3. 1 1
      utils/strings.js

+ 5 - 3
devices/ble_manager.js

@@ -325,18 +325,20 @@ class bleManager {
   // 断开与指定设备的连接
   disconnect() {
     var that = this;
+    const strings = require('../utils/strings');
     let device = that.publicDevice;
-    if (device == null) {
+    if (strings.isEmpty(device)) {
       return;
     }
+
     let deviceId = device.deviceId
-    if (deviceId == null || deviceId == "") {
+    if (strings.isEmpty(deviceId)) {
       return;
     }
 
     return new Promise((resolve, reject) => {
       wx.closeBLEConnection({
-        deviceId: that.publicDevice.deviceId ?? "",
+        deviceId: deviceId,
         success: (res) => {
           that.publicDevice = null;
           console.log('断开连接成功:', res);

+ 4 - 3
pages/index/index.js

@@ -129,7 +129,7 @@ Page({
   onDeviceLoad() {
     var that = this;
     // BtHelper.getInstance().initBluetoothAdapter();
-    var l = wx.getStorageSync("deviceList") || "";
+    var l = wx.getStorageSync("deviceList");
     if (!strings.isEmpty(l)) {
       try {
         var list = JSON.parse(l);
@@ -445,8 +445,7 @@ Page({
     if (!strings.isEmpty(deviceList)) {
       for (var i = 0; i < deviceList.length; i++) {
         if (deviceList[i].connectType == 3) {
-          console.log("gasdfqwwerqewrqwerqr==22222===" + i);
-          console.log("gasdfqwwerqewrqwerqr==3333===" + JSON.stringify(deviceList[i]));
+
           let topic = `/AIrSMArT_${deviceList[i].deviceId.split("BLUFI_")[1]}/status/onoffline`;
           app.subscribe(topic);
         }
@@ -816,6 +815,7 @@ Page({
     if (onlyCancel) {
       return;
     }
+
     const id = that.data.deviceList[index].deviceId;
     const deviceList = that.data.deviceList.filter((item, i) => {
       return id !== item.deviceId
@@ -846,6 +846,7 @@ Page({
     const deviceList = that.data.deviceList.filter((item, i) => {
       return id !== item.deviceId
     });
+
     wx.setStorageSync("deviceList", JSON.stringify(deviceList));
     that.setData({
       deviceList,

+ 1 - 1
utils/strings.js

@@ -5,7 +5,7 @@ module.exports = {
 }
 
 function isEmpty(e) {
-  return e == null || e == undefined || e.length == 0 || e == [];
+  return e == null || e == undefined || e.length == 0 || e == [] || e == {};
 };
 
 function length(e) {