index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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" auto-start @finish="time_out"></van-count-down>
  12. </view>
  13. </van-col>
  14. <van-col span="10" offset="4">
  15. <view class="text-sm">
  16. <text class="title">计费模式</text>
  17. <text class="type padding-left-sm">{{ mode_item.name }}</text>
  18. </view>
  19. <button class="cu-btn bg-red text-white margin-top-sm" @click="game_finished">
  20. <img src="https://img.shuimuai.com/m_duankainaohuan.png" class="cut_brain_icon" alt="">
  21. <text class="padding-lr cut_text text-sm" @click="game_finished">停止游玩</text>
  22. </button>
  23. </van-col>
  24. </van-row>
  25. </div>
  26. <!-- 设备状态-->
  27. <div class="game_status padding-tb">
  28. <van-row>
  29. <van-col span="10" offset="2">
  30. <div class="flex flex-direction align-center justify-center">
  31. <img src="https://img.shuimuai.com/web/touhuanzhuangtai_2.png" alt="" class="device_img">
  32. <text class="text-default">已经正确佩戴</text>
  33. </div>
  34. </van-col>
  35. <van-col span="10">
  36. <div class="flex flex-direction align-center justify-center">
  37. <van-row class="elc_power_container text-center">
  38. <van-col v-for="(item,index) in elc_power/25" :key="index" span="6">
  39. <img src="https://img.shuimuai.com/web/dianchi_3.png" alt="" class="device_elc">
  40. </van-col>
  41. </van-row>
  42. <text class="text-default">{{ elc_power }}%</text>
  43. <text class="text-default">设备电量</text>
  44. </div>
  45. </van-col>
  46. </van-row>
  47. </div>
  48. <!-- 图标模块-->
  49. <div class="ec_container">
  50. <view class="bar">
  51. <view>
  52. <view class="line"></view>
  53. <view class="title">大脑状态</view>
  54. </view>
  55. <view class="label">
  56. <view>
  57. <view class="dot dot-orange"></view>
  58. <view class="name">Att(专注度)</view>
  59. </view>
  60. <view>
  61. <view class="dot dot-pink"></view>
  62. <view class="name">Att(放松度)</view>
  63. </view>
  64. </view>
  65. </view>
  66. <view class="chart">
  67. <mpvue-echarts :echarts="echarts" :onInit="attCharts" canvasId="demo-canvas"/>
  68. </view>
  69. <view class="bar">
  70. <view>
  71. <view class="line"></view>
  72. <view class="title">基本脑波</view>
  73. </view>
  74. <view class="label">
  75. <view>
  76. <view class="dot dot-blue"></view>
  77. <view class="name">Delta</view>
  78. </view>
  79. <view>
  80. <view class="dot dot-green"></view>
  81. <view class="name">Theta</view>
  82. </view>
  83. <view>
  84. <view class="dot dot-yellow"></view>
  85. <view class="name">Beta</view>
  86. </view>
  87. <view>
  88. <view class="dot dot-orange-yellow"></view>
  89. <view class="name">Alpha</view>
  90. </view>
  91. </view>
  92. </view>
  93. <view class="chart">
  94. <mpvue-echarts :echarts="echarts" :onInit="medCharts" canvasId="demo-canvas-1"/>
  95. </view>
  96. </div>
  97. <!-- 窗口弹出-->
  98. <van-popup :show="pop_show" @close="close_pop" round :close-on-click-overlay="false">
  99. <div class="err_container flex flex-direction align-center justify-around">
  100. <view>
  101. <text class="title">
  102. 本次游玩时间已到
  103. </text>
  104. </view>
  105. <view>
  106. <text class="sub-title">
  107. 您的{{ mode_item.name }}消费已用完
  108. </text>
  109. </view>
  110. <img src="https://img.shuimuai.com/web/sign_notimeJfish.png" class="err_img" alt="">
  111. <button class="cu-btn lg bg-primary text-white" @click="game_finished">查看报告</button>
  112. </div>
  113. </van-popup>
  114. </div>
  115. </template>
  116. <script>
  117. import mpvueEcharts from 'mpvue-echarts'
  118. import echarts from '../../../static/echarts.min';
  119. import util from '../../utils/index'
  120. import bluetooth from "../../utils/bluetooth";
  121. import game_store from "@/store/game";
  122. var att_charts,
  123. med_charts,
  124. $this
  125. // 大脑图表初始化
  126. function initAttChart(canvas, width, height) {
  127. att_charts = echarts.init(canvas, null, {
  128. width: width,
  129. height: height
  130. });
  131. canvas.setChart(att_charts);
  132. var option = util.getLineOption([0], [0]); // ECharts 配置项
  133. att_charts.setOption(option);
  134. return att_charts; // 返回 chart 后可以自动绑定触摸操作
  135. }
  136. // 我的设备图表初始化
  137. function initMedChart(canvas, width, height) {
  138. med_charts = echarts.init(canvas, null, {
  139. width: width,
  140. height: height
  141. });
  142. canvas.setChart(med_charts);
  143. var option = util.getBaseOption([0], [0], [0], [0]); // ECharts 配置项
  144. med_charts.setOption(option);
  145. return med_charts; // 返回 chart 后可以自动绑定触摸操作
  146. }
  147. export default {
  148. name: "index_container",
  149. components: {
  150. mpvueEcharts
  151. },
  152. data() {
  153. return {
  154. // 使用类型 1次数 2时间 0未选择
  155. mode: 0,
  156. mode_list: [
  157. {
  158. id: 2,
  159. name: "会员时间消费",
  160. time: 20 * 60 * 1000,
  161. err_title: "本次游玩时间已用完"
  162. },
  163. {
  164. id: 1,
  165. name: "次卡(10分钟)",
  166. time: 10 * 60 * 1000,
  167. err_title: "您的会员消费时间已用完",
  168. }
  169. ],
  170. mode_item: {},
  171. pop_show: false,
  172. elc_power: 100,
  173. // 折线图
  174. echarts,
  175. attCharts: initAttChart,
  176. medCharts: initMedChart,
  177. user_info: {},
  178. _deviceId: "",
  179. _serviceId: "",
  180. _characteristicId: "",
  181. toy_index: 0
  182. }
  183. },
  184. methods: {
  185. //游戏结束方法
  186. game_finished() {
  187. let $deviceId = game_store.getters.getDeviceId
  188. let $serviceId = game_store.getters.getServiceId
  189. let $charatorId = game_store.getters.getCharacterId
  190. //停止控制游戏
  191. bluetooth.sendEnd($deviceId, $serviceId, $charatorId)
  192. //关闭脑环
  193. wx.closeBLEConnection({
  194. deviceId: $deviceId,
  195. success(res) {
  196. Toast.success('已成功断开')
  197. //设置游戏状态为 停止游戏
  198. game_store.commit('setGameStatus', 0)
  199. $this.to_report()
  200. }
  201. })
  202. },
  203. // 关闭弹窗
  204. close_pop() {
  205. $this.pop_show = false
  206. },
  207. // 时间到
  208. time_out() {
  209. $this.pop_show = true
  210. },
  211. to_report() {
  212. mpvue.navigateTo({
  213. url: "/pages/report/main"
  214. })
  215. },
  216. //获取蓝牙设备服务
  217. getBLEDeviceServices(deviceId) {
  218. wx.getBLEDeviceServices({
  219. deviceId,
  220. success: (res) => {
  221. for (let i = 0; i < res.services.length; i++) {
  222. if (res.services[i].isPrimary) {
  223. this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid)
  224. return
  225. }
  226. }
  227. }
  228. })
  229. },
  230. //获取蓝牙设备某个服务中所有特征值
  231. getBLEDeviceCharacteristics(deviceId, serviceId) {
  232. wx.getBLEDeviceCharacteristics({
  233. deviceId,
  234. serviceId,
  235. success: (res) => {
  236. console.log('getBLEDeviceCharacteristics success', res.characteristics)
  237. for (let i = 0; i < res.characteristics.length; i++) {
  238. let item = res.characteristics[i]
  239. if (item.properties.read) {
  240. wx.readBLECharacteristicValue({
  241. deviceId,
  242. serviceId,
  243. characteristicId: item.uuid,
  244. })
  245. }
  246. if (item.properties.write) {
  247. $this.canWrite = true
  248. $this._deviceId = deviceId
  249. $this._serviceId = serviceId
  250. $this._characteristicId = item.uuid
  251. game_store.commit('setDeviceId', deviceId)
  252. game_store.commit('setServiceId', serviceId)
  253. game_store.commit('setCid', item.uuid)
  254. bluetooth.sendPause(deviceId, serviceId, item.uuid)
  255. bluetooth.sendConnect($this.toy_index, deviceId, serviceId, item.uuid)
  256. }
  257. if (item.properties.notify || item.properties.indicate) {
  258. wx.notifyBLECharacteristicValueChange({
  259. deviceId,
  260. serviceId,
  261. characteristicId: item.uuid,
  262. state: true,
  263. })
  264. }
  265. }
  266. },
  267. fail(res) {
  268. console.error('getBLEDeviceCharacteristics', res)
  269. }
  270. })
  271. // 操作之前先监听,保证第一时间获取数据
  272. wx.onBLECharacteristicValueChange((characteristic) => {
  273. console.log("收到" + $this.ab2hex(characteristic.value));
  274. let hexStr = $this.ab2hex(characteristic.value)
  275. let $flag = bluetooth.connect_toy(hexStr)
  276. if ($flag) {
  277. bluetooth.sendControl($this._deviceId, $this._serviceId, $this._characteristicId)
  278. }
  279. })
  280. },
  281. // ArrayBuffer转16进度字符串示例
  282. ab2hex(buffer) {
  283. var hexArr = Array.prototype.map.call(
  284. new Uint8Array(buffer),
  285. function (bit) {
  286. return ('00' + bit.toString(16)).slice(-2)
  287. }
  288. )
  289. return hexArr.join('');
  290. }
  291. }
  292. ,
  293. mounted() {
  294. $this._deviceId = game_store.getters.getDeviceId
  295. $this.toy_index = game_store.getters.getToyIndex
  296. $this.getBLEDeviceServices($this._deviceId)
  297. // 模拟蓝牙输入值
  298. //55 55 20 01 00 02 1b 03 33 04 18 00 15 d7 00 75 37 00 0d da 00 0d 9b 00 10 c2 00 0a f9 00 01 fb 00 04 5a 39
  299. let hex = "5555200100021b033304180015d7007537000dda000d9b0010c2000af90001fb00045a39"
  300. let $data = bluetooth.get_big_data(hex)
  301. console.log($data)
  302. },
  303. created() {
  304. $this = this;
  305. },
  306. onLoad($option) {
  307. $this.userinfo = wx.getStorageSync('userinfo')
  308. $this.mode_list.forEach(($val, $index) => {
  309. if ($val['id'] == 2) {
  310. $val['time'] = parseInt($this.userinfo['play_time']) * 60 * 1000
  311. }
  312. })
  313. console.log($this.mode_list)
  314. $this.mode = $option.mode
  315. $this.mode_list.forEach(($val, $index) => {
  316. if ($option.mode == $val['id']) {
  317. $this.mode_item = $val
  318. }
  319. })
  320. }
  321. }
  322. </script>
  323. <style scoped>
  324. @import "index.css";
  325. </style>