index.vue 19 KB

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