瀏覽代碼

游戏状态关闭手机蓝牙处理

chaooo 1 年之前
父節點
當前提交
121bce69e8

+ 0 - 1
src/components/connection/brains/connecting.vue

@@ -1,6 +1,5 @@
 <script>
 import ble_store from "@/store/bluetooth";
-import Dialog from "../../../../static/vant/dialog/dialog";
 import Toast from "../../../../static/vant/toast/toast";
 import {LOG_DEBUG, LOG_WECHAT} from "../../../utils/log";
 

+ 10 - 0
src/components/connection/index.vue

@@ -152,6 +152,16 @@ export default {
     if($this.device_status*1 === 0){
       // 打开蓝牙扫描 重置游戏状态
       game_store.setters.setGameStatus(0);
+    }else if ($this.device_status*1 === 2) {
+      // 获取本机蓝牙适配器状态
+      wx.getBluetoothAdapterState({
+        complete: function (res) {
+          LOG_DEBUG("已连接界面,获取手机蓝牙状态:", JSON.stringify(res));
+          if (!res.available) {
+            $this.setDeviceStatus(5);
+          }
+        }
+      });
     }
     //判断是否游戏中
     $this.game_status = game_store.getters.getGameStatus()*1;

+ 21 - 9
src/components/connection/toys/selected.vue

@@ -2,6 +2,7 @@
 import {gameStart} from "@/requests/game";
 import Toast from "../../../../static/vant/toast/toast";
 import game_store from "../../../store/game";
+import {LOG_DEBUG} from "../../../utils/log";
 
 let $this;
 export default {
@@ -40,15 +41,26 @@ export default {
     },
     // 打开选择消费的选项框
     openPayWindow() {
-      if (!$this.device_bg) {
-        Toast.fail("请佩戴好脑机开始训练");
-        return false;
-      }
-      if (wx.getStorageSync("userinfo").level*1 === 11) {
-        $this.choosePayType(2);
-      } else {
-        $this.pay_window = true;
-      }
+      // 获取本机蓝牙适配器状态
+      wx.getBluetoothAdapterState({
+        complete: function (res) {
+          LOG_DEBUG("开始游戏之前,获取手机蓝牙状态:", JSON.stringify(res));
+          if (res.available) {
+            if (!$this.device_bg) {
+              Toast.fail("请佩戴好脑机开始训练");
+              return false;
+            }
+            if (wx.getStorageSync("userinfo").level*1 === 11) {
+              $this.choosePayType(2);
+            } else {
+              $this.pay_window = true;
+            }
+          } else {
+            wx.showToast({ title: "蓝牙已断开", icon: "error", duration: 2000});
+            $this.cancelConnect();
+          }
+        }
+      });
     },
     //点击隐藏
     hidePayWindow() {

+ 23 - 5
src/pages/games/play/index.vue

@@ -501,7 +501,8 @@ export default {
       $this.countdownInterval();
     },
     countdownInterval(){
-      clearInterval($timeout)
+      clearInterval($timeout);
+      let count = 0;
       $timeout = setInterval(() => {
         //判断是否游戏中
         let game_status = game_store.getters.getGameStatus()*1;
@@ -511,12 +512,29 @@ export default {
           game_store.setters.setPlayedTime($this.total_time-$this.play_time);
           $this.countdown_text = formatPlaySeconds($this.play_time);
           console.log("%c游戏倒计时", "color:red;", $this.countdown_text);
+          count += 1;
+          if( count % 10 === 0) {
+            // 每10秒获取手机蓝牙状态
+            $this.getBleAdapterState();
+          }
         } else {
           clearInterval($timeout);
           $this.endTheGame();
         }
       }, 1000);
     },
+    getBleAdapterState(){
+      // 获取本机蓝牙适配器状态
+      wx.getBluetoothAdapterState({
+        complete: function (res) {
+          LOG_DEBUG("游戏中,获取手机蓝牙状态:", JSON.stringify(res));
+          if (!res.available) {
+            clearInterval($timeout);
+            $this.endTheGame();
+          }
+        }
+      });
+    },
     /**
      * 点击结束游戏按钮
      */
@@ -560,15 +578,15 @@ export default {
      */
     postGameData() {
       let post_data = [];
-      for (let i = 0; i < this.save_index.length; i++) {
-        post_data[this.save_index[i]] = this.game_data[this.save_index[i]].join(',');
+      for (let i = 0; i < $this.save_index.length; i++) {
+        post_data[$this.save_index[i]] = $this.game_data[$this.save_index[i]]?$this.game_data[$this.save_index[i]].join(','):"";
       }
       //判断数据长度大于理想状态 需要对数组进行切割 逐个提交数据
       let $dataParams = {
         game_record_id: game_store.getters.getGameRecordId(),
         //数据段长度小于65 单个提交
-        line: this.game_data['att'].join(','),
-        line_med: this.game_data['med'].join(','),
+        line: $this.game_data['att']?$this.game_data['att'].join(','):"",
+        line_med: $this.game_data['med']?$this.game_data['med'].join(','):"",
         ...post_data,
         sn: ble_store.getters.getDeviceSn()
       };