device.vue 17 KB

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