device.vue 16 KB

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