index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <template>
  2. <div id="playing_container">
  3. <!-- 计费面板模块-->
  4. <div class="game_panel padding-top-sm">
  5. <van-row gutter="16">
  6. <van-col span="8" offset="2">
  7. <view class="text-sm">
  8. <text class="title">已游玩时间</text>
  9. </view>
  10. <view class="text-lg padding-top">
  11. <van-count-down :time="mode_item.time" format="mm:ss" use-slot auto-start @finish="time_out"
  12. @change="watch_time" class="control-count-down"
  13. >
  14. <text>{{ timeData.minutes }}:{{ timeData.seconds }}</text>
  15. </van-count-down>
  16. </view>
  17. </van-col>
  18. <van-col span="10" offset="4">
  19. <view class="text-sm">
  20. <text class="title">计费模式</text>
  21. <text class="type padding-left-sm">{{ mode_item.name }}</text>
  22. </view>
  23. <button class="cu-btn bg-red text-white margin-top-sm" @click="game_finished">
  24. <img src="https://img.shuimuai.com/m_duankainaohuan.png" class="cut_brain_icon" alt="">
  25. <text class="padding-lr cut_text text-sm">停止游玩</text>
  26. </button>
  27. </van-col>
  28. </van-row>
  29. </div>
  30. <!-- 设备状态-->
  31. <div class="game_status padding-tb">
  32. <van-row>
  33. <van-col span="10" offset="2">
  34. <div class="flex flex-direction align-center justify-center">
  35. <img src="https://img.shuimuai.com/web/touhuanzhuangtai_2.png" alt="" class="device_img">
  36. <text class="text-default">已经正确佩戴</text>
  37. </div>
  38. </van-col>
  39. <van-col span="10">
  40. <div class="flex flex-direction align-center justify-center">
  41. <van-row class="elc_power_container text-center">
  42. <van-col v-for="(item,index) in elc_power/25" :key="index" span="6">
  43. <img src="https://img.shuimuai.com/web/dianchi_3.png" alt="" class="device_elc">
  44. </van-col>
  45. </van-row>
  46. <text class="text-default">{{ elc_power }}%</text>
  47. <text class="text-default">设备电量</text>
  48. </div>
  49. </van-col>
  50. </van-row>
  51. </div>
  52. <!-- 图标模块-->
  53. <div class="ec_container">
  54. <view class="bar">
  55. <view>
  56. <view class="line"></view>
  57. <view class="title">大脑状态</view>
  58. </view>
  59. <view class="label">
  60. <view>
  61. <view class="dot dot-orange"></view>
  62. <view class="name">Att(专注度)</view>
  63. </view>
  64. <view>
  65. <view class="dot dot-pink"></view>
  66. <view class="name">Att(放松度)</view>
  67. </view>
  68. </view>
  69. </view>
  70. <view class="chart">
  71. <mpvue-echarts :echarts="echarts" :onInit="attCharts" canvasId="demo-canvas"/>
  72. </view>
  73. <view class="bar">
  74. <view>
  75. <view class="line"></view>
  76. <view class="title">基本脑波</view>
  77. </view>
  78. <view class="label">
  79. <view>
  80. <view class="dot dot-blue"></view>
  81. <view class="name">Delta</view>
  82. </view>
  83. <view>
  84. <view class="dot dot-green"></view>
  85. <view class="name">Theta</view>
  86. </view>
  87. <view>
  88. <view class="dot dot-yellow"></view>
  89. <view class="name">Beta</view>
  90. </view>
  91. <view>
  92. <view class="dot dot-orange-yellow"></view>
  93. <view class="name">Alpha</view>
  94. </view>
  95. </view>
  96. </view>
  97. <view class="chart">
  98. <mpvue-echarts :echarts="echarts" :onInit="medCharts" canvasId="demo-canvas-1"/>
  99. </view>
  100. </div>
  101. <!-- 窗口弹出-->
  102. <van-popup :show="pop_show" @close="close_pop" round :close-on-click-overlay="false">
  103. <div class="err_container flex flex-direction align-center justify-around">
  104. <view>
  105. <text class="title">
  106. 本次游玩时间已到
  107. </text>
  108. </view>
  109. <view>
  110. <text class="sub-title">
  111. 您的{{ mode_item.name }}消费已用完
  112. </text>
  113. </view>
  114. <img src="https://img.shuimuai.com/web/sign_notimeJfish.png" class="err_img" alt="">
  115. <button class="cu-btn lg bg-primary text-white" @click="game_finished">查看报告</button>
  116. </div>
  117. </van-popup>
  118. <van-toast id="van-toast"/>
  119. </div>
  120. </template>
  121. <script>
  122. import mpvueEcharts from 'mpvue-echarts'
  123. import echarts from '../../../static/echarts.min';
  124. import util from '../../utils/index'
  125. import bluetooth from "../../utils/bluetooth";
  126. import game_store from "@/store/game";
  127. import Toast from '../../../static/vant/toast/toast';
  128. import {game_devices, gameAddLine, gameEnd} from "../../requests/game";
  129. var att_charts,
  130. med_charts,
  131. $this
  132. // 大脑图表初始化
  133. function initAttChart(canvas, width, height) {
  134. att_charts = echarts.init(canvas, null, {
  135. width: width,
  136. height: height
  137. });
  138. canvas.setChart(att_charts);
  139. var option = util.getLineOption([0], [0]); // ECharts 配置项
  140. att_charts.setOption(option);
  141. return att_charts; // 返回 chart 后可以自动绑定触摸操作
  142. }
  143. // 我的设备图表初始化
  144. function initMedChart(canvas, width, height) {
  145. med_charts = echarts.init(canvas, null, {
  146. width: width,
  147. height: height
  148. });
  149. canvas.setChart(med_charts);
  150. var option = util.getBaseOption([0], [0], [0], [0]); // ECharts 配置项
  151. med_charts.setOption(option);
  152. return med_charts; // 返回 chart 后可以自动绑定触摸操作
  153. }
  154. export default {
  155. name: "index_container",
  156. components: {
  157. mpvueEcharts
  158. },
  159. data() {
  160. return {
  161. // 使用类型 1次数 2时间 0未选择
  162. mode: 0,
  163. mode_list: [
  164. {
  165. id: 2,
  166. name: "会员时间消费",
  167. time: 20 * 60 * 1000,
  168. err_title: "本次游玩时间已用完"
  169. },
  170. {
  171. id: 1,
  172. name: "次卡(10分钟)",
  173. time: 10 * 60 * 1000,
  174. err_title: "您的会员消费时间已用完",
  175. }
  176. ],
  177. timeData: {},
  178. mode_item: {},
  179. pop_show: false,
  180. elc_power: 100,
  181. // 折线图
  182. echarts,
  183. attCharts: initAttChart,
  184. medCharts: initMedChart,
  185. user_info: {},
  186. //设备值
  187. _deviceId: "",
  188. _serviceId: "",
  189. _characteristicId: "",
  190. toy_index: 0,
  191. toy_id: 0,
  192. toy: {},
  193. toy_hex: "",
  194. play_time: 0,
  195. //图标数据集合
  196. att_list: [],
  197. med_list: [],
  198. delta_list: [],
  199. alpha_list: [],
  200. beta_list: [],
  201. theta_list: []
  202. }
  203. },
  204. methods: {
  205. //游戏结束方法
  206. game_finished() {
  207. //停止控制游戏
  208. bluetooth.sendPause($this._deviceId, $this._serviceId, $this._characteristicId)
  209. bluetooth.sendEnd($this._deviceId, $this._serviceId, $this._characteristicId)
  210. const countDown = $this.$mp.page.selectComponent('.control-count-down');
  211. countDown.pause();
  212. Toast.success('已成功断开')
  213. //设置游戏状态为 停止游戏
  214. game_store.commit('setGameStatus', 0)
  215. //游戏结束传送一下数据
  216. $this.post_data()
  217. $this.game_over()
  218. $this.to_report()
  219. },
  220. // 关闭弹窗
  221. close_pop() {
  222. $this.pop_show = false
  223. },
  224. // 时间到
  225. time_out() {
  226. $this.pop_show = true
  227. $this.game_finished()
  228. },
  229. to_report() {
  230. mpvue.reLaunch({
  231. url: "/pages/report/main"
  232. })
  233. },
  234. //获取蓝牙设备服务
  235. getBLEDeviceServices(deviceId) {
  236. wx.getBLEDeviceServices({
  237. deviceId,
  238. success: (res) => {
  239. for (let i = 0; i < res.services.length; i++) {
  240. if (res.services[i].isPrimary) {
  241. this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid)
  242. return
  243. }
  244. }
  245. }
  246. })
  247. },
  248. //获取蓝牙设备某个服务中所有特征值
  249. getBLEDeviceCharacteristics(deviceId, serviceId) {
  250. wx.getBLEDeviceCharacteristics({
  251. deviceId,
  252. serviceId,
  253. success: (res) => {
  254. console.log('getBLEDeviceCharacteristics success', res.characteristics)
  255. for (let i = 0; i < res.characteristics.length; i++) {
  256. let item = res.characteristics[i]
  257. if (item.properties.read) {
  258. wx.readBLECharacteristicValue({
  259. deviceId,
  260. serviceId,
  261. characteristicId: item.uuid,
  262. })
  263. }
  264. if (item.properties.write) {
  265. $this.canWrite = true
  266. $this._deviceId = deviceId
  267. $this._serviceId = serviceId
  268. $this._characteristicId = item.uuid
  269. game_store.commit('setDeviceId', deviceId)
  270. game_store.commit('setServiceId', serviceId)
  271. game_store.commit('setCid', item.uuid)
  272. bluetooth.sendPause(deviceId, serviceId, item.uuid)
  273. //获取玩具的值
  274. bluetooth.sendConnect($this.toy_hex, deviceId, serviceId, item.uuid)
  275. }
  276. if (item.properties.notify || item.properties.indicate) {
  277. wx.notifyBLECharacteristicValueChange({
  278. deviceId,
  279. serviceId,
  280. characteristicId: item.uuid,
  281. state: true,
  282. })
  283. }
  284. }
  285. },
  286. fail(res) {
  287. console.error('getBLEDeviceCharacteristics', res)
  288. }
  289. })
  290. // 操作之前先监听,保证第一时间获取数据
  291. wx.onBLECharacteristicValueChange((characteristic) => {
  292. console.log("收到" + $this.ab2hex(characteristic.value));
  293. let hexStr = $this.ab2hex(characteristic.value)
  294. let $data = bluetooth.get_big_data(hexStr)
  295. if ($data) {
  296. $this.att_list.push($data['att'])
  297. $this.med_list.push($data['med'])
  298. $this.delta_list.push(Math.round($data['delta'] / 10000))
  299. $this.alpha_list.push(Math.round($data['alpha'] / 1000000))
  300. $this.theta_list.push(Math.round($data['theta'] / 10000))
  301. $this.beta_list.push(Math.round($data['beta']) / 1000000)
  302. //通过专注放松度 画图
  303. let $option = util.getLineOption($this.att_list, $this.med_list)
  304. att_charts.setOption($option)
  305. //通过基本脑波发送数据
  306. let $base_option = util.getBaseOption($this.delta_list, $this.theta_list, $this.alpha_list, $this.beta_list)
  307. med_charts.setOption($base_option)
  308. }
  309. //获取设备电量
  310. let $power = bluetooth.get_device_elc(hexStr)
  311. if ($power) {
  312. $this.elc_power = $power
  313. }
  314. //连接玩具
  315. console.log('玩具hexStr', hexStr)
  316. let $flag = bluetooth.connect_toy(hexStr, $this._deviceId, $this._serviceId, $this._characteristicId)
  317. //连接不上设备
  318. if ($flag == false) {
  319. // $this.game_finished()
  320. console.log('info', $this._deviceId, $this._serviceId, $this._characteristicId)
  321. }
  322. })
  323. },
  324. // ArrayBuffer转16进度字符串示例
  325. ab2hex(buffer) {
  326. var hexArr = Array.prototype.map.call(
  327. new Uint8Array(buffer),
  328. function (bit) {
  329. return ('00' + bit.toString(16)).slice(-2)
  330. }
  331. )
  332. return hexArr.join('');
  333. },
  334. // 监听时间发生变动
  335. watch_time(e) {
  336. let $datetime = e.mp.detail
  337. $this.timeData = $datetime
  338. //时间倒数
  339. if ($datetime.seconds == '00') {
  340. // 时间满一分钟 则提交一次数据
  341. // 添加折线图的点
  342. $this.post_data()
  343. }
  344. },
  345. // 往后端推送一次定时数据
  346. post_data() {
  347. let $params = {
  348. game_record_id: game_store.getters.getGameRecordId,
  349. line: $this.att_list,
  350. line_med: $this.med_list
  351. }
  352. gameAddLine($params).then((res) => {
  353. let $data = res.data
  354. if ($data.code == 0) {
  355. $this.att_list = []
  356. $this.med_list = []
  357. $this.delta_list = []
  358. $this.alpha_list = []
  359. $this.theta_list = []
  360. $this.beta_list = []
  361. console.log('推送数据成功')
  362. } else {
  363. console.log('推送数据失败')
  364. }
  365. })
  366. },
  367. // 结束游戏
  368. game_over() {
  369. let $current_time = $this.timeData.minutes * 60 + $this.timeData.seconds
  370. let $play_time = $this.play_time - $current_time
  371. if ($this.mode == 2) {
  372. let $userinfo = wx.getStorageSync('userinfo')
  373. $play_time = $userinfo['play_time'] - $current_time
  374. }
  375. let $params = {
  376. record_id: game_store.getters.getGameRecordId,
  377. type: $this.mode,
  378. play_time: $play_time
  379. }
  380. gameEnd($params).then((res) => {
  381. let $data = res.data
  382. })
  383. }
  384. }
  385. ,
  386. mounted() {
  387. },
  388. created() {
  389. $this = this;
  390. },
  391. onShow() {
  392. $this._deviceId = game_store.getters.getDeviceId
  393. $this.getBLEDeviceServices($this._deviceId)
  394. // 筛选 玩具id
  395. $this.toy_hex = game_store.getters.getToyHex
  396. // bluetooth.sendConnect($this.toy_hex, $this._deviceId, $this._serviceId, $this._characteristicId)
  397. //筛选模式
  398. $this.mode = game_store.getters.getMode
  399. //获取用户游玩时间
  400. let $play_time = $this.play_time = game_store.getters.getPlayTime
  401. $this.mode_list.forEach(($val, $index) => {
  402. $val['time'] = $play_time * 1000
  403. if ($val['id'] == $this.mode) {
  404. $this.mode_item = $val
  405. }
  406. })
  407. },
  408. onLoad() {
  409. },
  410. onUnload() {
  411. const countDown = $this.$mp.page.selectComponent('.control-count-down');
  412. countDown.pause();
  413. bluetooth.sendPause($this._deviceId, $this._serviceId, $this._characteristicId)
  414. bluetooth.sendEnd($this._deviceId, $this._serviceId, $this._characteristicId)
  415. let $play_time = $this.timeData.minutes * 60 + $this.timeData.seconds
  416. console.log($play_time)
  417. game_store.commit('setPlayTime', $play_time)
  418. }
  419. }
  420. </script>
  421. <style scoped>
  422. @import "index.css";
  423. </style>