device.vue 15 KB

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