device.vue 18 KB

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