device.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. <template>
  2. <div id="device_container">
  3. <view>
  4. <text class="cuIcon-titles text-primary"></text>
  5. <text class="">我的设备</text>
  6. </view>
  7. <div class="device padding">
  8. <!-- 未连接部分-->
  9. <div v-if="connect_toy == 0">
  10. <div
  11. class="connect_box"
  12. v-if="device_status == 0 && connect_show == false"
  13. >
  14. <device_unconnect @open_scan="open_scan"></device_unconnect>
  15. </div>
  16. <!-- 连接中-->
  17. <div
  18. class="connecting_box"
  19. v-if="device_status != 0 && connect_show == false"
  20. >
  21. <device_connecting :status="device_status"></device_connecting>
  22. </div>
  23. <!-- 已链接 -->
  24. <div
  25. class="connected_box"
  26. v-if="connect_show"
  27. >
  28. <device_connected
  29. @open_choose_toy="open_choose_toy"
  30. @change_brain_status="change_device_status"
  31. :device_bg="device_bg"
  32. :device_power="device_power"
  33. :rssi="rssi"
  34. ></device_connected>
  35. </div>
  36. </div>
  37. <div v-else>
  38. <!-- 玩具模块-->
  39. <!-- 玩具连接中-->
  40. <div class="connecting_toy">
  41. <toy_connecting
  42. :connect_toy="connect_toy"
  43. :deviceId="_deviceId"
  44. :toy_id="toy_action"
  45. :toy="toy_item"
  46. :device_bg="device_bg"
  47. :device_power="device_power"
  48. @open_choose_toy="open_choose_toy"
  49. @change_toy_connect_status="change_toy_connect_status"
  50. @change_status="change_device_status"
  51. @gameStart="gameStart"
  52. ></toy_connecting>
  53. </div>
  54. </div>
  55. </div>
  56. <!-- 选择玩具-->
  57. <van-popup
  58. :show="choose_toy_window.show"
  59. @close="on_close"
  60. position="bottom"
  61. round
  62. closeable
  63. safe-area-inset-bottom
  64. >
  65. <!-- 标题 -->
  66. <div class="head padding">
  67. <div>
  68. <div class="line"></div>
  69. <div class="title">选择玩具</div>
  70. </div>
  71. </div>
  72. <!-- 内容 -->
  73. <div class="padding toy_list">
  74. <van-row
  75. gutter="14"
  76. class="toy_list_content"
  77. >
  78. <van-col
  79. v-for="(toy, index) in toy_list"
  80. :key="index"
  81. class="text-center"
  82. >
  83. <div
  84. class="toy_item flex flex-direction justify-center align-center"
  85. @click="choose_toy(index)"
  86. :class="
  87. toy_action == toy.id
  88. ? 'toy_item_action_bg'
  89. : 'toy_item_normal_bg'
  90. "
  91. >
  92. <img
  93. :src="toy.img"
  94. alt=""
  95. class="toy_img"
  96. />
  97. <text class="toy_text padding-top">{{ toy.name }}</text>
  98. </div>
  99. </van-col>
  100. </van-row>
  101. </div>
  102. <!-- 结尾 -->
  103. <div class="toy_actions padding text-center">
  104. <view class="text-gray toy_action_text padding">选择你最感兴趣的项目,点击“选好了”以后将会自动连接
  105. </view>
  106. <button
  107. class="cu-btn lg cu-btn-primary text-white text-center padding"
  108. @click="choose_ok"
  109. >
  110. 选好了
  111. </button>
  112. </div>
  113. </van-popup>
  114. <van-toast id="van-toast" />
  115. <van-dialog id="van-dialog" />
  116. <!-- <van-popup-->
  117. <!-- :show="start_show"-->
  118. <!-- :closeable="false"-->
  119. <!-- position="bottom"-->
  120. <!-- custom-style="height: 100%"-->
  121. <!-- @close="onStartGameShowClose"-->
  122. <!-- >-->
  123. <!-- <gameIng v-if="game_status" @closePop="onStartGameShowClose"></gameIng>-->
  124. <!-- </van-popup>-->
  125. </div>
  126. </template>
  127. <script>
  128. //蓝牙未连接
  129. import device_unconnect from "@/components/device/unconnect";
  130. //蓝牙连接中
  131. import device_connecting from "@/components/device/connecting";
  132. //蓝牙完成链接
  133. import device_connected from "@/components/device/connected";
  134. //连接玩具
  135. import toy_connecting from "@/components/device/toy/connecting";
  136. //开始游戏的界面
  137. import gameIng from "@/pages/start/index";
  138. //获取个人信息
  139. import Toast from "../../../static/vant/toast/toast";
  140. import { game_devices } from "../../requests/game";
  141. import game_store from "@/store/game";
  142. import bluetooth from "@/utils/bluetooth";
  143. import ble_store from "../../store/bluetooth";
  144. import Dialog from "../../../static/vant/dialog/dialog";
  145. let $this;
  146. export default {
  147. name: "device",
  148. components: {
  149. device_unconnect,
  150. device_connecting,
  151. device_connected,
  152. toy_connecting,
  153. gameIng,
  154. },
  155. data() {
  156. return {
  157. rssi: 0,
  158. //设备状态 0为未连接,1:连接中,2:已连接 3:连接失败
  159. device_status: 0,
  160. // device_status: 2,
  161. connect_show: false,
  162. //设置图标的颜色
  163. device_bg: false,
  164. choose_toy_window: {
  165. show: false,
  166. // show: true,
  167. },
  168. //'水柱音箱', '喷雾恐龙(大)', '喷雾恐龙(小)', '轨道车', '碰碰车', '小车(大)', '小车(中)', '小车(小)', '飞行器(大)', '飞行器(小)', '水母灯'
  169. toy_list: [],
  170. toy_item: {},
  171. toy_action: 1,
  172. // connect_show: true,
  173. //连接玩具 0:未连接 1:连接中 2:已连接 3:连接失败 4:游戏中
  174. connect_toy: 0,
  175. code: "jellyfish1234",
  176. device: {},
  177. _deviceId: "",
  178. _device_index: false,
  179. toy_id: 0,
  180. // 电量
  181. device_power: 0,
  182. // 开始游戏模块
  183. start_show: false,
  184. game_status: 0,
  185. };
  186. },
  187. methods: {
  188. //打开 扫描二维码
  189. open_scan() {
  190. // 打开蓝牙扫描 重置游戏状态
  191. game_store.setters.setGameStatus(0);
  192. //
  193. $this.scan_to_bluetooth();
  194. },
  195. //扫描连接蓝牙
  196. scan_to_bluetooth() {
  197. console.log("在扫码时是否存在deviceId?:", this._deviceId);
  198. wx.scanCode({
  199. onlyFromCamera: true,
  200. success: (res) => {
  201. wx.vibrateShort({
  202. type: "heavy",
  203. });
  204. let $data = res;
  205. if ($data.result) {
  206. let url = decodeURIComponent($data.result);
  207. let $code = url.match(/\?ring=(.*)/)
  208. ? url.match(/\?ring=(.*)/)
  209. : url.match(/\?code=(.*)/);
  210. // 判断新的标识值
  211. $this.code = $code[1].toUpperCase();
  212. console.log("头环码", $code);
  213. //打开蓝牙设备
  214. wx.openBluetoothAdapter({
  215. success(res) {
  216. //判断已经打开连接了
  217. if (res["errMsg"] == "openBluetoothAdapter:ok") {
  218. $this.startBluetoothDevicesDiscovery();
  219. }
  220. },
  221. fail(err) {
  222. if (err["errCode"] == 10001) {
  223. Toast.fail({
  224. message: "请打开蓝牙和位置信息!",
  225. });
  226. } else {
  227. Toast.fail({
  228. message: "蓝牙连接失败",
  229. });
  230. }
  231. },
  232. });
  233. }
  234. },
  235. });
  236. },
  237. //关闭窗口的方法
  238. on_close() {
  239. $this.choose_toy_window.show = false;
  240. },
  241. //选择玩具
  242. choose_toy($index) {
  243. $this.toy_action = $this.toy_list[$index].id;
  244. },
  245. // 打开选择玩具窗口
  246. open_choose_toy() {
  247. // $this.choose_toy_window.show = false;
  248. $this.choose_toy_window.show = true;
  249. },
  250. // 选好玩具
  251. choose_ok() {
  252. $this.on_close();
  253. $this.change_toy_connect_status(1);
  254. $this._device_index = $this.toy_action - 1;
  255. //获取玩具
  256. let $toy = {};
  257. $this.toy_list.forEach(($val, $index) => {
  258. if ($val["id"] == $this.toy_action) {
  259. $this.toy_item = $toy = $val;
  260. }
  261. });
  262. $this.toy_id = $toy.id;
  263. let $hex = $toy["hex"].substr($toy["hex"].length - 2, 2);
  264. let toy_hex = "0x" + $hex;
  265. // game_store.setters.setToyHex(toy_hex)
  266. //连接玩具
  267. // 打开数据帧
  268. let $serviceId = game_store.getters.getServiceId();
  269. let $charateristic = game_store.getters.getCharacterId();
  270. bluetooth.sendConnect(
  271. toy_hex,
  272. $this._deviceId,
  273. $serviceId,
  274. $charateristic,
  275. Toast,
  276. $this
  277. );
  278. },
  279. //修改玩具连接状态
  280. change_toy_connect_status($status = 0) {
  281. $this.connect_toy = $status;
  282. if ($status == 1) {
  283. $this.connect_show = true;
  284. } else {
  285. $this.connect_show = false;
  286. }
  287. },
  288. // 修改设备连接状态
  289. change_device_status($status = 0) {
  290. $this.device_status = $status;
  291. //当蓝牙连接已断开
  292. //当脑环断开
  293. if ($status == 0) {
  294. game_store.setters.setGameStatus(0);
  295. let $deviceId = game_store.getters.getDeviceId();
  296. let $serviceId = game_store.getters.getServiceId();
  297. let $cid = game_store.getters.getCharacterId();
  298. bluetooth.shutdownSendControl($deviceId, $serviceId, $cid);
  299. // 清空链接得设备 三值
  300. game_store.setters.clearDeviceToy();
  301. $this.connect_toy = $status;
  302. $this.connect_show = false;
  303. $this._deviceId = "";
  304. wx.closeBLEConnection({
  305. deviceId: $this._deviceId,
  306. success(res) {
  307. Toast.success({
  308. message: "已成功断开",
  309. });
  310. },
  311. });
  312. wx.closeBluetoothAdapter();
  313. } else if ($status == 2) {
  314. $this.connect_show = true;
  315. }
  316. },
  317. //开始蓝牙被发现
  318. startBluetoothDevicesDiscovery() {
  319. wx.startBluetoothDevicesDiscovery({
  320. allowDuplicatesKey: true,
  321. success: (res) => {
  322. $this.onBluetoothDeviceFound();
  323. $this.change_device_status(1);
  324. },
  325. fail(err) {
  326. $this.change_device_status(3);
  327. },
  328. });
  329. },
  330. //打开蓝牙搜索
  331. onBluetoothDeviceFound() {
  332. console.log(
  333. "打开蓝牙搜索 device_status",
  334. $this.device_status,
  335. "是否有id _deviceId",
  336. $this._deviceId
  337. );
  338. try {
  339. // 5秒后判断 这5秒在搜索设备,搜索到就赋值给_deviceId
  340. setTimeout(() => {
  341. if (!$this._deviceId) {
  342. wx.stopBluetoothDevicesDiscovery();
  343. $this.change_device_status(0);
  344. Toast.fail("未连接到设备");
  345. } else if ($this.device_status == 1) {
  346. wx.stopBluetoothDevicesDiscovery();
  347. $this.change_device_status(0);
  348. Toast.fail("设备未连接到");
  349. // console.log(
  350. // "设备未连接到device_status的状态为:",
  351. // $this.device_status
  352. // );
  353. }
  354. }, 5000);
  355. // 小乌龟
  356. wx.onBluetoothDeviceFound((res) => {
  357. res.devices.forEach((device) => {
  358. if (!device.name && !device.localName) {
  359. return;
  360. }
  361. if (device.localName && device.localName != "") {
  362. device.name = device.localName;
  363. }
  364. if (device["name"].toUpperCase() == $this.code) {
  365. $this.stopBluetoothDevicesDiscovery();
  366. $this.device = device;
  367. game_store.setters.setDeviceId(device.deviceId);
  368. $this._deviceId = device.deviceId;
  369. console.log("蓝牙搜索状态 device_status", $this.device_status);
  370. $this.createBLEConnection();
  371. }
  372. });
  373. });
  374. } catch (e) {
  375. console.log("打开蓝牙error", e);
  376. }
  377. },
  378. // 停止蓝牙搜索
  379. stopBluetoothDevicesDiscovery() {
  380. console.log(
  381. "脑环被占用的情况下我能获取到吗?",
  382. $this.device_status,
  383. "是否有id _deviceId",
  384. $this._deviceId
  385. );
  386. wx.stopBluetoothDevicesDiscovery();
  387. },
  388. //连接低功耗蓝牙设备。
  389. createBLEConnection() {
  390. wx.createBLEConnection({
  391. deviceId: $this.device.deviceId,
  392. success: (res) => {
  393. console.log("成功连接");
  394. //成功连接脑环蓝牙
  395. $this.change_device_status(2);
  396. bluetooth.watch_bluetooth_status($this);
  397. bluetooth.getBLEDeviceServices($this.device.deviceId);
  398. bluetooth.watchingDevice($this);
  399. },
  400. fail(err) {
  401. console.log(err);
  402. },
  403. });
  404. },
  405. // 获取游戏设备玩具
  406. get_toy_list() {
  407. // 清空toy_list
  408. $this.toy_list = [];
  409. game_devices().then((res) => {
  410. let $data = res.data;
  411. let $toylist = $data.data;
  412. let _item = {};
  413. $toylist.forEach(($val, $index) => {
  414. _item = {
  415. id: parseInt($val["device_id"]),
  416. name: $val["name"],
  417. img: "https://img.shuimuai.com/" + $val["img"],
  418. hex: $val["bluetooth"],
  419. };
  420. $this.toy_list.push(_item);
  421. });
  422. game_store.setters.setToyList($this.toy_list);
  423. });
  424. },
  425. onStartGameShowClose() {
  426. $this.start_show = false;
  427. $this.game_status = 0;
  428. },
  429. gameStart() {
  430. $this.game_status = 1;
  431. $this.connect_toy = 4;
  432. },
  433. },
  434. mounted() {
  435. $this.get_toy_list();
  436. },
  437. created() {
  438. $this = this;
  439. },
  440. onShow() {
  441. //判断是否游戏中
  442. let $game_status = game_store.getters.getGameStatus();
  443. console.log("游戏中:" + $game_status);
  444. if ($game_status == 3) {
  445. //不在游戏状态
  446. $this.connect_toy = 0;
  447. $this.connect_show = true;
  448. bluetooth.watchingDevice($this);
  449. bluetooth.watch_bluetooth_status($this);
  450. // let $ble_status = ble_store.getters.getBluetoothLinkStatus();
  451. // if ($ble_status == false) {
  452. // //断开蓝牙连接
  453. // $this.change_device_status(0);
  454. // }
  455. // 状态为0的时候重置为0 小乌龟
  456. } else if ($game_status == 0) {
  457. $this.connect_toy = 0;
  458. $this.connect_show = false;
  459. $this.device_status = 0;
  460. bluetooth.watch_bluetooth_status($this);
  461. // console.log('我是在游戏中断开,我触发了嘛~',$this.connect_toy, $this.connect_show,$this.device_status );
  462. }
  463. },
  464. onLoad(options) {
  465. // 原有的code
  466. let $_code = wx.getStorageSync("code");
  467. if (options.q) {
  468. let url = decodeURIComponent(options.q);
  469. let $code = url.match(/\?code=(.*)/)[1];
  470. //判断新的code 和 旧的code 是否一致 不一致则重新登录
  471. console.log("1---" + $_code, "2---" + $code);
  472. if ($_code && $_code != $code) {
  473. Toast.fail("该用户已绑定邀请码");
  474. }
  475. }
  476. },
  477. };
  478. </script>
  479. <!--共有样式-->
  480. <style>
  481. .second_device_text {
  482. position: relative;
  483. bottom: 5px;
  484. }
  485. .connect_img {
  486. width: 85px;
  487. height: 80px;
  488. }
  489. /*玩具不同背景*/
  490. .toy_item_normal_bg {
  491. background-image: url("https://img.shuimuai.com/web/toy_bg.png");
  492. background-position: center;
  493. background-size: 100% 100%;
  494. }
  495. /*玩具选中背景*/
  496. .toy_item_action_bg {
  497. background-image: url("https://img.shuimuai.com/web/toy_bg_action.png");
  498. background-position: center;
  499. background-size: 100% 100%;
  500. }
  501. .ring_2 {
  502. width: 199px;
  503. height: 203px;
  504. background: rgba(93, 77, 184, 0);
  505. border: 2px solid #f7f7f7;
  506. opacity: 0.43;
  507. border-radius: 50%;
  508. }
  509. .ring_3 {
  510. width: 158px;
  511. height: 158px;
  512. background: rgba(93, 77, 184, 0);
  513. border: 3px solid #f6f6f6;
  514. opacity: 0.54;
  515. border-radius: 50%;
  516. }
  517. .dot_container {
  518. height: 100px;
  519. }
  520. .dot_wait {
  521. height: 5px;
  522. width: 80px;
  523. background-image: url("https://img.shuimuai.com/web/connect_line.png");
  524. background-position: center;
  525. background-size: 100% 100%;
  526. }
  527. .device_phone {
  528. width: 30px;
  529. height: 40px;
  530. bottom: 5px;
  531. }
  532. .device_brain {
  533. width: 40px;
  534. height: 40px;
  535. bottom: 10px;
  536. }
  537. .device_text {
  538. padding: 3px;
  539. font-size: 9px;
  540. }
  541. .moving_dot {
  542. width: 18px;
  543. height: 18px;
  544. position: relative;
  545. left: 15px;
  546. bottom: 7px;
  547. }
  548. .moving {
  549. animation: moving 2s linear infinite;
  550. }
  551. /*左右移动动画*/
  552. @keyframes moving {
  553. 0% {
  554. left: 0px;
  555. }
  556. 50% {
  557. left: 35px;
  558. }
  559. 100% {
  560. left: 0px;
  561. }
  562. }
  563. .cut_brain_icon {
  564. width: 11px;
  565. height: 11px;
  566. }
  567. .cut_text {
  568. font-size: 11px;
  569. }
  570. /*设备绿色信号灯*/
  571. .sign_green {
  572. width: 20px;
  573. height: 10px;
  574. position: relative;
  575. top: 9px;
  576. left: 0;
  577. }
  578. /*水母男孩*/
  579. .connected_boy {
  580. width: 110px;
  581. height: 110px;
  582. position: absolute;
  583. right: -60px;
  584. top: 63px;
  585. }
  586. .boy_session {
  587. background-image: url("https://img.shuimuai.com/web/boy_session.png");
  588. background-position: center;
  589. background-size: 100% 100%;
  590. width: 120px;
  591. height: 100px;
  592. position: absolute;
  593. top: 18px;
  594. right: 25px;
  595. z-index: 4;
  596. }
  597. .boy_session_text {
  598. font-size: 12px;
  599. color: #6b6b6b;
  600. }
  601. .device_electric {
  602. position: relative;
  603. width: 16px;
  604. height: 16px;
  605. top: 0px;
  606. right: 0px;
  607. z-index: 5;
  608. }
  609. /*设备连接模块*/
  610. .device_bg {
  611. width: 90px;
  612. height: 100px;
  613. background-position: center;
  614. background-size: 100% 100%;
  615. background-image: url("https://img.shuimuai.com/web/device_bg.png");
  616. }
  617. .left {
  618. line-height: 32px;
  619. }
  620. </style>
  621. <!--私有样式-->
  622. <style scoped>
  623. #device_container {
  624. position: relative;
  625. bottom: 80px;
  626. }
  627. .head .line {
  628. width: 4px;
  629. height: 14px;
  630. background-color: #5d4db8;
  631. margin-right: 7px;
  632. }
  633. .head view {
  634. display: flex;
  635. justify-self: start;
  636. align-items: center;
  637. }
  638. /*玩具列表*/
  639. .toy_list {
  640. overflow-x: scroll;
  641. }
  642. .toy_item {
  643. margin: 0px auto;
  644. /* width: 120px; */
  645. width: 140px;
  646. height: 130px;
  647. }
  648. /*玩具图片*/
  649. .toy_img {
  650. width: 65px;
  651. height: 65px;
  652. }
  653. .toy_text {
  654. font-size: 12px;
  655. }
  656. .toy_action_text {
  657. font-size: 11px;
  658. width: 100%;
  659. }
  660. /* 选择玩具 */
  661. .toy_list {
  662. width: 100%;
  663. overflow-x: auto;
  664. }
  665. .toy_list_content {
  666. width: 130%;
  667. display: inline-block;
  668. display: flex;
  669. flex-wrap: wrap;
  670. }
  671. /* padding toy_list */
  672. </style>