Explorar o código

feature: 未登录和登录状态列表数据在线与被选中问题

Damon hai 8 meses
pai
achega
c3643268cd
Modificáronse 3 ficheiros con 26 adicións e 7 borrados
  1. 8 3
      app.js
  2. 17 3
      pages/index/index.js
  3. 1 1
      utils/strings.js

+ 8 - 3
app.js

@@ -127,6 +127,7 @@ App({
         }
       };
     });
+
     //服务器连接异常的回调
     this.globalData.client.on("error", function (error) {
       console.log(" 服务器 error 的回调" + error)
@@ -204,19 +205,23 @@ App({
    * AIrSMArT_: 设备前缀
    */
   PubMsg(option, callback) {
-    if (this.globalData.client && this.globalData.client.connected) {
+    var that = this;
+    if (strings.isEmpty(that.globalData.userInfo)) {
+      return;
+    }
+    if (that.globalData.client && that.globalData.client.connected) {
       // wx.showLoading({
       //   title: '请稍后',
       // });
       const data = {
         "DstDeviceName": option.DstDeviceName,
-        "SrcDeviceName": `ALY_${this.globalData.userInfo.userId}`,
+        "SrcDeviceName": `ALY_${that.globalData.userInfo.userId}`,
         "type": option.type
       };
       if (option.other) {
         data.other = option.other;
       };
-      this.globalData.client.publish(`/${option.DstDeviceName}/user/sub_control`, `${JSON.stringify(data)}`, (err) => {
+      that.globalData.client.publish(`/${option.DstDeviceName}/user/sub_control`, `${JSON.stringify(data)}`, (err) => {
         if (err) {
           console.log("发布消息失败");
         }

+ 17 - 3
pages/index/index.js

@@ -71,6 +71,14 @@ Page({
       // 更新
       else if (that.data.deviceListIndex !== null) {
         that.actionDevice(that.data.deviceListIndex);
+      } else if (app.globalData.client == null) {
+        app.connect();
+      } else if (that.data.deviceListIndex == null && that.data.deviceList.length > 0) {
+        that.actionDevice(0)
+      }
+    } else {
+      if (app.globalData.client == null) {
+        app.connect();
       }
     }
   },
@@ -79,7 +87,6 @@ Page({
   onDeviceLoad() {
     var that = this;
     var devicelist = wx.getStorageSync("devicelist") || "";
-    console.log("gadfasdfqwerqwerqewrqr====" + devicelist);
     if (!strings.isEmpty(devicelist)) {
       var list = JSON.parse(devicelist);
       that.updateDeviceList(list, true);
@@ -132,16 +139,20 @@ Page({
       payloads = JSON.parse(option.payload);
     };
 
+    console.log("gadsfadsfadsfa==8888===" + type);
     switch (type) {
       ///连接成功订阅
       case "connect":
         that.subscribeDevicesStatus();
         break;
       case "message_onoffline":
-        that.online(payloads);
+        that.onlineDevice(payloads);
         break;
       case "message":
         // 接收设备播放信息
+        // if (!that.data.isLogin) {
+        //   return;
+        // }
         if (payloads.type === "get_position" && payloads.other) {
           let actionIndex = null;
           that.data.channelData.map((v, index) => {
@@ -235,7 +246,7 @@ Page({
   ///连上就调用2次 处理离线在线问题 wifi设备 BLUFI_
   /// payloads:{"uuid":"AIrSMArT_7cdfa1fcbb24","state":"online","userid":"1"}
   // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
-  online(payloads) {
+  onlineDevice(payloads) {
     // 设置在线状态
     var that = this;
     console.log("gadsfadsfadsfa==777===" + JSON.stringify(payloads));
@@ -536,6 +547,9 @@ Page({
 
   actionDevice(index) {
     var that = this;
+    if (!that.data.isLogin) {
+      return;
+    }
     if (that.data.deviceList.length <= index) {
       return;
     };

+ 1 - 1
utils/strings.js

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