device.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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. wx.scanCode({
  198. onlyFromCamera: true,
  199. success: (res) => {
  200. wx.vibrateShort({
  201. type: "heavy",
  202. });
  203. let $data = res;
  204. if ($data.result) {
  205. let url = decodeURIComponent($data.result);
  206. let $code = url.match(/\?ring=(.*)/)
  207. ? url.match(/\?ring=(.*)/)
  208. : url.match(/\?code=(.*)/);
  209. // 判断新的标识值
  210. $this.code = $code[1].toUpperCase();
  211. console.log("头环码", $code);
  212. //打开蓝牙设备
  213. wx.openBluetoothAdapter({
  214. success(res) {
  215. //判断已经打开连接了
  216. if (res["errMsg"] == "openBluetoothAdapter:ok") {
  217. $this.startBluetoothDevicesDiscovery();
  218. }
  219. },
  220. fail(err) {
  221. if (err["errCode"] == 10001) {
  222. Toast.fail({
  223. message: "请打开蓝牙和位置信息!",
  224. });
  225. } else {
  226. Toast.fail({
  227. message: "蓝牙连接失败",
  228. });
  229. }
  230. },
  231. });
  232. }
  233. },
  234. });
  235. },
  236. //关闭窗口的方法
  237. on_close() {
  238. $this.choose_toy_window.show = false;
  239. },
  240. //选择玩具
  241. choose_toy($index) {
  242. $this.toy_action = $this.toy_list[$index].id;
  243. },
  244. // 打开选择玩具窗口
  245. open_choose_toy() {
  246. // $this.choose_toy_window.show = false;
  247. $this.choose_toy_window.show = true;
  248. },
  249. // 选好玩具
  250. choose_ok() {
  251. $this.on_close();
  252. $this.change_toy_connect_status(1);
  253. $this._device_index = $this.toy_action - 1;
  254. //获取玩具
  255. let $toy = {};
  256. $this.toy_list.forEach(($val, $index) => {
  257. if ($val["id"] == $this.toy_action) {
  258. $this.toy_item = $toy = $val;
  259. }
  260. });
  261. $this.toy_id = $toy.id;
  262. let $hex = $toy["hex"].substr($toy["hex"].length - 2, 2);
  263. let toy_hex = "0x" + $hex;
  264. // game_store.setters.setToyHex(toy_hex)
  265. //连接玩具
  266. // 打开数据帧
  267. let $serviceId = game_store.getters.getServiceId();
  268. let $charateristic = game_store.getters.getCharacterId();
  269. bluetooth.sendConnect(
  270. toy_hex,
  271. $this._deviceId,
  272. $serviceId,
  273. $charateristic,
  274. Toast,
  275. $this
  276. );
  277. },
  278. //修改玩具连接状态
  279. change_toy_connect_status($status = 0) {
  280. $this.connect_toy = $status;
  281. if ($status == 1) {
  282. $this.connect_show = true;
  283. } else {
  284. $this.connect_show = false;
  285. }
  286. },
  287. // 修改设备连接状态
  288. change_device_status($status = 0) {
  289. $this.device_status = $status;
  290. //当蓝牙连接已断开
  291. //当脑环断开
  292. if ($status == 0) {
  293. game_store.setters.setGameStatus(0);
  294. let $deviceId = game_store.getters.getDeviceId();
  295. let $serviceId = game_store.getters.getServiceId();
  296. let $cid = game_store.getters.getCharacterId();
  297. bluetooth.shutdownSendControl($deviceId, $serviceId, $cid);
  298. // 清空链接得设备 三值
  299. game_store.setters.clearDeviceToy();
  300. $this.connect_toy = $status;
  301. $this.connect_show = false;
  302. wx.closeBLEConnection({
  303. deviceId: $this._deviceId,
  304. success(res) {
  305. Toast.success({
  306. message: "已成功断开",
  307. });
  308. },
  309. });
  310. wx.closeBluetoothAdapter();
  311. } else if ($status == 2) {
  312. $this.connect_show = true;
  313. }
  314. },
  315. //开始蓝牙被发现
  316. startBluetoothDevicesDiscovery() {
  317. wx.startBluetoothDevicesDiscovery({
  318. allowDuplicatesKey: true,
  319. success: (res) => {
  320. $this.onBluetoothDeviceFound();
  321. $this.change_device_status(1);
  322. },
  323. fail(err) {
  324. $this.change_device_status(3);
  325. },
  326. });
  327. },
  328. //打开蓝牙搜索
  329. onBluetoothDeviceFound() {
  330. console.log(
  331. "打开蓝牙搜索 device_status",
  332. $this.device_status,
  333. "是否有id _deviceId",
  334. $this._deviceId
  335. );
  336. try {
  337. // 5秒后判断 这5秒在搜索设备,搜索到就赋值给_deviceId
  338. setTimeout(() => {
  339. if (!$this._deviceId) {
  340. wx.stopBluetoothDevicesDiscovery();
  341. $this.change_device_status(0);
  342. Toast.fail("未连接到设备");
  343. } else if ($this.device_status == 1) {
  344. wx.stopBluetoothDevicesDiscovery();
  345. $this.change_device_status(0);
  346. Toast.fail("设备未连接到");
  347. console.log('设备未连接到device_status的状态为:',$this.device_status)
  348. }
  349. }, 5000);
  350. wx.onBluetoothDeviceFound((res) => {
  351. res.devices.forEach((device) => {
  352. if (!device.name && !device.localName) {
  353. return;
  354. }
  355. if (device.localName && device.localName != "") {
  356. device.name = device.localName;
  357. }
  358. if (device["name"].toUpperCase() == $this.code) {
  359. $this.stopBluetoothDevicesDiscovery();
  360. $this.device = device;
  361. game_store.setters.setDeviceId(device.deviceId);
  362. $this._deviceId = device.deviceId;
  363. console.log("蓝牙搜索状态 device_status", $this.device_status);
  364. $this.createBLEConnection();
  365. }
  366. });
  367. });
  368. } catch (e) {
  369. console.log("打开蓝牙error", e);
  370. }
  371. },
  372. // 停止蓝牙搜索
  373. stopBluetoothDevicesDiscovery() {
  374. console.log(
  375. "脑环被占用的情况下我能获取到吗?",
  376. $this.device_status,
  377. "是否有id _deviceId",
  378. $this._deviceId
  379. );
  380. wx.stopBluetoothDevicesDiscovery();
  381. },
  382. //连接低功耗蓝牙设备。
  383. createBLEConnection() {
  384. wx.createBLEConnection({
  385. deviceId: $this.device.deviceId,
  386. success: (res) => {
  387. console.log("成功连接");
  388. //成功连接脑环蓝牙
  389. $this.change_device_status(2);
  390. bluetooth.watch_bluetooth_status($this);
  391. bluetooth.getBLEDeviceServices($this.device.deviceId);
  392. bluetooth.watchingDevice($this);
  393. },
  394. fail(err) {
  395. console.log(err);
  396. },
  397. });
  398. },
  399. // 获取游戏设备玩具
  400. get_toy_list() {
  401. // 清空toy_list
  402. $this.toy_list = [];
  403. game_devices().then((res) => {
  404. let $data = res.data;
  405. let $toylist = $data.data;
  406. let _item = {};
  407. $toylist.forEach(($val, $index) => {
  408. _item = {
  409. id: parseInt($val["device_id"]),
  410. name: $val["name"],
  411. img: "https://img.shuimuai.com/" + $val["img"],
  412. hex: $val["bluetooth"],
  413. };
  414. $this.toy_list.push(_item);
  415. });
  416. game_store.setters.setToyList($this.toy_list);
  417. });
  418. },
  419. onStartGameShowClose() {
  420. $this.start_show = false;
  421. $this.game_status = 0;
  422. },
  423. gameStart() {
  424. $this.game_status = 1;
  425. $this.connect_toy = 4;
  426. },
  427. },
  428. mounted() {
  429. $this.get_toy_list();
  430. },
  431. created() {
  432. $this = this;
  433. },
  434. onShow() {
  435. //判断是否游戏中
  436. let $game_status = game_store.getters.getGameStatus();
  437. console.log("游戏中:"+$game_status);
  438. if ($game_status == 3) {
  439. //不在游戏状态
  440. $this.connect_toy = 0;
  441. $this.connect_show = true;
  442. bluetooth.watchingDevice($this);
  443. bluetooth.watch_bluetooth_status($this);
  444. // let $ble_status = ble_store.getters.getBluetoothLinkStatus();
  445. // if ($ble_status == false) {
  446. // //断开蓝牙连接
  447. // $this.change_device_status(0);
  448. // }
  449. // 状态为0的时候重置为0 小乌龟
  450. }else if ($game_status == 0){
  451. $this.connect_toy = 0 ;
  452. $this.connect_show = false;
  453. $this.device_status = 0;
  454. bluetooth.watch_bluetooth_status($this);
  455. // console.log('我是在游戏中断开,我触发了嘛~',$this.connect_toy, $this.connect_show,$this.device_status );
  456. }
  457. },
  458. onLoad(options) {
  459. // 原有的code
  460. let $_code = wx.getStorageSync("code");
  461. if (options.q) {
  462. let url = decodeURIComponent(options.q);
  463. let $code = url.match(/\?code=(.*)/)[1];
  464. //判断新的code 和 旧的code 是否一致 不一致则重新登录
  465. console.log("1---" + $_code, "2---" + $code);
  466. if ($_code && $_code != $code) {
  467. Toast.fail("该用户已绑定邀请码");
  468. }
  469. }
  470. },
  471. };
  472. </script>
  473. <!--共有样式-->
  474. <style>
  475. .second_device_text {
  476. position: relative;
  477. bottom: 5px;
  478. }
  479. .connect_img {
  480. width: 85px;
  481. height: 80px;
  482. }
  483. /*玩具不同背景*/
  484. .toy_item_normal_bg {
  485. background-image: url("https://img.shuimuai.com/web/toy_bg.png");
  486. background-position: center;
  487. background-size: 100% 100%;
  488. }
  489. /*玩具选中背景*/
  490. .toy_item_action_bg {
  491. background-image: url("https://img.shuimuai.com/web/toy_bg_action.png");
  492. background-position: center;
  493. background-size: 100% 100%;
  494. }
  495. .ring_2 {
  496. width: 199px;
  497. height: 203px;
  498. background: rgba(93, 77, 184, 0);
  499. border: 2px solid #f7f7f7;
  500. opacity: 0.43;
  501. border-radius: 50%;
  502. }
  503. .ring_3 {
  504. width: 158px;
  505. height: 158px;
  506. background: rgba(93, 77, 184, 0);
  507. border: 3px solid #f6f6f6;
  508. opacity: 0.54;
  509. border-radius: 50%;
  510. }
  511. .dot_container {
  512. height: 100px;
  513. }
  514. .dot_wait {
  515. height: 5px;
  516. width: 80px;
  517. background-image: url("https://img.shuimuai.com/web/connect_line.png");
  518. background-position: center;
  519. background-size: 100% 100%;
  520. }
  521. .device_phone {
  522. width: 30px;
  523. height: 40px;
  524. bottom: 5px;
  525. }
  526. .device_brain {
  527. width: 40px;
  528. height: 40px;
  529. bottom: 10px;
  530. }
  531. .device_text {
  532. padding: 3px;
  533. font-size: 9px;
  534. }
  535. .moving_dot {
  536. width: 18px;
  537. height: 18px;
  538. position: relative;
  539. left: 15px;
  540. bottom: 7px;
  541. }
  542. .moving {
  543. animation: moving 2s linear infinite;
  544. }
  545. /*左右移动动画*/
  546. @keyframes moving {
  547. 0% {
  548. left: 0px;
  549. }
  550. 50% {
  551. left: 35px;
  552. }
  553. 100% {
  554. left: 0px;
  555. }
  556. }
  557. .cut_brain_icon {
  558. width: 11px;
  559. height: 11px;
  560. }
  561. .cut_text {
  562. font-size: 11px;
  563. }
  564. /*设备绿色信号灯*/
  565. .sign_green {
  566. width: 20px;
  567. height: 10px;
  568. position: relative;
  569. top: 9px;
  570. left: 0;
  571. }
  572. /*水母男孩*/
  573. .connected_boy {
  574. width: 110px;
  575. height: 110px;
  576. position: absolute;
  577. right: -60px;
  578. top: 63px;
  579. }
  580. .boy_session {
  581. background-image: url("https://img.shuimuai.com/web/boy_session.png");
  582. background-position: center;
  583. background-size: 100% 100%;
  584. width: 120px;
  585. height: 100px;
  586. position: absolute;
  587. top: 18px;
  588. right: 25px;
  589. z-index: 4;
  590. }
  591. .boy_session_text {
  592. font-size: 12px;
  593. color: #6b6b6b;
  594. }
  595. .device_electric {
  596. position: relative;
  597. width: 16px;
  598. height: 16px;
  599. top: 0px;
  600. right: 0px;
  601. z-index: 5;
  602. }
  603. /*设备连接模块*/
  604. .device_bg {
  605. width: 90px;
  606. height: 100px;
  607. background-position: center;
  608. background-size: 100% 100%;
  609. background-image: url("https://img.shuimuai.com/web/device_bg.png");
  610. }
  611. .left {
  612. line-height: 32px;
  613. }
  614. </style>
  615. <!--私有样式-->
  616. <style scoped>
  617. #device_container {
  618. position: relative;
  619. bottom: 80px;
  620. }
  621. .head .line {
  622. width: 4px;
  623. height: 14px;
  624. background-color: #5d4db8;
  625. margin-right: 7px;
  626. }
  627. .head view {
  628. display: flex;
  629. justify-self: start;
  630. align-items: center;
  631. }
  632. /*玩具列表*/
  633. .toy_list {
  634. overflow-x: scroll;
  635. }
  636. .toy_item {
  637. margin: 0px auto;
  638. /* width: 120px; */
  639. width: 140px;
  640. height: 130px;
  641. }
  642. /*玩具图片*/
  643. .toy_img {
  644. width: 65px;
  645. height: 65px;
  646. }
  647. .toy_text {
  648. font-size: 12px;
  649. }
  650. .toy_action_text {
  651. font-size: 11px;
  652. width: 100%;
  653. }
  654. /* 选择玩具 */
  655. .toy_list {
  656. width: 100%;
  657. overflow-x: auto;
  658. }
  659. .toy_list_content {
  660. width: 130%;
  661. display: inline-block;
  662. display: flex;
  663. flex-wrap: wrap;
  664. }
  665. /* padding toy_list */
  666. </style>