Forráskód Böngészése

feature: 综合处理兼容定时任务

Damon 7 hónapja
szülő
commit
80625d9ae6
4 módosított fájl, 164 hozzáadás és 103 törlés
  1. 146 96
      pages/deviceWake/deviceWake.js
  2. 4 4
      pages/deviceWake/deviceWake.wxml
  3. 2 0
      pages/index/index.js
  4. 12 3
      utils/util.js

+ 146 - 96
pages/deviceWake/deviceWake.js

@@ -1,7 +1,11 @@
 // pages/deviceWake/deviceWake.js
 const app = getApp();
-const { formatNumber }=require('./../../utils/util.js');
-const { deviceWakedetail }=require('./../../utils/api.js');
+const {
+  formatNumber
+} = require('./../../utils/util.js');
+const {
+  deviceWakedetail
+} = require('./../../utils/api.js');
 let _this = null
 Page({
 
@@ -17,12 +21,11 @@ Page({
     alarm_id_0_time: "00:00:00",
     switch2Checked: false,
     deviceMac: null,
-    devicerStatus: false,  // false 已断开连接
+    devicerStatus: false, // false 已断开连接
     alarm_id_0: {}, // 休眠
     alarm_id_1: {}, // 唤醒
     timeindex: 1,
-    timeArray: [
-      {
+    timeArray: [{
         id: 600,
         name: '10分钟'
       },
@@ -44,7 +47,7 @@ Page({
     time: '07:00:00',
     repeat: "",
     repeat0: "",
-    week: ["周日","周一","周二","周三","周四","周五","周六"],
+    week: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
     week_actives: [],
     isOne: true,
     wakeName: "",
@@ -57,26 +60,25 @@ Page({
       twoButText: "好的",
       footTop: 10,
     },
-    twoButCallback: ()=> {},
+    twoButCallback: () => {},
     toast_visible: false,
     // 后续新加需求
     deviceOther: null,
     updataDeviceInfo: false,
     repeatIndex: 0,
   },
-  onShow(options) {
-    app.PubMsg({type: "get_dev_info", DstDeviceName: _this.data.deviceMac});
-  },
+
   /**
    * 生命周期函数--监听页面加载
    */
   onLoad(options) {
     _this = this;
+
     deviceWakedetail({
       clientType: options.clientType,
       deviceMac: `AIrSMArT_${options.deviceId.split("BLUFI_")[1]}`,
-    }).then((res)=> {
-      if(res && res.length > 0) {
+    }).then((res) => {
+      if (res && res.length > 0) {
         this.setData({
           wakeName: res[0].audioName || null
         });
@@ -85,27 +87,37 @@ Page({
     this.data.clientType = options.clientType
     this.data.deviceMac = `AIrSMArT_${options.deviceId.split("BLUFI_")[1]}`;
     // this.data.deviceMac =  `AIrSMArT_${"BLUFI_7cdfa1fd3cfc".split("BLUFI_")[1]}`;
-    app.PubMsg({type: "get_dev_info", DstDeviceName: this.data.deviceMac});
+    app.PubMsg({
+      type: "get_dev_info",
+      DstDeviceName: this.data.deviceMac
+    });
 
     // 赋值按钮回调
     this.setData({
-      twoButCallback: ()=> {
+      twoButCallback: () => {
         _this.setData({
           toast_visible: false
         })
       }
     })
+  },
 
+  onShow(options) {
+    app.PubMsg({
+      type: "get_dev_info",
+      DstDeviceName: _this.data.deviceMac
+    });
   },
+
   mqttCallback(type, option) {
     let payloads = null;
-    if(option) {
+    if (option) {
       payloads = JSON.parse(option.payload);
     };
     console.log(payloads)
-    switch(type) {
+    switch (type) {
       case "message_onoffline":
-        if(payloads.state !== "online") {
+        if (payloads.state !== "online") {
           wx.showToast({
             title: '设备已断开连接',
             icon: 'none',
@@ -120,32 +132,32 @@ Page({
           })
         }
         break;
-      case"message":
+      case "message":
         // 设备信息
-        if(payloads.type === "get_dev_info") {
+        if (payloads.type === "get_dev_info") {
           _this.setData({
             deviceOther: payloads.other,
             updataDeviceInfo: true,
           });
-          if(payloads.other.alarm) {
-            payloads.other.alarm.map((v)=> {
-              if(v.alarm_id === "0") {
+          if (payloads.other.alarm) {
+            payloads.other.alarm.map((v) => {
+              if (v.alarm_id === "0") {
                 v.action = "update";
                 // 休眠
                 _this.setData({
                   alarm_id_0: v
                 });
-                
+
                 // 定时时间
-                if(v.on_off_timestamp !== "")  {
+                if (v.on_off_timestamp !== "") {
                   _this.setData({
                     alarm_id_0_time: v.on_off_timestamp
                   })
                 }
-                
+
 
                 // 是否启用
-                if(v.enable === "1")  {
+                if (v.enable === "1") {
                   _this.setData({
                     switch1Checked: true
                   })
@@ -156,25 +168,25 @@ Page({
                 }
 
                 // 重复
-                if(v.week_actives) {
+                if (v.week_actives) {
                   _this.setData({
                     repeat0: _this.repeatText(v.week_actives),
                   })
                 }
 
-              } else if(v.alarm_id === "1") {
+              } else if (v.alarm_id === "1") {
                 // 唤醒
                 _this.setData({
                   alarm_id_1: v
                 });
                 // 定时时间
-                if(v.on_off_timestamp !== "")  {
+                if (v.on_off_timestamp !== "") {
                   _this.setData({
                     time: v.on_off_timestamp
                   })
                 }
                 // 是否启用
-                if(v.enable === "1")  {
+                if (v.enable === "1") {
                   _this.setData({
                     switch2Checked: true
                   })
@@ -184,7 +196,7 @@ Page({
                   })
                 }
                 // 重复
-                if(v.week_actives) {
+                if (v.week_actives) {
                   _this.setData({
                     repeat: _this.repeatText(v.week_actives),
                   })
@@ -192,14 +204,17 @@ Page({
               }
             })
           }
-        } else if(payloads.type === "alert_set") {
-          if(payloads.code === 200) {
-            app.PubMsg({type: "get_dev_info", DstDeviceName: _this.data.deviceMac});
+        } else if (payloads.type === "alert_set") {
+          if (payloads.code === 200) {
+            app.PubMsg({
+              type: "get_dev_info",
+              DstDeviceName: _this.data.deviceMac
+            });
           } else {
             wx.showModal({
               title: '保存失败',
               content: '请保持设备网络通畅并检查是否已连接电源?',
-              success (res) {
+              success(res) {
                 if (res.confirm) {
                   console.log('用户点击确定')
                 } else if (res.cancel) {
@@ -209,14 +224,14 @@ Page({
             })
           }
         }
-      default:
+        default:
     }
   },
 
   // 休眠-选择时间
   switch1Change(e) {
     let other = this.data.alarm_id_0;
-    if(e.detail.value) {
+    if (e.detail.value) {
       _this.setData({
         switch1Checked: e.detail.value
       });
@@ -225,75 +240,98 @@ Page({
       other.on_off_timestamp = _this.data.alarm_id_0_time;
 
       // 有重复
-      if(other.week_actives.filter((v)=> v === 1).length > 0) {
+      if (other.week_actives.filter((v) => v === 1).length > 0) {
         // other.week_actives = [0,1,1,1,1,1,0],
         other.weekly_repeat = "1";
       } else {
         other.weekly_repeat = "0";
       };
       // 如果跨天了
-      if(!this.compareDate(_this.data.alarm_id_0_time, this.formatTime(thisTime, "h:m:s"))) {
-        const date = new Date((_this.formatTime(thisTime, "Y-M-D") + " 00:00:00").replace(/-/g,'/'));
+      if (!this.compareDate(_this.data.alarm_id_0_time, this.formatTime(thisTime, "h:m:s"))) {
+        const date = new Date((_this.formatTime(thisTime, "Y-M-D") + " 00:00:00").replace(/-/g, '/'));
         other.current_timestamp = this.formatTime(date.getTime() / 1000 + 86400, "Y-M-D h:m:s");
       };
-      app.PubMsg({type: "alert_set", DstDeviceName: _this.data.deviceMac, other});
+      app.PubMsg({
+        type: "alert_set",
+        DstDeviceName: _this.data.deviceMac,
+        other
+      });
     } else {
       _this.setData({
         switch1Checked: e.detail.value
       });
       other = this.setOther(other, 0);
-      app.PubMsg({type: "alert_set", DstDeviceName: _this.data.deviceMac, other});
+      app.PubMsg({
+        type: "alert_set",
+        DstDeviceName: _this.data.deviceMac,
+        other
+      });
     }
   },
   switch2Change(e) {
     _this.data.updataDeviceInfo = false;
     let other = _this.data.alarm_id_1;
-    if(e.detail.value) {
-      if(_this.data.deviceOther && _this.data.deviceOther.Power < 20) {
+    if (e.detail.value) {
+      if (_this.data.deviceOther && _this.data.deviceOther.Power < 20) {
         _this.setData({
           toast_visible: true,
         });
       };
-      app.PubMsg({type: "get_dev_info", DstDeviceName: _this.data.deviceMac});
+      app.PubMsg({
+        type: "get_dev_info",
+        DstDeviceName: _this.data.deviceMac
+      });
       // 循环查询
-      const Interval = setInterval(()=> {
-        if(_this.data.updataDeviceInfo) {
+      const Interval = setInterval(() => {
+        if (_this.data.updataDeviceInfo) {
           clearInterval(Interval);
           other.on_off_timestamp = _this.data.time;
           other = this.setOther(other, 1);
-          if(other.week_actives.filter((v)=> v === 1).length > 0) {
+          if (other.week_actives.filter((v) => v === 1).length > 0) {
             // other.week_actives = [0,1,1,1,1,1,0],
             other.weekly_repeat = "1";
           } else {
             other.weekly_repeat = "0";
           };
-          app.PubMsg({type: "alert_set", DstDeviceName: _this.data.deviceMac, other});
+          app.PubMsg({
+            type: "alert_set",
+            DstDeviceName: _this.data.deviceMac,
+            other
+          });
           // 需打开休眠
-          if(_this.data.alarm_id_0.enable === "0") {
+          if (_this.data.alarm_id_0.enable === "0") {
 
             const thisTime = Math.round(new Date() / 1000);
             const alarm_id_0 = this.setOther(_this.data.alarm_id_0, 1);
             alarm_id_0.on_off_timestamp = _this.data.alarm_id_0_time;
-      
+
             // 有重复
-            if(alarm_id_0.week_actives.filter((v)=> v === 1).length > 0) {
+            if (alarm_id_0.week_actives.filter((v) => v === 1).length > 0) {
               alarm_id_0.weekly_repeat = "1";
             } else {
               alarm_id_0.weekly_repeat = "0";
             };;
             // 如果跨天了
-            if(!this.compareDate(_this.data.alarm_id_0_time, this.formatTime(thisTime, "h:m:s"))) {
-              const date = new Date((_this.formatTime(thisTime, "Y-M-D") + " 00:00:00").replace(/-/g,'/'));
+            if (!this.compareDate(_this.data.alarm_id_0_time, this.formatTime(thisTime, "h:m:s"))) {
+              const date = new Date((_this.formatTime(thisTime, "Y-M-D") + " 00:00:00").replace(/-/g, '/'));
               alarm_id_0.current_timestamp = this.formatTime(date.getTime() / 1000 + 86400, "Y-M-D h:m:s");
             };
-            app.PubMsg({type: "alert_set", DstDeviceName: _this.data.deviceMac, other: alarm_id_0});
+            app.PubMsg({
+              type: "alert_set",
+              DstDeviceName: _this.data.deviceMac,
+              other: alarm_id_0
+            });
           }
 
         }
       }, 500);
     } else {
       other = this.setOther(other, 0);
-      app.PubMsg({type: "alert_set", DstDeviceName: _this.data.deviceMac, other});
+      app.PubMsg({
+        type: "alert_set",
+        DstDeviceName: _this.data.deviceMac,
+        other
+      });
     }
   },
   // bindPickerChange(e) {
@@ -308,7 +346,7 @@ Page({
   //   };
   //   app.PubMsg({type: "alert_set", DstDeviceName: _this.data.deviceMac, other});
   // },
-  bindTimeChange0: function(e) {
+  bindTimeChange0: function (e) {
     this.setData({
       alarm_id_0_time: e.detail.value + ":00"
     });
@@ -316,7 +354,7 @@ Page({
     other.on_off_timestamp = e.detail.value + ":00";
     other = this.setOther(other, 1);
     // 有重复
-    if(other.week_actives.filter((v)=> v === 1).length > 0) {
+    if (other.week_actives.filter((v) => v === 1).length > 0) {
       // other.week_actives = [0,1,1,1,1,1,0],
       other.weekly_repeat = "1";
     } else {
@@ -324,36 +362,48 @@ Page({
     };
     // 如果跨天了
     const thisTime = Math.round(new Date() / 1000);
-    if(!this.compareDate(_this.data.alarm_id_0_time, this.formatTime(thisTime, "h:m:s"))) {
-      const date = new Date((_this.formatTime(thisTime, "Y-M-D") + " 00:00:00").replace(/-/g,'/'));
+    if (!this.compareDate(_this.data.alarm_id_0_time, this.formatTime(thisTime, "h:m:s"))) {
+      const date = new Date((_this.formatTime(thisTime, "Y-M-D") + " 00:00:00").replace(/-/g, '/'));
       other.current_timestamp = this.formatTime(date.getTime() / 1000 + 86400, "Y-M-D h:m:s");
     };
-    app.PubMsg({type: "alert_set", DstDeviceName: _this.data.deviceMac, other});
+    app.PubMsg({
+      type: "alert_set",
+      DstDeviceName: _this.data.deviceMac,
+      other
+    });
   },
-  bindTimeChange: function(e) {
+  bindTimeChange: function (e) {
     this.setData({
       time: e.detail.value + ":00"
     });
     let other = this.data.alarm_id_1;
     other.on_off_timestamp = e.detail.value + ":00";
     other = this.setOther(other, 1);
-    app.PubMsg({type: "alert_set", DstDeviceName: _this.data.deviceMac, other});
+    app.PubMsg({
+      type: "alert_set",
+      DstDeviceName: _this.data.deviceMac,
+      other
+    });
   },
   setRepeat(arr) {
     let other = this.data.repeatIndex === "0" ? _this.data.alarm_id_0 : _this.data.alarm_id_1;
     other = this.setOther(other, 1);
     other.week_actives = arr;
-    other.weekly_repeat = arr.filter((v)=> v === 1).length > 0 ? "1" : "0";
+    other.weekly_repeat = arr.filter((v) => v === 1).length > 0 ? "1" : "0";
     // 倒计时
-    if(this.data.repeatIndex === "0") {
+    if (this.data.repeatIndex === "0") {
       // 如果跨天了
       const thisTime = Math.round(new Date() / 1000);
-      if(!this.compareDate(_this.data.alarm_id_0_time, this.formatTime(thisTime, "h:m:s"))) {
-        const date = new Date((_this.formatTime(thisTime, "Y-M-D") + " 00:00:00").replace(/-/g,'/'));
+      if (!this.compareDate(_this.data.alarm_id_0_time, this.formatTime(thisTime, "h:m:s"))) {
+        const date = new Date((_this.formatTime(thisTime, "Y-M-D") + " 00:00:00").replace(/-/g, '/'));
         other.current_timestamp = this.formatTime(date.getTime() / 1000 + 86400, "Y-M-D h:m:s");
       };
     }
-    app.PubMsg({type: "alert_set", DstDeviceName: _this.data.deviceMac, other});
+    app.PubMsg({
+      type: "alert_set",
+      DstDeviceName: _this.data.deviceMac,
+      other
+    });
   },
   goWakeList() {
     wx.navigateTo({
@@ -371,43 +421,43 @@ Page({
     deviceWakedetail({
       clientType: this.data.clientType,
       deviceMac: this.data.deviceMac,
-    }).then((res)=> {
+    }).then((res) => {
       this.setData({
         wakeName: res[0].audioName
       })
     })
   },
   /** 
- * 时间戳转化为年 月 日 时 分 秒 
- * number: 传入时间戳 
- * format:返回格式,支持自定义,但参数必须与formateArr里保持一致 
-*/
- formatTime(number, format) {
+   * 时间戳转化为年 月 日 时 分 秒 
+   * number: 传入时间戳 
+   * format:返回格式,支持自定义,但参数必须与formateArr里保持一致 
+   */
+  formatTime(number, format) {
 
-  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
-  var returnArr = [];
+    var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
+    var returnArr = [];
 
-  var date = new Date(number * 1000);
-  returnArr.push(date.getFullYear());
-  returnArr.push(formatNumber(date.getMonth() + 1));
-  returnArr.push(formatNumber(date.getDate()));
+    var date = new Date(number * 1000);
+    returnArr.push(date.getFullYear());
+    returnArr.push(formatNumber(date.getMonth() + 1));
+    returnArr.push(formatNumber(date.getDate()));
 
-  returnArr.push(formatNumber(date.getHours()));
-  returnArr.push(formatNumber(date.getMinutes()));
-  returnArr.push(formatNumber(date.getSeconds()));
+    returnArr.push(formatNumber(date.getHours()));
+    returnArr.push(formatNumber(date.getMinutes()));
+    returnArr.push(formatNumber(date.getSeconds()));
 
-  for (var i in returnArr) {
+    for (var i in returnArr) {
       format = format.replace(formateArr[i], returnArr[i]);
-  }
-  return format;
-},
+    }
+    return format;
+  },
   // type: 0 关闭  1 开启
   setOther(other, type) {
-    if(type === 0) {
+    if (type === 0) {
       other.enable = "0";
       other.alarm_name = "close";
       other.operation = "off";
-    } else if(type === 1){
+    } else if (type === 1) {
       other.enable = "1";
       other.alarm_name = "open";
       other.operation = "on";
@@ -415,7 +465,7 @@ Page({
     const thisTime = Math.round(new Date() / 1000);
     other.current_timestamp = this.formatTime(thisTime, "Y-M-D h:m:s");
     other.action = "update",
-    other.song_uri = "-1";
+      other.song_uri = "-1";
     other.version = 3;
     other.is_debug = app.globalData.is_debug
     return other
@@ -430,21 +480,21 @@ Page({
   repeatText(arr) {
 
     let text = "";
-    if(arr.toString() === [0, 1, 1, 1, 1, 1, 0].toString()) {
+    if (arr.toString() === [0, 1, 1, 1, 1, 1, 0].toString()) {
       text = "工作日";
-    } else if(arr.toString() === [1, 0, 0, 0, 0, 0, 1].toString()) {
+    } else if (arr.toString() === [1, 0, 0, 0, 0, 0, 1].toString()) {
       text = "周末";
     } else {
-      arr.map((v, index)=> {
-        if(v === 1) {
-          if(text === "") {
+      arr.map((v, index) => {
+        if (v === 1) {
+          if (text === "") {
             text = text + _this.data.week[index]
           } else {
             text = text + "、" + _this.data.week[index];
           }
         }
       });
-      if(text === "") {
+      if (text === "") {
         text = "不重复";
       };
     }

+ 4 - 4
pages/deviceWake/deviceWake.wxml

@@ -12,14 +12,14 @@
         <image src="./../../img/wk1.png" mode="heightFix"></image>
         <text>倒计时休眠</text>
         <view class="swicth-view">
-          <switch color="#6D00FF" checked="{{switch1Checked}}" bindchange="switch1Change"/>
+          <switch color="#6D00FF" checked="{{switch1Checked}}" bindchange="switch1Change" />
         </view>
       </view>
       <view class="page__bd" style="border: none; opacity: {{switch1Checked ? 1 : 0.4}};">
         <text>设置休眠时间</text>
         <view class="choice">
           <view class="mode" wx:if="{{!switch1Checked}}"></view>
-          <picker mode="time" bindchange="bindTimeChange0" value="{{alarm_id_0_time}}" >
+          <picker mode="time" bindchange="bindTimeChange0" value="{{alarm_id_0_time}}">
             <view class="picker">
               <text>{{alarm_id_0_time}}</text>
             </view>
@@ -42,7 +42,7 @@
         <image src="./../../img/wk2.png" mode="heightFix"></image>
         <text>定时唤醒</text>
         <view class="swicth-view">
-          <switch color="#6D00FF" checked="{{switch2Checked}}" bindchange="switch2Change"/>
+          <switch color="#6D00FF" checked="{{switch2Checked}}" bindchange="switch2Change" />
         </view>
       </view>
       <view class="page__bd" style="opacity: {{switch2Checked ? 1 : 0.4}};">
@@ -76,4 +76,4 @@
     </view>
   </view>
   <Toast visible="{{toast_visible}}" toast-data='{{toastData}}' twoButCallback="{{twoButCallback}}"></Toast>
-</view>
+</view>

+ 2 - 0
pages/index/index.js

@@ -663,6 +663,8 @@ Page({
     if (that.data.deviceListIndex === null) {
       return;
     };
+
+    console.log("gadsfqwwerqwerqweqr====" + JSON.stringify(that.data.deviceList[that.data.deviceListIndex]));
     wx.navigateTo({
       url: './../deviceWake/deviceWake?deviceId=' + that.data.deviceList[that.data.deviceListIndex].deviceId + "&clientType=" + that.data.deviceList[that.data.deviceListIndex].ProdModel,
     });

+ 12 - 3
utils/util.js

@@ -77,6 +77,8 @@ function request(url, data = {}, method = "GET", header = urlencodeType, isToken
     });
   }
   console.log("请求参数:", fullurl, data)
+  console.log("gadsfwqerqewqr====aaa==" + fullurl);
+  console.log("gadsfwqerqewqr====bbb==" + JSON.stringify(data));
   return new Promise((resolve, reject) => {
     wx.request({
       url: fullurl,
@@ -89,6 +91,7 @@ function request(url, data = {}, method = "GET", header = urlencodeType, isToken
       }, header),
       success(res) {
         console.log("接口:", fullurl, res)
+        console.log("gadsfwqerqewqr====00==" + JSON.stringify(res));
         if (res.statusCode !== 200) {
           wx.hideLoading();
           wx.showToast({
@@ -108,7 +111,9 @@ function request(url, data = {}, method = "GET", header = urlencodeType, isToken
           resolve(res.data.data);
         } else if (res.data.code === 401) {
           resolve(res.data);
-          wx.showToast({ title: '登录过期' })
+          wx.showToast({
+            title: '登录过期'
+          })
           // token过期
           try {
             getCurrentPages()[0].logOut();
@@ -124,11 +129,15 @@ function request(url, data = {}, method = "GET", header = urlencodeType, isToken
       },
       fail: (err) => {
         console.log("请求失败" + JSON.stringify(err));
+        console.log("gadsfwqerqewqr====11==" + JSON.stringify(err));
         _show_error("1");
         wx.hideLoading();
-        reject({ code: -1, msg: JSON.stringify(err) })
+        reject({
+          code: -1,
+          msg: JSON.stringify(err)
+        })
       },
-      complete() { }
+      complete() {}
     })
   })
 }