Jelajahi Sumber

完成了蓝牙连接设备得模块

yerong 4 tahun lalu
induk
melakukan
1a2ca53798

+ 1 - 1
dist/wx

@@ -1 +1 @@
-Subproject commit b458880b330cc3d73dc6ba2d905e8716ccc81cd9
+Subproject commit d5ffa1a7d02218a046df333f81e51a55a1bbd318

+ 35 - 64
src/components/device/device.vue

@@ -6,10 +6,10 @@
           <div class="line"></div>
           <div class="title">我的设备</div>
         </div>
-<!--        <div class="my_msg" @click="get_toy_list">-->
-<!--          <text class="cuIcon-mark"></text>-->
-<!--          <text class="under_line">获取设备列表</text>-->
-<!--        </div>-->
+        <!--        <div class="my_msg" @click="get_toy_list">-->
+        <!--          <text class="cuIcon-mark"></text>-->
+        <!--          <text class="under_line">获取设备列表</text>-->
+        <!--        </div>-->
       </div>
       <div v-if="connect_toy==0">
 
@@ -36,6 +36,7 @@
         <div class="connecting_toy">
           <toy_connecting :connect_toy="connect_toy"
                           :deviceId="_deviceId"
+                          :toy_id="toy_action"
                           @open_choose_toy="open_choose_toy"
                           @change_toy_connect_status="change_toy_connect_status"
                           @change_status="change_device_status"
@@ -115,57 +116,7 @@ export default {
           id: 1,
           name: "水柱音箱",
           img: "https://img.shuimuai.com/web/toy_car.png"
-        },
-        {
-          id: 2,
-          name: "喷雾恐龙(大)",
-          img: "https://img.shuimuai.com/web/toy_dinasour.png"
-        },
-        {
-          id: 3,
-          name: "喷雾恐龙(小)",
-          img: "https://img.shuimuai.com/web/toy_dinasour.png"
-        },
-        {
-          id: 4,
-          name: "轨道车",
-          img: "https://img.shuimuai.com/web/toy_car_roat.png"
-        },
-        {
-          id: 5,
-          name: "碰碰车",
-          img: "https://img.shuimuai.com/web/toy_car.png"
-        },
-        {
-          id: 6,
-          name: "小车(大)",
-          img: "https://img.shuimuai.com/web/toy_car.png"
-        },
-        {
-          id: 7,
-          name: "小车(中)",
-          img: "https://img.shuimuai.com/web/toy_car.png"
-        },
-        {
-          id: 8,
-          name: "小车(小)",
-          img: "https://img.shuimuai.com/web/toy_car.png"
-        },
-        {
-          id: 9,
-          name: "飞行器(大)",
-          img: "https://img.shuimuai.com/web/toy_car.png"
-        },
-        {
-          id: 10,
-          name: "飞行器(小)",
-          img: "https://img.shuimuai.com/web/toy_car.png"
-        },
-        {
-          id: 11,
-          name: "水母灯",
-          img: "https://img.shuimuai.com/web/toy_car.png"
-        },
+        }
       ],
       toy_action: 1,
       connect_show: false,
@@ -194,6 +145,16 @@ export default {
   methods: {
     //打开 扫描二维码
     open_scan() {
+      //
+      // wx.scanCode({
+      //   onlyFromCamera: true,
+      //   success: (res) => {
+      //     let $data = res
+      //     if ($data.result) {
+      //       let url = decodeURIComponent($data.result)
+      //       let $code = url.match(/\?code=(.*)/)[1];
+      //       $this.code = $code
+      //打开蓝牙设备
       wx.openBluetoothAdapter({
         success(res) {
           //判断已经打开连接了
@@ -214,15 +175,7 @@ export default {
           }
         }
       })
-      //
-      // wx.scanCode({
-      //   onlyFromCamera: true,
-      //   success: (res) => {
-      //     let $data = res
-      //     if ($data.result) {
-      //       let url = decodeURIComponent($data.result)
-      //       let $code = url.match(/\?code=(.*)/)[1];
-      //       console.log($code)
+
       //     }
       //   }
       // })
@@ -329,8 +282,26 @@ export default {
     },
     //  获取游戏设备玩具
     get_toy_list() {
+//      清空toy_list
+      $this.toy_list = []
+//       device_id: "1"
+// name: "音响"
+// img: "penquan.png"
+// bluetooth: "aadd0a0001"
       game_devices().then((res) => {
         let $data = res.data
+        let $toylist = $data.data
+        let _item = {}
+        $toylist.forEach(($val, $index) => {
+          _item = {
+            id: parseInt($val['device_id']),
+            name: $val['name'],
+            img: "https://img.shuimuai.com/" + $val['img'],
+            hex: $val['bluetooth']
+          }
+          $this.toy_list.push(_item)
+        })
+        game_store.commit('setToyList', $this.toy_list)
       })
     },
 

+ 5 - 10
src/components/device/toy/connecting.vue

@@ -162,7 +162,7 @@ import Toast from "../../../../static/vant/toast/toast";
 let $this;
 export default {
   name: "connected",
-  props: ['connect_toy', 'deviceId'],
+  props: ['connect_toy', 'deviceId', 'toy_id'],
   data() {
     return {
       pay_window: false,
@@ -170,14 +170,6 @@ export default {
       pay_type: 0
     }
   },
-  watch: {
-    // pay_type($new, $old) {
-    //   if ($new > 0) {
-    //     $this.change_toy_connect_status(4)
-    //     $this.to_playing()
-    //   }
-    // }
-  },
   methods: {
     open_choose_toy() {
       $this.$emit('open_choose_toy', true)
@@ -202,9 +194,12 @@ export default {
           Toast.success({
             message: $data.errmsg,
             onClose() {
+              //调用父级得方法游戏开始得方法
               $this.$emit('game_start')
+              //设置游戏中
+              $this.connect_toy = 4
               mpvue.navigateTo({
-                url: "/pages/start/main?mode=" + $event
+                url: "/pages/start/main?mode=" + $event + "&toy_id=" + $this.toy_id
               })
             }
           })

+ 1 - 1
src/components/index/login.vue

@@ -1,6 +1,6 @@
 <template>
   <div id="index_login_container" class="animation-slide-right">
-    <view class="solid-bottom text-xl padding text-center">
+    <view class=" text-xl padding text-center">
       <text class=" text-bold title_color">欢迎来到水母星球!</text>
     </view>
     <view class="text-df text-center slogen">在此你将会体验到操控专注力的神奇感觉</view>

+ 4 - 2
src/components/index/regist.vue

@@ -126,14 +126,15 @@ export default {
         js_code: $this.js_code,
         invite: $this.invite,
       }
-      if ($this._userinfo) {
+      // if ($this._userinfo) {
         $params['user_name'] = $this._userinfo.nickName
         $params['portrait'] = $this._userinfo.avatarUrl
         $params['sex'] = $this._userinfo.gender
-      }
+      // }
 
       if ($this.title == "注册") {
         userRegister($params).then((res) => {
+          console.log(res)
           let $data = res.data
           if (!$data['code']) {
             Toast.clear()
@@ -144,6 +145,7 @@ export default {
         })
       } else if ($this.title == "登录") {
         userLogin($params).then((res) => {
+          console.log(res)
           let $data = res.data
           Toast.clear()
           if (!$data['code']) {

+ 1 - 1
src/components/index/scan.vue

@@ -1,6 +1,6 @@
 <template>
   <div id="index_login_container" class="animation-slide-right">
-    <view class="solid-bottom text-xl padding text-center">
+    <view class=" text-xl padding text-center">
       <text class=" text-bold title_color">欢迎来到水母星球!</text>
     </view>
     <view class="text-df text-center slogen">在此你将会体验到操控专注力的神奇感觉</view>

+ 1 - 1
src/components/static/device.css

@@ -130,7 +130,7 @@
 
 /*玩具图片*/
 .toy_img {
-  width: 75px;
+  width: 35px;
   height: 65px;
 }
 

+ 1 - 0
src/pages/index/index.vue

@@ -109,6 +109,7 @@ export default {
     let $token = wx.getStorageSync('token')
     if ($token) {
       this.$store.commit('set_token', $token);
+      this.$store.commit('set_login', true);
       this.is_login = this.$store.getters.get_login_status
     }
   }

+ 67 - 19
src/pages/start/index.vue

@@ -8,7 +8,10 @@
             <text class="title">已游玩时间</text>
           </view>
           <view class="text-lg   padding-top">
-            <van-count-down :time="mode_item.time" format="mm:ss" auto-start @finish="time_out"></van-count-down>
+            <van-count-down :time="mode_item.time" format="mm:ss" use-slot auto-start @finish="time_out"
+                            @change="watch_time">
+              <text>{{ timeData.minutes }}:{{ timeData.seconds }}</text>
+            </van-count-down>
           </view>
         </van-col>
         <van-col span="10" offset="4">
@@ -116,6 +119,8 @@
         <button class="cu-btn lg bg-primary text-white" @click="game_finished">查看报告</button>
       </div>
     </van-popup>
+    <van-toast id="van-toast"/>
+
   </div>
 </template>
 
@@ -125,6 +130,8 @@ import echarts from '../../../static/echarts.min';
 import util from '../../utils/index'
 import bluetooth from "../../utils/bluetooth";
 import game_store from "@/store/game";
+import Toast from '../../../static/vant/toast/toast';
+import {game_devices, gameAddLine} from "../../requests/game";
 
 var att_charts,
   med_charts,
@@ -183,6 +190,7 @@ export default {
           err_title: "您的会员消费时间已用完",
         }
       ],
+      timeData: {},
       mode_item: {},
       pop_show: false,
       elc_power: 100,
@@ -195,6 +203,8 @@ export default {
       _serviceId: "",
       _characteristicId: "",
       toy_index: 0,
+      toy_id: 0,
+      toy: {},
       att_list: [],
       med_list: [],
       delta_list: [],
@@ -209,23 +219,24 @@ export default {
       let $deviceId = game_store.getters.getDeviceId
       let $serviceId = game_store.getters.getServiceId
       let $charatorId = game_store.getters.getCharacterId
-      console.log('delta_list',$this.delta_list)
-      console.log('alpha_list',$this.alpha_list)
-      console.log('beta_list',$this.beta_list)
-      console.log('theta_list',$this.theta_list)
+      console.log('delta_list', $this.delta_list)
+      console.log('alpha_list', $this.alpha_list)
+      console.log('beta_list', $this.beta_list)
+      console.log('theta_list', $this.theta_list)
       //停止控制游戏
       bluetooth.sendEnd($deviceId, $serviceId, $charatorId)
       //关闭脑环
-      wx.closeBLEConnection({
-        deviceId: $deviceId,
-        success(res) {
-          Toast.success('已成功断开')
-          //设置游戏状态为 停止游戏
-          game_store.commit('setGameStatus', 0)
-          $this.to_report()
-        }
-      })
-
+      // wx.closeBLEConnection({
+      //   deviceId: $deviceId,
+      //   success(res) {
+      //   }
+      // })
+      const countDown = $this.$mp.page.selectComponent('.control-count-down');
+      countDown.pause();
+      Toast.success('已成功断开')
+      //设置游戏状态为 停止游戏
+      game_store.commit('setGameStatus', 0)
+      $this.to_report()
     },
     //  关闭弹窗
     close_pop() {
@@ -234,6 +245,7 @@ export default {
     // 时间到
     time_out() {
       $this.pop_show = true
+      $this.game_finished()
     },
     to_report() {
       mpvue.navigateTo({
@@ -279,7 +291,7 @@ export default {
               game_store.commit('setServiceId', serviceId)
               game_store.commit('setCid', item.uuid)
               bluetooth.sendPause(deviceId, serviceId, item.uuid)
-              bluetooth.sendConnect($this.toy_index, deviceId, serviceId, item.uuid)
+              bluetooth.sendConnect($this.toy, deviceId, serviceId, item.uuid)
             }
 
             if (item.properties.notify || item.properties.indicate) {
@@ -305,9 +317,9 @@ export default {
           $this.att_list.push($data['att'])
           $this.med_list.push($data['med'])
           $this.delta_list.push(Math.round($data['delta'] / 10000))
-          $this.alpha_list.push(Math.round($data['alpha'] / 10000))
+          $this.alpha_list.push(Math.round($data['alpha'] / 1000000))
           $this.theta_list.push(Math.round($data['theta'] / 10000))
-          $this.beta_list.push(Math.round($data['beta']) / 10000)
+          $this.beta_list.push(Math.round($data['beta']) / 1000000)
 
           //通过专注放松度 画图
           let $option = util.getLineOption($this.att_list, $this.med_list)
@@ -340,6 +352,30 @@ export default {
         }
       )
       return hexArr.join('');
+    },
+
+    //  监听时间发生变动
+    watch_time(e) {
+      let $datetime = e.mp.detail
+      $this.timeData = $datetime
+      if ($datetime.seconds == '00') {
+        console.log('满一分钟')
+      }
+    },
+    // 往后端推送一次定时数据
+    post_data() {
+      let $params = {
+        "game_record_id": 1,
+        "line": $this.att_list
+      }
+      gameAddLine($params).then((res) => {
+        let $data = res.data
+        if ($data.code == 0) {
+          console.log('推送数据成功')
+        } else {
+          console.log('推送数据失败')
+        }
+      })
     }
   }
   ,
@@ -352,19 +388,31 @@ export default {
     $this = this;
   },
   onLoad($option) {
+    //获取用户游玩时间
     $this.userinfo = wx.getStorageSync('userinfo')
     $this.mode_list.forEach(($val, $index) => {
       if ($val['id'] == 2) {
         $val['time'] = parseInt($this.userinfo['play_time']) * 60 * 1000
       }
     })
-    console.log($this.mode_list)
+    //筛选模式
     $this.mode = $option.mode
     $this.mode_list.forEach(($val, $index) => {
       if ($option.mode == $val['id']) {
         $this.mode_item = $val
       }
     })
+    //  筛选 玩具id
+    $this.toy_id = $option.toy_id
+    game_devices().then((res) => {
+      let $data = res.data
+      let $toylist = $data.data
+      $toylist.forEach(($val, $index) => {
+        if ($val['device_id'] == $this.toy_id) {
+          $this.toy = $val
+        }
+      })
+    })
   }
 }
 </script>

+ 8 - 1
src/store/game.js

@@ -10,7 +10,8 @@ export default new Vuex.Store({
     characteristicId: "",
     //游戏状态 0 不在游戏,1游戏中
     game_status: 0,
-    toy_index: 0
+    toy_index: 0,
+    toy_list: []
   },
   mutations: {
     setDeviceId(state, deviceId) {
@@ -28,6 +29,9 @@ export default new Vuex.Store({
     },
     setToyIndex(state, toy_index) {
       state.toy_index = toy_index
+    },
+    setToyList(state, toy_list) {
+      state.toy_list = toy_list
     }
   },
   getters: {
@@ -45,6 +49,9 @@ export default new Vuex.Store({
     },
     getToyIndex(state) {
       return state.toy_index
+    },
+    getToyList(state) {
+      return state.toy_list
     }
   },
 

+ 5 - 2
src/utils/bluetooth.js

@@ -64,12 +64,15 @@ export default {
     })
   },
   // 发送连接玩具指令
-  sendConnect(index, deviceId, serviceId, Cid) {
+  sendConnect($toy, deviceId, serviceId, Cid) {
+    //获取最后两个字节
+    let $toy_hex = $toy['bluetooth'].substr($toy['bluetooth'].length - 2, -2)
+
     const that = this
     // 玩具对应指令数组
     let arr = ['0x01', '0x02', '0x03', '0x04', '0x05', '0x06', '0x07', '0x08', '0x09', '0x0a', '0x0b']
     let arr1 = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b]
-    let CheckSum = ((0x03 + 0x00 + arr1[index] + 0x00 + 0x0a) ^ 0xFFFFFFFF) & 0xFF
+    let CheckSum = ((0x03 + 0x00 + ('0x' + $toy_hex) + 0x00 + 0x0a) ^ 0xFFFFFFFF) & 0xFF
     // 向蓝牙设备发送指令
     let buffer = new ArrayBuffer(8)
     let dataView = new DataView(buffer)