فهرست منبع

feature: 处理that和this的数据交互调用问题

Damon 8 ماه پیش
والد
کامیت
ae6a2e3839
1فایلهای تغییر یافته به همراه116 افزوده شده و 101 حذف شده
  1. 116 101
      pages/index/index.js

+ 116 - 101
pages/index/index.js

@@ -8,7 +8,6 @@ const {
 const {
   isCN
 } = require('../../utils/util.js');
-let _this = null;
 const interval = null;
 // import routeUtil from '../../utils/route_util.js'
 import routeUtil from '../../utils/route_util'
@@ -103,11 +102,11 @@ Page({
   },
 
   onHomeLoad() {
-    _this = this;
+    var that = this;
     // 版本自动更新代码
     const updateManager = wx.getUpdateManager();
     updateManager.onUpdateReady(function () {
-      _this.setData({
+      that.setData({
         newVersion: true
       });
     })
@@ -126,8 +125,8 @@ Page({
 
     const str = setTimeout(() => {
       clearTimeout(str);
-      if (_this.data.isOneLoading) {
-        _this.setData({
+      if (that.data.isOneLoading) {
+        that.setData({
           isOneLoading: false,
         });
       }
@@ -137,13 +136,13 @@ Page({
     wx.getStorage({
       key: "channelData",
       success(res) {
-        _this.setData({
+        that.setData({
           channelData: res.data
         })
       }
     });
     // 登录
-    this.login();
+    that.login();
   },
 
   onMeLoad() {
@@ -176,25 +175,26 @@ Page({
   },
 
   onHomeShow() {
-    if ((!this.data.isOneLoading)) {
+    var that = this;
+    if ((!that.data.isOneLoading)) {
       wx.getStorage({
         key: 'userInfo',
         fail(res) {
           // 取消订阅
-          if (_this.data.thisDeviceMac !== null) {
-            app.unsubscribe(`/${_this.data.thisDeviceMac}/user/pub_response`);
+          if (that.data.thisDeviceMac !== null) {
+            app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
           };
-          _this.setData({
+          that.setData({
             showLogin: true,
           });
         },
         success() {
           if (app.globalData.newDeviceId) {
             // 获取设备本地数据
-            _this.getDeviceData();
-          } else if (_this.data.deviceListIndex !== null) {
+            that.getDeviceData();
+          } else if (that.data.deviceListIndex !== null) {
             // 更新
-            _this.actionDevice(_this.data.deviceListIndex);
+            that.actionDevice(that.data.deviceListIndex);
           }
         }
       });
@@ -203,6 +203,7 @@ Page({
 
   ////开始是home的
   login() {
+    var that = this;
     wx.login({
       success: res => {
         let phone = undefined;
@@ -227,15 +228,15 @@ Page({
           });
           // 需要用授权登录
           if ((res.isNewUser && res.isNewUser === true) || !phone) {
-            _this.setData({
+            that.setData({
               showLogin: true,
             });
             return;
           };
           // 获取设备本地数据
-          this.getDeviceData();
+          that.getDeviceData();
 
-          _this.setData({
+          that.setData({
             showLogin: false,
           });
           wx.setStorage({
@@ -252,6 +253,7 @@ Page({
 
   ///开始时home的
   getDeviceData() {
+    var that = this;
     // 获取设备数据
     wx.getStorage({
       key: 'devicelist',
@@ -260,14 +262,14 @@ Page({
         if (res.data) {
           const resData = JSON.parse(res.data);
           console.log("本地设备列表", resData)
-          _this.setData({
+          that.setData({
             deviceList: resData
           });
           // 连接mqtt
           if (app.globalData.client === null) {
             app.connect();
           } else if (app.globalData.newDeviceId) {
-            _this.setData({
+            that.setData({
               actionIndex: null,
               deviceListIndex: null,
             });
@@ -277,13 +279,13 @@ Page({
             app.subscribe(topic);
             const Timeout = setTimeout(() => {
               clearTimeout(Timeout);
-              _this.actionDevice(0);
+              that.actionDevice(0);
             }, 1000);
           }
 
           // // 有新设备
           // if(app.globalData.newDeviceId) {
-          //   _this.setData({
+          //   that.setData({
           //     actionIndex: null,
           //     deviceListIndex: null,
           //   });
@@ -301,38 +303,39 @@ Page({
   // 回调
   mqttCallback(type, option) {
     // console.log("gadsfadsfqwerq===" + type + "===" + option);
+    var that = this;
     let payloads = null;
     if (option) {
       payloads = JSON.parse(option.payload);
     };
     switch (type) {
       case "connect":
-        _this.connectSuccess();
+        that.connectSuccess();
         break;
       case "message_onoffline":
-        _this.online(payloads);
+        that.online(payloads);
         break;
       case "message":
         // 接收设备播放信息
         if (payloads.type === "get_position" && payloads.other) {
           let actionIndex = null;
 
-          _this.data.channelData.map((v, index) => {
+          that.data.channelData.map((v, index) => {
             if (v.channelNum === payloads.other.channel) {
               actionIndex = index;
             }
           });
-          _this.setData({
+          that.setData({
             actionIndex,
           });
         } else if (payloads.type === "play" || payloads.type === "play_state") {
 
-          if (_this.data.deviceListIndex === null) {
+          if (that.data.deviceListIndex === null) {
             return;
           }
           // 接收设备当前播放状态
           const obj = {
-            DstDeviceName: _this.getThisDeviceID()
+            DstDeviceName: that.getThisDeviceID()
           }
           app.PubMsg({
             type: "get_position",
@@ -340,42 +343,42 @@ Page({
           });
         } else if (payloads.type === "get_dev_info") {
           // 接收设备当前信息
-          this.getchannelData(payloads.other.ProdModel);
+          that.getchannelData(payloads.other.ProdModel);
           // 电量
-          _this.setData({
-            battery: _this._battery(payloads.other.Power),
+          that.setData({
+            battery: that._battery(payloads.other.Power),
           })
           // 当前设备木有设置定时
-          _this.setData({
+          that.setData({
             isSetWake: false,
           });
           payloads.other.alarm.map((v) => {
             if (v.enable === "1") {
-              _this.setData({
+              that.setData({
                 isSetWake: true
               });
             }
           });
           // 更新信息
-          _this.data.deviceList.map((v, index) => {
+          that.data.deviceList.map((v, index) => {
             if (payloads.SrcDeviceName && payloads.SrcDeviceName.indexOf(v.name.split("BLUFI_")[1]) !== -1) {
-              _this.data.deviceList[index].ProdModel = payloads.other.ProdModel;
-              _this.data.deviceList[index].devName = payloads.other.devName;
+              that.data.deviceList[index].ProdModel = payloads.other.ProdModel;
+              that.data.deviceList[index].devName = payloads.other.devName;
             }
           });
           // 更新缓存
           wx.setStorage({
             key: "devicelist",
-            data: JSON.stringify(_this.data.deviceList),
+            data: JSON.stringify(that.data.deviceList),
             success() {
-              _this.setData({
-                deviceList: _this.data.deviceList
+              that.setData({
+                deviceList: that.data.deviceList
               })
             }
           });
         } else if (payloads.type === "battery" && payloads.other) {
-          _this.setData({
-            battery: _this._battery(payloads.other.battery),
+          that.setData({
+            battery: that._battery(payloads.other.battery),
           })
         }
         break;
@@ -404,46 +407,48 @@ Page({
 
   connectSuccess() {
     // 订阅设备在线信息
-    _this.subscribeDevicesStatus();
+    var that = this;
+    that.subscribeDevicesStatus();
   },
 
   // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
   online(payloads) {
     // 设置在线状态
-    _this.data.deviceList.map((v, index) => {
+    var that = this;
+    that.data.deviceList.map((v, index) => {
       if (payloads.uuid && payloads.uuid.indexOf(v.name.split("BLUFI_")[1]) !== -1) {
-        _this.data.deviceList[index].state = payloads.state;
+        that.data.deviceList[index].state = payloads.state;
       }
     });
 
-    _this.setData({
-      deviceList: _this.data.deviceList,
+    that.setData({
+      deviceList: that.data.deviceList,
     });
     // 更新缓存
     wx.setStorage({
       key: "devicelist",
-      data: JSON.stringify(_this.data.deviceList)
+      data: JSON.stringify(that.data.deviceList)
     });
     // 如没有选中,选中最新的
     (() => {
-      if (_this.data.deviceListIndex === null) {
+      if (that.data.deviceListIndex === null) {
         let itue = false;
-        _this.data.deviceList.map((v, index) => {
+        that.data.deviceList.map((v, index) => {
           if (v.state === "online" && !itue) {
             itue = true;
-            _this.actionDevice(index);
+            that.actionDevice(index);
           }
         });
       }
       //  else {
-      //   _this.actionDevice(_this.data.deviceListIndex);
+      //   that.actionDevice(that.data.deviceListIndex);
       // };
 
     })();
 
     // 当前播放设备离线
-    if (_this.data.deviceListIndex !== null && _this.data.deviceList[_this.data.deviceListIndex].state !== "online") {
-      _this.setData({
+    if (that.data.deviceListIndex !== null && that.data.deviceList[that.data.deviceListIndex].state !== "online") {
+      that.setData({
         actionIndex: null,
         deviceListIndex: null,
       });
@@ -452,29 +457,31 @@ Page({
 
   // 订阅设备在线状态
   subscribeDevicesStatus() {
-    this.data.deviceList.forEach((value) => {
+    var that = this;
+    that.data.deviceList.forEach((value) => {
       let topic = `/AIrSMArT_${value.name.split("BLUFI_")[1]}/status/onoffline`;
       app.subscribe(topic);
     })
   },
 
   subscribeCurrDevice() {
+    var that = this;
     if (!(app.globalData.client && app.globalData.client.connected)) {
       console.log("未连接MQTT服务器");
       const str = setInterval(() => {
         clearInterval(str);
-        _this.subscribeCurrDevice();
+        that.subscribeCurrDevice();
       }, 500);
       return;
     };
-    if (this.data.deviceList.length === 0 || this.data.deviceListIndex === null) {
+    if (that.data.deviceList.length === 0 || that.data.deviceListIndex === null) {
       return
     };
 
-    let topic = `/AIrSMArT_${this.data.deviceList[this.data.deviceListIndex].name.split("BLUFI_")[1]}/user/pub_response`;
+    let topic = `/AIrSMArT_${that.data.deviceList[that.data.deviceListIndex].name.split("BLUFI_")[1]}/user/pub_response`;
     app.subscribe(topic);
     const obj = {
-      DstDeviceName: _this.getThisDeviceID()
+      DstDeviceName: that.getThisDeviceID()
     };
     app.PubMsg({
       type: "get_dev_info",
@@ -483,17 +490,18 @@ Page({
   },
 
   actionMusic(e) {
-    if (e.currentTarget.dataset.index === this.data.actionIndex) {
+    var that = this;
+    if (e.currentTarget.dataset.index === that.data.actionIndex) {
       return;
     };
-    if (this.data.deviceListIndex === null) {
+    if (that.data.deviceListIndex === null) {
       wx.showToast({
         title: '请选择设备',
         icon: "none"
       })
       return;
     };
-    this.setData({
+    that.setData({
       actionIndex: e.currentTarget.dataset.index
     });
     const other = {
@@ -501,7 +509,7 @@ Page({
       "media_data": "",
       "user_id": `${app.globalData.userInfo.deviceUid}`,
       "timestamp": `${Math.round(new Date() / 1000)}`,
-      "channel_id": `${this.data.channelData[e.currentTarget.dataset.index].channelNum}`,
+      "channel_id": `${that.data.channelData[e.currentTarget.dataset.index].channelNum}`,
       "order": "",
       "resource_from": "",
       "songAlbumID": "",
@@ -510,30 +518,31 @@ Page({
     };
     app.PubMsg({
       type: "play",
-      DstDeviceName: _this.getThisDeviceID(),
+      DstDeviceName: that.getThisDeviceID(),
       other
     });
   },
 
   getThisDeviceID() {
-    return `AIrSMArT_${this.data.deviceList[this.data.deviceListIndex].name.split("BLUFI_")[1]}`
+    var that = this;
+    return `AIrSMArT_${that.data.deviceList[that.data.deviceListIndex].name.split("BLUFI_")[1]}`
   },
 
   getchannelData(clientType) {
-    let _this = this;
-    if (this.data.deviceListIndex === null) {
+    var that = this;
+    if (that.data.deviceListIndex === null) {
       return;
     };
 
     listByDevice({
       clientType
     }).then((res) => {
-      _this.setData({
+      that.setData({
         channelData: res
       });
       // 接收设备当前播放状态
       const obj = {
-        DstDeviceName: _this.getThisDeviceID()
+        DstDeviceName: that.getThisDeviceID()
       }
       app.PubMsg({
         type: "get_position",
@@ -563,7 +572,7 @@ Page({
         // };
         // app.PubMsg({
         //   type: "play",
-        //   DstDeviceName: _this.getThisDeviceID(),
+        //   DstDeviceName: that.getThisDeviceID(),
         //   other
         // });
       }
@@ -572,9 +581,10 @@ Page({
   },
 
   goChnnel() {
+    var that = this;
     wx.setStorage({
       key: "channelDeta",
-      data: this.data.channelData[this.data.actionIndex],
+      data: that.data.channelData[that.data.actionIndex],
       success() {
         wx.navigateTo({
           url: './../channelDetails/channelDetails'
@@ -589,7 +599,8 @@ Page({
   },
 
   updataClone() {
-    this.setData({
+    var that = this;
+    that.setData({
       newVersion: false
     })
   },
@@ -621,7 +632,7 @@ Page({
   },
 
   goMeLogin() {
-    const that = this;
+    var that = this;
     if (that.data.islogin) {
       // 退出登录
       wx.removeStorage({
@@ -681,7 +692,7 @@ Page({
     var item = e.currentTarget.dataset.item;
     var ProdModel = item.ProdModel;
     if (ProdModel == "猫王小王子OTR-X") {
-      if (e.currentTarget.dataset.index === this.data.deviceListIndex) {
+      if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
         that.goWake();
       } else {
         that.actionDevice(e.currentTarget.dataset.index)
@@ -692,33 +703,35 @@ Page({
   },
 
   actionDevice(index) {
-    const device = this.data.deviceList[index];
+    var that = this;
+    const device = that.data.deviceList[index];
     if (device.state !== "online") {
       return;
     };
     // 取消订阅
-    if (this.data.thisDeviceMac !== null) {
-      app.unsubscribe(`/${this.data.thisDeviceMac}/user/pub_response`);
+    if (that.data.thisDeviceMac !== null) {
+      app.unsubscribe(`/${that.data.thisDeviceMac}/user/pub_response`);
     };
 
-    this.setData({
+    that.setData({
       deviceListIndex: index,
-      thisDeviceMac: `AIrSMArT_${this.data.deviceList[index].name.split("BLUFI_")[1]}`
+      thisDeviceMac: `AIrSMArT_${that.data.deviceList[index].name.split("BLUFI_")[1]}`
     });
 
     // app.PubMsg({
     //   type: "get_dev_info",
-    //   DstDeviceName: _this.getThisDeviceID()
+    //   DstDeviceName: that.getThisDeviceID()
     // });
-    this.subscribeCurrDevice();
+    that.subscribeCurrDevice();
   },
 
   goWake() {
-    if (this.data.deviceListIndex === null) {
+    var that = this;
+    if (that.data.deviceListIndex === null) {
       return;
     };
     wx.navigateTo({
-      url: './../deviceWake/deviceWake?deviceId=' + this.data.deviceList[this.data.deviceListIndex].deviceId + "&clientType=" + this.data.deviceList[this.data.deviceListIndex].ProdModel,
+      url: './../deviceWake/deviceWake?deviceId=' + that.data.deviceList[that.data.deviceListIndex].deviceId + "&clientType=" + that.data.deviceList[that.data.deviceListIndex].ProdModel,
     });
   },
 
@@ -729,40 +742,43 @@ Page({
   },
 
   deleteDevice(e) {
+    var that = this;
     wx.showModal({
       title: '确定删除?',
       success: function (res) {
         if (res.confirm) {
-          const id = this.data.deviceList[e.currentTarget.dataset.index].deviceId;
-          let name = _this.data.deviceListIndex !== null ? this.data.deviceList[_this.data.deviceListIndex].name : null;
-          const deviceList = this.data.deviceList.filter((v, i) => {
+          if (element.ProdModel == "MW-2AX(WIFI-N)") {}
+
+          const id = that.data.deviceList[e.currentTarget.dataset.index].deviceId;
+          let name = that.data.deviceListIndex !== null ? that.data.deviceList[that.data.deviceListIndex].name : null;
+          const deviceList = that.data.deviceList.filter((v, i) => {
             return id !== v.deviceId
           });
           // 取消订阅
-          app.unsubscribe(`/AIrSMArT_${_this.data.deviceList[e.currentTarget.dataset.index].name.split("BLUFI_")[1]}/user/pub_response`);
+          app.unsubscribe(`/AIrSMArT_${that.data.deviceList[e.currentTarget.dataset.index].name.split("BLUFI_")[1]}/user/pub_response`);
 
           wx.setStorage({
             key: "devicelist",
             data: JSON.stringify(deviceList),
             success() {
 
-              _this.setData({
+              that.setData({
                 deviceList,
                 thisDeviceMac: null
               });
 
               // 当前没有设备
               if (deviceList.length === 0) {
-                _this.setData({
+                that.setData({
                   actionIndex: null,
                   deviceListIndex: null,
                 });
               }
 
-              if (_this.data.deviceListIndex === null) {
+              if (that.data.deviceListIndex === null) {
                 return
               };
-              if (e.currentTarget.dataset.index === _this.data.deviceListIndex) {
+              if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
                 let index_ = null;
                 deviceList.map((v, index) => {
                   if (v.state === "online" && index_ === null) {
@@ -770,9 +786,9 @@ Page({
                   }
                 });
                 if (index_ !== null) {
-                  _this.actionDevice(index_);
+                  that.actionDevice(index_);
                 } else {
-                  _this.setData({
+                  that.setData({
                     actionIndex: null,
                     deviceListIndex: null,
                   });
@@ -780,7 +796,7 @@ Page({
               } else {
                 deviceList.map((v, index) => {
                   if (v.name === name) {
-                    _this.setData({
+                    that.setData({
                       deviceListIndex: index,
                     });
                   }
@@ -829,12 +845,11 @@ Page({
       key: "devicelist",
       data: JSON.stringify(finalList)
     });
-    console.log("dfadsfasdfasdfa==111==" + JSON.stringify(that.data.deviceList))
-    console.log("dfadsfasdfasdfa==222==" + that.data.deviceList.length)
+    // console.log("dfadsfasdfasdfa==111==" + JSON.stringify(that.data.deviceList))
+    // console.log("dfadsfasdfasdfa==222==" + that.data.deviceList.length)
   },
 
   updateDeviceList(deviceList) {
-    var that = this;
     if (deviceList.length == 0) return [];
 
     var finalList = [];
@@ -844,16 +859,16 @@ Page({
     var onNoLineList = [];
     for (var i = 0; i < deviceList.length; i++) {
       if (i > 0) {
-        if (connectDevice.state == "online") {
-          onLineList.push(element)
+        if (deviceList[i].state == "online") {
+          onLineList.push(deviceList[i])
         } else {
-          onNoLineList.push(element)
+          onNoLineList.push(deviceList[i])
         }
       }
     }
 
     onLineList.forEach(element => {
-      if (connectDevice.ProdModel == "MW-2AX(WIFI-N)") {
+      if (element.ProdModel == "MW-2AX(WIFI-N)") {
         onLineList.push(element)
       } else {
         onNoLineList.push(element)
@@ -863,7 +878,7 @@ Page({
     var onLineWifiList = [];
     var onLineNoWifiList = [];
     onNoLineList.forEach(element => {
-      if (connectDevice.ProdModel == "MW-2AX(WIFI-N)") {
+      if (element.ProdModel == "MW-2AX(WIFI-N)") {
         onLineWifiList.push(element)
       } else {
         onLineNoWifiList.push(element)
@@ -875,7 +890,7 @@ Page({
     var onNoLineWifiList = [];
     var onNoLineNoWifiList = [];
     onNoLineList.forEach(element => {
-      if (connectDevice.ProdModel == "MW-2AX(WIFI-N)") {
+      if (element.ProdModel == "MW-2AX(WIFI-N)") {
         onNoLineWifiList.push(element)
       } else {
         onNoLineNoWifiList.push(element)