index.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. <template>
  2. <div id="playing_container">
  3. <!-- 计费面板模块-->
  4. <div class="game_panel padding-top-sm">
  5. <van-row gutter="16">
  6. <van-col
  7. span="8"
  8. offset="2"
  9. >
  10. <view class="text-sm">
  11. <text class="title">本次游玩时间</text>
  12. </view>
  13. <view class="text-lg padding-top">
  14. <text class="count_down_text">{{ played_time_text }}</text>
  15. </view>
  16. </van-col>
  17. <van-col
  18. span="10"
  19. offset="4"
  20. >
  21. <view class="text-sm">
  22. <text class="title">计费模式</text>
  23. <text class="type padding-left-sm">{{ mode_item.name }}</text>
  24. </view>
  25. <button
  26. class="cu-btn bg-red text-white margin-top-sm"
  27. @click="game_finished"
  28. >
  29. <img
  30. src="https://img.shuimuai.com/m_duankainaohuan.png"
  31. class="cut_brain_icon"
  32. alt=""
  33. >
  34. <text class="padding-lr cut_text text-sm">结束游戏</text>
  35. </button>
  36. </van-col>
  37. </van-row>
  38. </div>
  39. <!-- 设备状态-->
  40. <div class="game_status padding-tb">
  41. <van-row>
  42. <van-col
  43. span="10"
  44. offset="2"
  45. >
  46. <div
  47. class="flex flex-direction align-center justify-center"
  48. v-if="device_bg"
  49. >
  50. <img
  51. src="https://img.shuimuai.com/web/touhuanzhuangtai_2.png"
  52. alt=""
  53. class="device_img"
  54. >
  55. <text class="text-default">已经正确佩戴</text>
  56. </div>
  57. <div
  58. class="flex flex-direction align-center justify-center"
  59. v-else
  60. >
  61. <img
  62. src="https://img.shuimuai.com/web/touhuanzhuangtai_3.png"
  63. alt=""
  64. class="device_img"
  65. >
  66. <text class="text-default">未正确佩戴</text>
  67. </div>
  68. </van-col>
  69. <van-col span="10">
  70. <div class="flex flex-direction align-center justify-center">
  71. <view class="elc_power_container">
  72. <img
  73. src="https://img.shuimuai.com/web/dianchi_3.png"
  74. alt=""
  75. :style="{width:device_power+'%'}"
  76. class="device_elc"
  77. >
  78. </view>
  79. <text class="text-default">{{ device_power }}%</text>
  80. <text class="text-default">设备电量</text>
  81. </div>
  82. </van-col>
  83. <!-- <van-col span="2">
  84. <div class="flex flex-direction align-center justify-center">
  85. <view>
  86. <text>-{{ rssi }}dbm</text>
  87. </view>
  88. <view>
  89. <text class="text-default">信号强度</text>
  90. </view>
  91. </div>
  92. </van-col> -->
  93. </van-row>
  94. </div>
  95. <!-- 图标模块-->
  96. <div class="ec_container">
  97. <view class="chart_view_first margin-tb-xl">
  98. <view class="bar">
  99. <view>
  100. <view class="line"></view>
  101. <view class="title">大脑状态</view>
  102. </view>
  103. <view class="label">
  104. <view>
  105. <view class="dot dot-orange"></view>
  106. <view class="name">Att(专注度)</view>
  107. </view>
  108. <view>
  109. <view class="dot dot-pink"></view>
  110. <view class="name">Med(放松度)</view>
  111. </view>
  112. </view>
  113. </view>
  114. <view class="chart">
  115. <mpvue-echarts
  116. :echarts="echarts"
  117. :onInit="attCharts"
  118. canvasId="canvasId-demo1"
  119. />
  120. </view>
  121. </view>
  122. <view class="chart_view_second margin-tb-xl">
  123. <view class="bar">
  124. <view>
  125. <view class="line"></view>
  126. <view class="title">基本脑波</view>
  127. </view>
  128. <view class="label">
  129. <view>
  130. <view class="dot dot-blue"></view>
  131. <view class="name">Delta</view>
  132. </view>
  133. <view>
  134. <view class="dot dot-green"></view>
  135. <view class="name">Theta</view>
  136. </view>
  137. <view>
  138. <view class="dot dot-yellow"></view>
  139. <view class="name">Beta</view>
  140. </view>
  141. <view>
  142. <view class="dot dot-orange-yellow"></view>
  143. <view class="name">Alpha</view>
  144. </view>
  145. </view>
  146. </view>
  147. <view class="chart">
  148. <mpvue-echarts
  149. :echarts="echarts"
  150. :onInit="medCharts"
  151. canvasId="canvasId-demo2"
  152. />
  153. </view>
  154. </view>
  155. </div>
  156. <van-toast id="van-toast" />
  157. <van-dialog id="van-dialog" />
  158. </div>
  159. </template>
  160. <script>
  161. import util, { formatSeconds } from "../../utils/index";
  162. import bluetooth from "../../utils/bluetooth";
  163. import game_store from "@/store/game";
  164. import Toast from "../../../static/vant/toast/toast";
  165. import Dialog from "../../../static/vant/dialog/dialog";
  166. import { gameAddLine, gameEnd } from "../../requests/game";
  167. import mpvueEcharts from "mpvue-echarts";
  168. import echarts from "../../../static/echarts.min";
  169. var att_charts, med_charts, $this;
  170. // 大脑图表初始化
  171. function initAttChart(canvas, width, height) {
  172. att_charts = echarts.init(canvas, null, {
  173. width: width,
  174. height: height,
  175. });
  176. canvas.setChart(att_charts);
  177. let option = {}; // ECharts 配置项
  178. att_charts.setOption(option);
  179. return att_charts; // 返回 chart 后可以自动绑定触摸操作
  180. }
  181. // 我的设备图表初始化
  182. function initMedChart(canvas, width, height) {
  183. med_charts = echarts.init(canvas, null, {
  184. width: width,
  185. height: height,
  186. });
  187. canvas.setChart(med_charts);
  188. let option = {}; // ECharts 配置项
  189. med_charts.setOption(option);
  190. return med_charts; // 返回 chart 后可以自动绑定触摸操作
  191. }
  192. export default {
  193. name: "index_container",
  194. components: {
  195. mpvueEcharts,
  196. },
  197. data() {
  198. return {
  199. // 折线图
  200. echarts,
  201. attCharts: initAttChart,
  202. medCharts: initMedChart,
  203. // 使用类型 1次数 2时间 0未选择
  204. mode: 0,
  205. timeData: {},
  206. mode_item: {},
  207. device_power: 100,
  208. user_info: {},
  209. //设备值
  210. _deviceId: "",
  211. _serviceId: "",
  212. _characteristicId: "",
  213. toy_index: 0,
  214. toy_id: 0,
  215. toy: {},
  216. toy_hex: "",
  217. play_time: 0,
  218. //图标数据集合
  219. att_list: [],
  220. med_list: [],
  221. delta_list: [],
  222. alpha_list: [],
  223. beta_list: [],
  224. theta_list: [],
  225. //临时数据集合
  226. attList: [],
  227. medList: [],
  228. //判断是否结束游戏
  229. is_end: false,
  230. // 消耗的时间
  231. played_time: null,
  232. played_time_text: "--:--",
  233. //结束游戏时选择类型的时间
  234. over_play_time: 0,
  235. // 检测佩戴是否正确
  236. device_bg: true,
  237. // 蓝牙信号强度
  238. rssi: 0,
  239. game_status: 0,
  240. };
  241. },
  242. methods: {
  243. //游戏结束方法
  244. game_finished() {
  245. bluetooth.sendEnd(
  246. $this._deviceId,
  247. $this._serviceId,
  248. $this._characteristicId
  249. );
  250. $this.$emit("closePop", true);
  251. //取消监听低功耗蓝牙设备
  252. wx.offBLECharacteristicValueChange();
  253. //设置游戏状态为 停止游戏
  254. game_store.setters.setGameStatus(3);
  255. //游戏结束重置游戏时间
  256. wx.removeStorageSync("play_time");
  257. //删除游戏专注数据
  258. game_store.remover.rmGameAttMedDatas();
  259. //删除隐藏时间
  260. game_store.remover.rmHideTime();
  261. //游戏结束重置游戏模式
  262. // game_store.setters.setMode(0);
  263. //删除游戏得id
  264. game_store.setters.removeToyHex();
  265. // 结束游戏在提交报告函数里
  266. $this.post_data();
  267. Toast.loading({
  268. forbidClick: true,
  269. mask: true,
  270. message: "正在生成报告...",
  271. duration: 0,
  272. onClose() {
  273. bluetooth.shutdownSendControl(
  274. $this._deviceId,
  275. $this._serviceId,
  276. $this._characteristicId
  277. );
  278. },
  279. });
  280. },
  281. to_report() {
  282. mpvue.redirectTo({
  283. url: "/pages/report/main",
  284. success() {
  285. $this.timeData = {};
  286. $this.attList = [];
  287. $this.medList = [];
  288. },
  289. });
  290. },
  291. // 开始游戏
  292. start_game() {
  293. wx.setKeepScreenOn({
  294. keepScreenOn: true,
  295. });
  296. //打开内存警告
  297. // wx.onMemoryWarning(() => {
  298. // console.log('内存告急警告')
  299. // game_store.setGameAttMedDatas({
  300. // att: $this.attList,
  301. // med: $this.medList
  302. // })
  303. // })
  304. // //初始化数据
  305. // $this.played_time = null
  306. // //设置未结束游戏
  307. // $this.is_end = false
  308. // $this.att_list = []
  309. // $this.med_list = []
  310. // $this.delta_list = []
  311. // $this.alpha_list = []
  312. // $this.theta_list = []
  313. // $this.beta_list = []
  314. bluetooth.watchingDevice($this);
  315. bluetooth.sendControl(
  316. $this._deviceId,
  317. $this._serviceId,
  318. $this._characteristicId
  319. );
  320. },
  321. // ArrayBuffer转16进度字符串示例
  322. ab2hex(buffer) {
  323. var hexArr = Array.prototype.map.call(new Uint8Array(buffer), function(
  324. bit
  325. ) {
  326. return ("00" + bit.toString(16)).slice(-2);
  327. });
  328. return hexArr.join("");
  329. },
  330. // 往后端推送一次定时数据
  331. post_data() {
  332. //判断数据长度大于理想状态 需要对数组进行切割 逐个提交数据
  333. let $len = 300;
  334. console.log($this.attList.length, "结束游戏提交的数据量");
  335. if ($this.attList.length > $len) {
  336. let $count = Math.ceil($this.attList.length / $len);
  337. let index = 0;
  338. let inter = setInterval(() => {
  339. if (index < $count) {
  340. let $params = {
  341. game_record_id: game_store.getters.getGameRecordId(),
  342. //数据段长度大于65 多个提交
  343. line: $this.attList.slice(index * $len, (index + 1) * $len),
  344. line_med: $this.medList.slice(index * $len, (index + 1) * $len),
  345. };
  346. gameAddLine($params).then((res) => {
  347. let $data = res.data;
  348. if ($data.code == 0) {
  349. console.log("ok", $params["line"]);
  350. }
  351. });
  352. }
  353. index++;
  354. if (index == $count) {
  355. clearInterval(inter);
  356. $this.game_over();
  357. $this.to_report();
  358. }
  359. }, 200);
  360. return $count * 500;
  361. } else {
  362. let $params = {
  363. game_record_id: game_store.getters.getGameRecordId(),
  364. //数据段长度小于65 单个提交
  365. line: $this.attList,
  366. line_med: $this.medList,
  367. };
  368. if (!$params.line) {
  369. return false;
  370. }
  371. gameAddLine($params).then((res) => {
  372. let $data = res.data;
  373. if ($data.code == 0) {
  374. $this.attList = [];
  375. $this.medList = [];
  376. }
  377. setTimeout(() => {
  378. $this.game_over();
  379. $this.to_report();
  380. }, 800);
  381. });
  382. return 800;
  383. }
  384. },
  385. // 结束游戏
  386. game_over() {
  387. $this.over_play_time = game_store.getters.getOverPlayTime();
  388. $this.played_time = $this.played_time >= 0 ? $this.played_time : 0;
  389. console.log(
  390. "结束游戏时游玩的时间倒计时~~played_time~~",
  391. $this.played_time,
  392. "~~play_time~~",
  393. $this.play_time,
  394. // "所选类型的时间",
  395. "~~$this.over_play_time~~",
  396. $this.over_play_time
  397. );
  398. let $params = {
  399. game_record_id: game_store.getters.getGameRecordId(),
  400. type: $this.mode,
  401. // play_time: $this.play_time - Math.abs($this.played_time),
  402. // play_time: $this.play_time - $this.played_time,
  403. play_time: $this.over_play_time - $this.played_time,
  404. };
  405. //
  406. gameEnd($params).then((res) => {
  407. let $data = res.data;
  408. $this.played_time = null;
  409. //设置未结束游戏
  410. $this.is_end = false;
  411. $this.att_list = [];
  412. $this.med_list = [];
  413. $this.delta_list = [];
  414. $this.alpha_list = [];
  415. $this.theta_list = [];
  416. $this.beta_list = [];
  417. //游戏结束重置游戏时间
  418. wx.removeStorageSync("over_play_time");
  419. game_store.setters.setMode(0);
  420. });
  421. },
  422. // 处理游戏中数据的方法
  423. do_datas: function($data) {
  424. $this.att_list.push($data["att"]);
  425. $this.med_list.push($data["med"]);
  426. $this.delta_list.push(Math.round($data["delta"] / 10000));
  427. $this.alpha_list.push(Math.round($data["alpha"] / 1000000));
  428. $this.theta_list.push(Math.round($data["theta"] / 10000));
  429. $this.beta_list.push(Math.round($data["beta"]) / 1000000);
  430. if ($this.att_list.length > 50) {
  431. $this.att_list.shift();
  432. $this.med_list.shift();
  433. $this.delta_list.shift();
  434. $this.alpha_list.shift();
  435. $this.theta_list.shift();
  436. $this.beta_list.shift();
  437. }
  438. $this.attList.push($data["att"]);
  439. $this.medList.push($data["med"]);
  440. game_store.setters.setGameAttMedDatas({
  441. attList: $this.attList,
  442. medList: $this.medList,
  443. });
  444. //判断是否隐藏 隐藏则不绘画
  445. let $hide_status = game_store.getters.getHideStatus();
  446. if (!$hide_status) {
  447. //通过专注放松度 画图
  448. let $option = util.getLineOption($this.att_list, $this.med_list);
  449. att_charts.setOption($option);
  450. //通过基本脑波发送数据
  451. let $base_option = util.getBaseOption(
  452. $this.delta_list,
  453. $this.theta_list,
  454. $this.alpha_list,
  455. $this.beta_list
  456. );
  457. med_charts.setOption($base_option);
  458. }
  459. },
  460. //计算游玩时间
  461. calThePlayedTime() {
  462. //筛选模式
  463. $this.mode = game_store.getters.getMode();
  464. console.log("计费模式里面有什么数据~~~", game_store.getters.getMode());
  465. let mode_list = [
  466. {
  467. id: 1,
  468. name: "次卡(10分钟)",
  469. },
  470. {
  471. id: 2,
  472. name: "会员时间消费",
  473. },
  474. ];
  475. //可玩时间 (剩余时长)
  476. $this.play_time = game_store.getters.getPlayTime();
  477. //已经玩了多长时间
  478. $this.played_time = game_store.getters.getPlayedTime()
  479. ? game_store.getters.getPlayedTime()
  480. : game_store.getters.getPlayTime();
  481. $this.mode_item = {};
  482. $this.mode_item = mode_list[$this.mode - 1];
  483. },
  484. },
  485. mounted() {
  486. $this._deviceId = game_store.getters.getDeviceId();
  487. $this._serviceId = game_store.getters.getServiceId();
  488. $this._characteristicId = game_store.getters.getCharacterId();
  489. // setTimeout(() => {
  490. $this.game_status = game_store.getters.getGameStatus();
  491. // }, 1000)
  492. //判断是否结束游戏
  493. if ($this.is_end == true) {
  494. $this.game_finished();
  495. } else {
  496. $this.calThePlayedTime();
  497. $this.start_game();
  498. //打开蓝牙监听
  499. bluetooth.watch_bluetooth_status($this);
  500. }
  501. },
  502. created() {
  503. $this = this;
  504. },
  505. // 加载完成后、后台切到前台或重新进入页面时触发
  506. onShow() {
  507. // 判断可游玩时间是否小于 0
  508. game_store.setters.setHideStatus(false);
  509. if ($this.played_time == 0) {
  510. $this.game_finished();
  511. } else {
  512. wx.getSystemInfoAsync({
  513. success(res) {
  514. if (res.platform != "android") {
  515. //获取 拉到后台得时间
  516. let $hide_time = game_store.getters.getHideTime();
  517. if ($hide_time) {
  518. //获取当前时间
  519. let $now = Math.round(new Date() / 1000);
  520. //获得后台到当前时间差
  521. let $residue = $now - $hide_time;
  522. //将原来的游玩时间+上 时间差
  523. let $played_time = $this.played_time * 1 - $residue * 1;
  524. //判断时间差
  525. if ($residue > $this.played_time) {
  526. console.log(
  527. "后台退出时间大于游玩剩余的时间~~",
  528. "后台返回前台的时间差",
  529. $residue,
  530. "游玩的剩余时间played_time~~",
  531. $this.played_time,
  532. "所选类型的时间 $this.play_time~~",
  533. $this.play_time
  534. );
  535. $this.game_finished();
  536. }
  537. //重新设置游玩时间
  538. game_store.setters.setPlayedTime($played_time);
  539. $this.calThePlayedTime();
  540. console.log(
  541. "一进游戏页面我能触发嘛,且我得到的数据为:",
  542. "当前时间",
  543. $now,
  544. "拉到后台时间",
  545. $hide_time,
  546. ""
  547. );
  548. }
  549. }
  550. },
  551. });
  552. }
  553. },
  554. onLoad(options) {
  555. if (options.end == 1) {
  556. $this.is_end = true;
  557. }
  558. },
  559. // 页面从前台切换到后台或者去别的页面时候触发的
  560. onHide() {
  561. game_store.setters.setHideStatus(true);
  562. game_store.setters.setHideTime(Math.round(new Date() / 1000));
  563. },
  564. // 页面卸载时候触发的生命周期
  565. onUnload() {
  566. console.log("小程序被销毁");
  567. if (!$this.is_end) {
  568. //停止传输数据
  569. bluetooth.sendEnd(
  570. $this._deviceId,
  571. $this._serviceId,
  572. $this._characteristicId
  573. );
  574. wx.offBLECharacteristicValueChange();
  575. //存储时间
  576. game_store.setters.setPlayedTime($this.played_time);
  577. //设置未结束游戏
  578. $this.is_end = false;
  579. }
  580. },
  581. };
  582. </script>
  583. <style scoped>
  584. #playing_container {
  585. width: 100%;
  586. background: linear-gradient(
  587. 0deg,
  588. rgba(40, 157, 206, 0.51),
  589. rgba(135, 145, 226, 0.26)
  590. );
  591. background-color: #46425e;
  592. padding: 5px;
  593. height: 750px;
  594. }
  595. .game_panel {
  596. width: 100%;
  597. height: 76px;
  598. background: #46425e;
  599. box-shadow: 0px 3px 4px 0px #302d43;
  600. border-radius: 11px;
  601. margin: 0px auto;
  602. }
  603. .game_panel .title {
  604. color: #9a96b7;
  605. }
  606. .game_panel .type {
  607. color: #ffffff;
  608. }
  609. .cut_brain_icon {
  610. width: 11px;
  611. height: 11px;
  612. }
  613. .device_img {
  614. width: 36px;
  615. height: 36px;
  616. }
  617. .text-default {
  618. color: #9a95b7;
  619. font-size: 10px;
  620. }
  621. /*电量显示*/
  622. .elc_power_container {
  623. width: 28px;
  624. border: #9a95b7 3px solid;
  625. border-radius: 5px;
  626. height: 22px;
  627. }
  628. .device_elc {
  629. height: 18px;
  630. }
  631. /* 节标题 */
  632. .bar {
  633. width: 100%;
  634. height: 15px;
  635. display: flex;
  636. align-items: center;
  637. justify-content: space-between;
  638. padding: 0 7px;
  639. box-sizing: border-box;
  640. margin: 11px 0;
  641. }
  642. .bar view {
  643. display: flex;
  644. align-items: center;
  645. justify-content: start;
  646. }
  647. .bar .line {
  648. width: 4px;
  649. height: 15px;
  650. background-color: #ffb400;
  651. margin-right: 7px;
  652. }
  653. .bar .title {
  654. color: #fff;
  655. font-size: 15px;
  656. }
  657. .label {
  658. color: #9a95b7;
  659. font-size: 10px;
  660. }
  661. /*点图*/
  662. .dot {
  663. width: 8px;
  664. height: 8px;
  665. border-radius: 50%;
  666. margin-right: 4px;
  667. margin-left: 15px;
  668. }
  669. .dot-orange {
  670. background: #ffb400;
  671. }
  672. .dot-pink {
  673. background: #d4327a;
  674. }
  675. .dot-blue {
  676. background: #00ccff;
  677. }
  678. .dot-green {
  679. background: #0cda2e;
  680. }
  681. .dot-yellow {
  682. background: #d1d310;
  683. }
  684. .dot-orange-yellow {
  685. background: #f8a117;
  686. }
  687. /* 图表 */
  688. #mychart-dom-multi-line {
  689. width: 100%;
  690. height: 175px;
  691. }
  692. #mychart-dom-multi-line-med {
  693. width: 100%;
  694. height: 100px;
  695. }
  696. /*弹窗模块*/
  697. .err_container {
  698. width: 260px;
  699. height: 300px;
  700. border-radius: 5px;
  701. }
  702. /*标题*/
  703. .err_container .title {
  704. font-size: 20px;
  705. font-weight: bold;
  706. color: #4c4c4c;
  707. line-height: 24px;
  708. }
  709. /*子标题*/
  710. .err_container .sub-title {
  711. font-size: 12px;
  712. font-family: Microsoft YaHei;
  713. font-weight: 400;
  714. color: #4b4b4b;
  715. line-height: 24px;
  716. }
  717. .err_img {
  718. width: 73px;
  719. height: 83px;
  720. }
  721. .chart_view_first {
  722. position: relative;
  723. bottom: 10px;
  724. }
  725. .chart_view_second {
  726. position: relative;
  727. bottom: 5px;
  728. }
  729. /*倒计时样式*/
  730. .count_down_text {
  731. font-size: 35px;
  732. font-family: PingFang SC;
  733. font-weight: 400;
  734. color: #fff;
  735. line-height: 28px;
  736. }
  737. .chart {
  738. width: 360px;
  739. height: 193px;
  740. background: #302d43;
  741. opacity: 0.6;
  742. border-radius: 10px;
  743. }
  744. </style>