Ver código fonte

解决了大数据量导致无法结束游戏的bug

yerong 4 anos atrás
pai
commit
50597d0227

+ 0 - 5
src/components/index/login.vue

@@ -20,11 +20,6 @@
     </view>
     <van-toast id="van-toast"/>
     <van-dialog id="van-dialog"/>
-    <van-overlay :show="official_show" @click="onClickHide">
-      <div class="official_container">
-        <official-account @load="official_success"></official-account>
-      </div>
-    </van-overlay>
 
   </div>
 </template>

+ 105 - 144
src/pages/start/index.vue

@@ -8,11 +8,7 @@
             <text class="title">已游玩时间</text>
           </view>
           <view class="text-lg   padding-top">
-            <van-count-down :time="mode_item.time" format="mm:ss" use-slot auto-start @finish="time_out"
-                            @change="watch_time" class="control-count-down"
-            >
-              <text>{{ timeData.minutes }}:{{ timeData.seconds }}</text>
-            </van-count-down>
+            <text class="count_down_text">{{ played_time_text }}</text>
           </view>
         </van-col>
         <van-col span="10" offset="4">
@@ -32,7 +28,7 @@
     <div class="game_status padding-tb">
       <van-row>
         <van-col span="10" offset="2">
-          <div class="flex flex-direction align-center justify-center" v-if="device_ok == true">
+          <div class="flex flex-direction align-center justify-center" v-if="device_bg">
             <img src="https://img.shuimuai.com/web/touhuanzhuangtai_2.png" alt="" class="device_img">
             <text class="text-default">已经正确佩戴</text>
           </div>
@@ -51,14 +47,14 @@
             <text class="text-default">设备电量</text>
           </div>
         </van-col>
-<!--        <van-col span="2">-->
-<!--          <view>-->
-<!--            <text>{{ rssi }}</text>-->
-<!--          </view>-->
-<!--          <view>-->
-<!--            <text>信号强度</text>-->
-<!--          </view>-->
-<!--        </van-col>-->
+        <!--        <van-col span="2">-->
+        <!--          <view>-->
+        <!--            <text>{{ rssi }}</text>-->
+        <!--          </view>-->
+        <!--          <view>-->
+        <!--            <text>信号强度</text>-->
+        <!--          </view>-->
+        <!--        </van-col>-->
       </van-row>
     </div>
 
@@ -215,11 +211,11 @@ export default {
       //判断是否结束游戏
       is_end: false,
       //  消耗的时间
-      played_time: 0,
-      played_time_text: "",
+      played_time: null,
+      played_time_text: "--:--",
 
       //  检测佩戴是否正确
-      device_ok: true,
+      device_bg: true,
 
       //  蓝牙信号强度
       rssi: 0
@@ -228,55 +224,48 @@ export default {
   methods: {
     //游戏结束方法
     game_finished() {
-      try {
-        bluetooth.shutdownSendControl($this._deviceId, $this._serviceId, $this._characteristicId)
-        //停止控制游戏
-        const countDown = $this.$mp.page.selectComponent('.control-count-down');
-        countDown.pause();
-        //取消监听低功耗蓝牙设备
-        wx.offBLECharacteristicValueChange()
-        Toast.success('已成功断开')
-        //设置游戏状态为 停止游戏
-        game_store.setters.setGameStatus(3)
-        //游戏结束重置游戏时间
-        wx.removeStorageSync('play_time')
-        wx.removeStorageSync('hide_time')
-        //游戏结束重置游戏模式
-        game_store.setters.setMode(0)
-        //删除游戏得id
-        game_store.setters.removeToyHex()
-        //关闭游戏常亮
-        wx.setKeepScreenOn({
-          keepScreenOn: false
-        })
-        //游戏结束传送一下数据
-        $this.post_data()
-
+      bluetooth.shutdownSendControl($this._deviceId, $this._serviceId, $this._characteristicId)
+      //取消监听低功耗蓝牙设备
+      wx.offBLECharacteristicValueChange()
+      //设置游戏状态为 停止游戏
+      game_store.setters.setGameStatus(3)
+      //游戏结束重置游戏时间
+      wx.removeStorageSync('play_time')
+      wx.removeStorageSync('hide_time')
+      //游戏结束重置游戏模式
+      game_store.setters.setMode(0)
+      //删除游戏得id
+      game_store.setters.removeToyHex()
+      const $proce = new Promise((resolve, reject) => {
+        let cost_time = $this.post_data()
+        console.log('提交数据使用了:', cost_time)
+        resolve(cost_time)
+      }).then((value) => {
         Toast.loading({
           forbidClick: true,
+          mask: true,
           message: "正在生成报告...",
-          duration: 800,
+          duration: value,
           onClose() {
             $this.game_over()
             $this.to_report()
           }
         })
-      } catch (e) {
-        console.log('error', e)
-      }
+      })
     },
     // 时间到
     time_out() {
       console.log('游戏结束')
-      $this.played_time = $this.play_time
       //游戏结束
       $this.game_finished()
     },
     to_report() {
-      mpvue.reLaunch({
+      mpvue.redirectTo({
         url: "/pages/report/main",
         success() {
           $this.timeData = {}
+          $this.attList = []
+          $this.medList = []
         }
       })
     },
@@ -295,73 +284,71 @@ export default {
       )
       return hexArr.join('');
     },
-
-    //  监听时间发生变动
-    watch_time(e) {
-      let $datetime = e.mp.detail
-      for (let $key in $datetime) {
-        if ($datetime[$key].toString().length == 1) {
-          $datetime[$key] = "0" + $datetime[$key]
-        }
-      }
-      $this.timeData = $datetime
-      $this.played_time += 1
-      if ($this.played_time % 60 == 0) {
-        $this.post_data()
-      }
-    },
     // 往后端推送一次定时数据
     post_data() {
-      let $params = {
-        game_record_id: game_store.getters.getGameRecordId(),
-        line: $this.attList,
-        line_med: $this.medList
-      }
-      if (!$params.line && !$params.line_med) {
-        return false
-      }
-      gameAddLine($params).then((res) => {
-        let $data = res.data
-        if ($data.code == 0) {
-          $this.attList = []
-          $this.medList = []
-        } else {
-          bluetooth.shutdownSendControl($this._deviceId, $this._serviceId, $this._characteristicId)
-          //停止控制游戏
-          const countDown = $this.$mp.page.selectComponent('.control-count-down');
-          countDown.pause();
-          //取消监听低功耗蓝牙设备
-          wx.offBLECharacteristicValueChange()
-          Toast.success('已成功断开')
-          //设置游戏状态为 停止游戏
-          game_store.setters.setGameStatus(3)
-          //游戏结束重置游戏时间
-          wx.removeStorageSync('play_time')
-          wx.removeStorageSync('hide_time')
-          //游戏结束重置游戏模式
-          game_store.setters.setMode(0)
-          //删除游戏得id
-          game_store.setters.removeToyHex()
-          //关闭游戏常亮
-          wx.setKeepScreenOn({
-            keepScreenOn: false
-          })
-          userjs.do_logout($this)
+      //判断数据长度大于理想状态 需要对数组进行切割 逐个提交数据
+      let $len = 60
+      if ($this.attList.length > 65) {
+        let $count = Math.ceil($this.attList.length / 60)
+
+        let index = 0
+        let inter = setInterval(() => {
+          if (index < $count) {
+            let $params = {
+              game_record_id: game_store.getters.getGameRecordId(),
+              //数据段长度大于65 多个提交
+              line: $this.attList.slice(index * $len, (index + 1) * $len),
+              line_med: $this.medList.slice(index * $len, (index + 1) * $len)
+            }
+            console.log($this.attList)
+            console.log(index * $len, (index + 1) * $len)
+            console.log($this.attList.slice(index * $len, (index + 1) * $len))
+            gameAddLine($params).then((res) => {
+              let $data = res.data
+              if ($data.code == 0) {
+                console.log('ok', $params['line'])
+              }
+            })
+          }
+          index++
+          if (index == $count) {
+            clearInterval(inter)
+          }
+        }, 800)
+        return $count * 800
+      } else {
+        let $params = {
+          game_record_id: game_store.getters.getGameRecordId(),
+          //数据段长度小于65 单个提交
+          line: $this.attList,
+          line_med: $this.medList
         }
-      })
+
+        if (!$params.line) {
+          return false
+        }
+        gameAddLine($params).then((res) => {
+          let $data = res.data
+          if ($data.code == 0) {
+            $this.attList = []
+            $this.medList = []
+          }
+        })
+        return 800
+      }
     },
     //  结束游戏
     game_over() {
       let $params = {
         game_record_id: game_store.getters.getGameRecordId(),
         type: $this.mode,
-        play_time: Math.abs($this.played_time)
+        play_time: $this.play_time - Math.abs($this.played_time)
       }
       console.log("结束游戏参数", $params)
       //
       gameEnd($params).then((res) => {
         let $data = res.data
-        $this.played_time = 0
+        $this.played_time = null
         //设置未结束游戏
         $this.is_end = false
         $this.att_list = []
@@ -410,14 +397,10 @@ export default {
         {
           id: 1,
           name: "次卡(10分钟)",
-          time: 10 * 60 * 1000,
-          err_title: "您的会员消费时间已用完",
         },
         {
           id: 2,
           name: "会员时间消费",
-          time: 30 * 60 * 1000,
-          err_title: "本次游玩时间已用完"
         }
 
       ]
@@ -426,17 +409,19 @@ export default {
       let $play_time = $this.play_time = game_store.getters.getPlayTime()
 
       //已经完了多长时间
-      let $played_time = $this.played_time = game_store.getters.getPlayedTime() ? game_store.getters.getPlayedTime() : 0
+      // $this.played_time = game_store.getters.getPlayedTime() ? game_store.getters.getPlayedTime() : 0
+      $this.played_time = game_store.getters.getPlayTime()
 
       //判断可玩时长超出了时间 就直接结束游戏
-      if ($played_time >= ($play_time * 1000)) {
-        $this.game_finished()
-      }
+      // if ($played_time >= ($play_time * 1000)) {
+      //   $this.game_finished()
+      // }
 
       $this.mode_item = {}
       $this.mode_item = mode_list[$this.mode - 1]
-      let $origin_time = $play_time * 1000
-      $this.mode_item['time'] = $origin_time - ($played_time * 1000)
+      // let $origin_time = $play_time * 1000
+      // $this.mode_item['time'] = $origin_time - ($played_time * 1000)
+      $this.mode_item['time'] = $play_time * 1000
     }
   }
   ,
@@ -463,47 +448,14 @@ export default {
     $this = this;
   },
   onShow() {
-    if ($this.timeData.minutes == '00' && $this.timeData.seconds == '00') {
+    if ($this.played_time == 0) {
       $this.time_out()
-    } else {
-      //获取 拉到后台得时间
-      let $hide_time = game_store.getters.getHideTime()
-      if ($hide_time) {
-        //获取当前时间
-        let $now = Math.round(new Date() / 1000)
-        //获得后台到当前时间差
-        let $residue = $now - $hide_time
-        //将原来的游玩时间+上 时间差
-        let $played_time = $this.played_time * 1 + $residue * 1
-        //重新设置游玩时间
-        game_store.setters.setPlayedTime($played_time)
-        $this.calThePlayedTime()
-      }
     }
   },
-  onHide() {
-    game_store.setters.setHideTime(Math.round(new Date() / 1000))
-  },
   onLoad(options) {
     if (options.end == 1) {
       $this.is_end = true
     }
-  },
-  onUnload() {
-    if (!$this.is_end) {
-      // 返回 的时候 暂停
-      const countDown = $this.$mp.page.selectComponent('.control-count-down');
-      countDown.pause();
-      //停止传输数据
-      bluetooth.sendPause($this._deviceId, $this._serviceId, $this._characteristicId)
-      bluetooth.sendEnd($this._deviceId, $this._serviceId, $this._characteristicId)
-      wx.offBLECharacteristicValueChange()
-      //存储时间
-      game_store.setters.setPlayedTime($this.played_time)
-      //设置未结束游戏
-      $this.is_end = false
-    }
-
   }
 }
 </script>
@@ -700,4 +652,13 @@ export default {
   position: relative;
   bottom: 5px;
 }
+
+/*倒计时样式*/
+.count_down_text {
+  font-size: 35px;
+  font-family: PingFang SC;
+  font-weight: 400;
+  color: #fff;
+  line-height: 28px;
+}
 </style>

+ 0 - 1
src/pages/start/main.json

@@ -4,7 +4,6 @@
     "van-col": "../../static/vant/col/index",
     "van-toast": "../../static/vant/toast/index",
     "van-popup": "../../static/vant/popup/index",
-    "van-count-down": "../../static/vant/count-down/index",
     "van-dialog": "../../static/vant/dialog/index"
   }
 }

+ 18 - 4
src/pages/test/index.vue

@@ -2,13 +2,14 @@
   <div id="playing_container">
     <div class="flex flex-direction align-center justify-center">
       <view class="elc_power_container">
-        <img src="https://img.shuimuai.com/web/dianchi_3.png" alt="" :style="{width:device_power+'%'}" class="device_elc">
+        <img src="https://img.shuimuai.com/web/dianchi_3.png" alt="" :style="{width:device_power+'%'}"
+             class="device_elc">
       </view>
       <text class="text-default">{{ device_power }}%</text>
       <text class="text-default">设备电量</text>
     </div>
-    			<button class="cu-btn" @click="device_power++">+</button>
-			<button class="cu-btn" @click="device_power--">-</button>
+    <button class="cu-btn" @click="device_power++">+</button>
+    <button class="cu-btn" @click="device_power--">-</button>
 
   </div>
 </template>
@@ -25,7 +26,20 @@ export default {
   methods: {}
   ,
   mounted() {
-  },
+    let $params = []
+    let $data = [40, 51, 40, 30, 56, 54, 53, 56, 60, 63, 67, 75, 70, 74, 61, 61, 51, 40, 20, 38, 44, 57, 69, 53, 54, 43, 27, 17, 10, 11, 1, 1, 11, 35, 61, 78, 75, 74, 64, 57, 60, 66, 66, 74, 51, 61, 67, 70, 90, 63, 51, 41, 41, 64, 64, 70, 69, 51, 63, 38, 29, 41, 63, 96, 93, 97, 100, 96, 100, 84, 80, 67, 63, 78, 91, 80, 70, 75, 54, 74, 77, 47, 43, 34, 51, 66, 75, 77, 66, 60, 51, 56, 57, 47, 38, 30, 43, 53, 53, 66]
+    console.log($data.length)
+    let $len = 60
+    console.log($data.slice(0, 60))
+    if ($data.length > 65) {
+      let $count = $data.length / 60
+      console.log(Math.ceil($count))
+      for (let index = 0; index < $count; index++) {
+        let $line = $data.slice(index * $len, (index + 1) * $len)
+        console.log($line)
+      }
+    }
+  }
 }
 </script>
 

+ 32 - 3
src/utils/bluetooth.js

@@ -605,7 +605,6 @@ export default {
           deviceId: characteristic.deviceId,
           success(res) {
             $this.rssi = Math.abs(res['RSSI'])
-            console.log('信号强度:',$this.rssi)
           }
         })
         if (hexStr.substr(0, 6) == '555520') {
@@ -635,7 +634,15 @@ export default {
         let $game_status = game_store.getters.getGameStatus()
         if ($game_status == 1 && $data) {
           try {
-            $this.do_datas($data)
+            if ($this.played_time > 0) {
+              //自定义定时器
+              $this.played_time -= 1
+              $this.played_time_text = that.formatPlaySeconds($this.played_time)
+              $this.do_datas($data)
+            }
+            if ($this.played_time == 0) {
+              $this.time_out()
+            }
           } catch (e) {
             console.log('调用方法失败', e)
           }
@@ -643,6 +650,28 @@ export default {
 
       }
     )
-  }
+  },
+  formatPlaySeconds(value) {
+    var secondTime = parseInt(value);// 秒
+    var minuteTime = 0;// 分
+    if (secondTime > 60) {//如果秒数大于60,将秒数转换成整数
+      //获取分钟,除以60取整数,得到整数分钟
+      minuteTime = parseInt(secondTime / 60);
+      //获取秒数,秒数取佘,得到整数秒数
+      secondTime = parseInt(secondTime % 60);
+      //如果分钟大于60,将分钟转换成小时
+    }
+    var result = "" + parseInt(secondTime);
 
+    if (minuteTime > 0) {
+      if (result.length == 1) {
+        result = '0' + result
+      }
+      if (parseInt(minuteTime).toString().length == 1){
+        minuteTime = '0' + parseInt(minuteTime)
+      }
+      result = "" + minuteTime + ":" + result;
+    }
+    return result;
+  }
 }