device.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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="status == 0">
  17. <device_unconnect @open_scan="open_scan"></device_unconnect>
  18. </div>
  19. <!-- 连接中-->
  20. <div class="connecting_box" v-if="status != 0 && connect_show == false">
  21. <device_connecting :status="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. 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. 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. wx.scanCode({
  162. onlyFromCamera: true,
  163. success: (res) => {
  164. let $data = res;
  165. if ($data.result) {
  166. let url = decodeURIComponent($data.result);
  167. let $code = url.match(/\?code=(.*)/)[1];
  168. // 判断新的标识值
  169. if (!$code) {
  170. $code = url.match(/\?ring=(.*)/)[1];
  171. }
  172. $this.code = $code;
  173. //打开蓝牙设备
  174. wx.openBluetoothAdapter({
  175. success(res) {
  176. //判断已经打开连接了
  177. if (res["errMsg"] == "openBluetoothAdapter:ok") {
  178. console.log("openBluetoothAdapter success", res);
  179. $this.startBluetoothDevicesDiscovery();
  180. }
  181. },
  182. fail(err) {
  183. if (err["errCode"] == 10001) {
  184. Toast.fail({
  185. message: "请打开手机蓝牙,并启动脑环!"
  186. })
  187. } else {
  188. Toast.fail({
  189. message: "蓝牙连接失败",
  190. });
  191. }
  192. },
  193. });
  194. }
  195. },
  196. });
  197. },
  198. //关闭窗口的方法
  199. on_close() {
  200. $this.choose_toy_window.show = false;
  201. },
  202. //选择玩具
  203. choose_toy($index) {
  204. $this.toy_action = $this.toy_list[$index].id;
  205. },
  206. // 打开选择玩具窗口
  207. open_choose_toy() {
  208. // $this.choose_toy_window.show = false;
  209. $this.choose_toy_window.show = true;
  210. },
  211. // 选好玩具
  212. choose_ok() {
  213. $this.on_close();
  214. $this.change_toy_connect_status(1);
  215. $this._device_index = $this.toy_action - 1;
  216. //获取玩具
  217. let $toy = $this.toy_list[$this.toy_action - 1];
  218. $this.toy_id = $toy.id
  219. let $hex = $toy["hex"].substr($toy["hex"].length - 2, 2);
  220. let toy_hex = "0x" + $hex;
  221. game_store.commit("setToyHex", toy_hex);
  222. //连接玩具
  223. bluetooth.getBLEDeviceServices($this._deviceId)
  224. setTimeout(() => {
  225. console.log($this._deviceId, bluetooth.serviceId, bluetooth.cid)
  226. bluetooth.sendConnect(toy_hex, $this._deviceId, bluetooth.serviceId, bluetooth.cid, Toast)
  227. setTimeout(() => {
  228. bluetooth.watching_toy($this, $this._deviceId, bluetooth.serviceId, bluetooth.cid, Toast)
  229. }, 800)
  230. }, 1000)
  231. },
  232. //修改玩具连接状态
  233. change_toy_connect_status($status = 0) {
  234. $this.connect_toy = $status;
  235. if ($status == 1) {
  236. $this.connect_show = true
  237. } else {
  238. $this.connect_show = false
  239. }
  240. },
  241. // 修改设备连接状态
  242. change_device_status($status = 0) {
  243. $this.status = $status;
  244. //当蓝牙连接已断开
  245. if ($status == 0) {
  246. $this.connect_toy = $status;
  247. $this.connect_show = false;
  248. wx.closeBLEConnection({
  249. deviceId: $this._deviceId,
  250. success(res) {
  251. Toast.success({
  252. message: "已成功断开"
  253. });
  254. },
  255. });
  256. }
  257. },
  258. //开始蓝牙被发现
  259. startBluetoothDevicesDiscovery() {
  260. wx.startBluetoothDevicesDiscovery({
  261. allowDuplicatesKey: true,
  262. success: (res) => {
  263. console.log("startBluetoothDevicesDiscovery success", res);
  264. $this.onBluetoothDeviceFound();
  265. $this.change_device_status(1);
  266. },
  267. fail(err) {
  268. $this.change_device_status(3);
  269. },
  270. });
  271. },
  272. //打开蓝牙搜索
  273. onBluetoothDeviceFound() {
  274. wx.onBluetoothDeviceFound((res) => {
  275. res.devices.forEach((device) => {
  276. if (!device.name && !device.localName) {
  277. return;
  278. }
  279. if (device.localName && (device.localName != '')) {
  280. device.name = device.localName
  281. }
  282. console.log(device["name"]);
  283. if (device["name"] == $this.code) {
  284. $this.stopBluetoothDevicesDiscovery();
  285. $this.device = device;
  286. game_store.commit("setDeviceId", device.deviceId);
  287. $this._deviceId = device.deviceId;
  288. $this.createBLEConnection();
  289. }
  290. });
  291. });
  292. setTimeout(() => {
  293. if (!$this.device.deviceId) {
  294. $this.stopBluetoothDevicesDiscovery();
  295. $this.status = 0;
  296. $this.connect_show = false;
  297. Toast.fail("未连接到设备");
  298. }
  299. }, 7000);
  300. },
  301. // 停止蓝牙搜索
  302. stopBluetoothDevicesDiscovery() {
  303. wx.stopBluetoothDevicesDiscovery();
  304. },
  305. //连接低功耗蓝牙设备。
  306. createBLEConnection() {
  307. wx.createBLEConnection({
  308. deviceId: $this.device.deviceId,
  309. success: (res) => {
  310. console.log("成功连接");
  311. //成功连接脑环蓝牙
  312. $this.change_device_status(2);
  313. bluetooth.watch_bluetooth_status($this)
  314. // $this.getBLEDeviceServices($this.device.deviceId)
  315. },
  316. fail(err) {
  317. console.log("err", err);
  318. },
  319. });
  320. },
  321. // 获取游戏设备玩具
  322. get_toy_list() {
  323. // 清空toy_list
  324. $this.toy_list = [];
  325. // device_id: "1"
  326. // name: "音响"
  327. // img: "penquan.png"
  328. // bluetooth: "aadd0a0001"
  329. game_devices().then((res) => {
  330. let $data = res.data;
  331. let $toylist = $data.data;
  332. let _item = {};
  333. $toylist.forEach(($val, $index) => {
  334. _item = {
  335. id: parseInt($val["device_id"]),
  336. name: $val["name"],
  337. img: "https://img.shuimuai.com/" + $val["img"],
  338. hex: $val["bluetooth"],
  339. };
  340. $this.toy_list.push(_item);
  341. });
  342. game_store.commit("setToyList", $this.toy_list);
  343. });
  344. },
  345. // 开始游戏的方法
  346. game_start() {
  347. game_store.commit("setToyIndex", $this.toy_action - 1);
  348. console.log("device.vue", $this._deviceId);
  349. },
  350. },
  351. mounted() {
  352. $this.get_toy_list();
  353. },
  354. created() {
  355. $this = this;
  356. },
  357. onShow() {
  358. //判断是否游戏中
  359. let $game_status = game_store.getters.getGameStatus;
  360. if ($game_status == 1) {
  361. Dialog.confirm({
  362. title: '系统提示',
  363. message: '确认要退出游戏了吗?',
  364. }).then(() => {
  365. // on confirm
  366. mpvue.navigateTo({
  367. url: "/pages/start/main?end=1",
  368. });
  369. }).catch(() => {
  370. // on cancel
  371. mpvue.navigateTo({
  372. url: "/pages/start/main",
  373. });
  374. });
  375. $this.change_toy_connect_status(4)
  376. } else if ($game_status == 3) {
  377. //不在游戏状态
  378. $this.change_toy_connect_status(0)
  379. $this.connect_show = true
  380. bluetooth.watch_bluetooth_status($this)
  381. }
  382. },
  383. onLoad(options) {
  384. // 原有的code
  385. let $_code = wx.getStorageSync("code");
  386. if (options.q) {
  387. let url = decodeURIComponent(options.q);
  388. let $code = url.match(/\?code=(.*)/)[1];
  389. //判断新的code 和 旧的code 是否一致 不一致则重新登录
  390. console.log("1---" + $_code, "2---" + $code);
  391. if ($_code && $_code != $code) {
  392. Toast.fail("该用户已绑定邀请码");
  393. }
  394. }
  395. },
  396. };
  397. </script>
  398. <!--共有样式-->
  399. <style>
  400. .connect_img {
  401. width: 85px;
  402. height: 80px;
  403. }
  404. /*玩具不同背景*/
  405. .toy_item_normal_bg {
  406. background-image: url("https://img.shuimuai.com/web/toy_bg.png");
  407. background-position: center;
  408. background-size: 100% 100%;
  409. }
  410. /*玩具选中背景*/
  411. .toy_item_action_bg {
  412. background-image: url("https://img.shuimuai.com/web/toy_bg_action.png");
  413. background-position: center;
  414. background-size: 100% 100%;
  415. }
  416. .ring_2 {
  417. width: 199px;
  418. height: 203px;
  419. background: rgba(93, 77, 184, 0);
  420. border: 2px solid #f7f7f7;
  421. opacity: 0.43;
  422. border-radius: 50%;
  423. }
  424. .ring_3 {
  425. width: 158px;
  426. height: 158px;
  427. background: rgba(93, 77, 184, 0);
  428. border: 3px solid #f6f6f6;
  429. opacity: 0.54;
  430. border-radius: 50%;
  431. }
  432. .dot_container {
  433. height: 100px;
  434. }
  435. .dot_wait {
  436. height: 5px;
  437. width: 80px;
  438. background-image: url("https://img.shuimuai.com/web/connect_line.png");
  439. background-position: center;
  440. background-size: 100% 100%;
  441. }
  442. .device_phone {
  443. width: 30px;
  444. height: 40px;
  445. }
  446. .device_brain {
  447. width: 40px;
  448. height: 40px;
  449. }
  450. .device_text {
  451. padding: 3px;
  452. font-size: 10px;
  453. }
  454. .moving_dot {
  455. width: 18px;
  456. height: 18px;
  457. position: relative;
  458. left: 15px;
  459. bottom: 7px;
  460. }
  461. .moving {
  462. animation: moving 2s linear infinite;
  463. }
  464. /*左右移动动画*/
  465. @keyframes moving {
  466. 0% {
  467. left: 0px;
  468. }
  469. 50% {
  470. left: 35px;
  471. }
  472. 100% {
  473. left: 0px;
  474. }
  475. }
  476. .cut_brain_icon {
  477. width: 11px;
  478. height: 11px;
  479. }
  480. .cut_text {
  481. font-size: 12px;
  482. }
  483. /*设备绿色信号灯*/
  484. .sign_green {
  485. width: 20px;
  486. height: 10px;
  487. position: relative;
  488. top: 18px;
  489. left: 1px;
  490. }
  491. /*水母男孩*/
  492. .connected_boy {
  493. width: 110px;
  494. height: 110px;
  495. position: absolute;
  496. right: -60px;
  497. top: 63px;
  498. }
  499. .boy_session {
  500. background-image: url("https://img.shuimuai.com/web/boy_session.png");
  501. background-position: center;
  502. background-size: 100% 100%;
  503. width: 120px;
  504. height: 100px;
  505. position: absolute;
  506. top: 18px;
  507. right: 25px;
  508. z-index: 4;
  509. }
  510. .boy_session_text {
  511. font-size: 12px;
  512. color: #6b6b6b;
  513. }
  514. .device_electric {
  515. position: relative;
  516. width: 16px;
  517. height: 16px;
  518. top: 0px;
  519. right: 0px;
  520. z-index: 5;
  521. }
  522. /*玩具模块*/
  523. .uav_toy {
  524. width: 40px;
  525. height: 40px;
  526. }
  527. /*设备连接模块*/
  528. .device_bg {
  529. width: 90px;
  530. height: 100px;
  531. background-image: url("https://img.shuimuai.com/web/device_bg.png");
  532. background-position: center;
  533. background-size: 100% 100%;
  534. }
  535. .left {
  536. line-height: 35px;
  537. }
  538. </style>
  539. <!--私有样式-->
  540. <style scoped>
  541. #device_container {
  542. position: relative;
  543. top: -65px;
  544. }
  545. .head .line {
  546. width: 4px;
  547. height: 14px;
  548. background-color: #5d4db8;
  549. margin-right: 7px;
  550. }
  551. .head view {
  552. display: flex;
  553. justify-self: start;
  554. align-items: center;
  555. }
  556. /*玩具列表*/
  557. .toy_list {
  558. overflow-x: scroll;
  559. }
  560. .toy_item {
  561. width: 120px;
  562. height: 130px;
  563. }
  564. /*玩具图片*/
  565. .toy_img {
  566. width: 65px;
  567. height: 65px;
  568. }
  569. .toy_text {
  570. font-size: 12px;
  571. }
  572. .toy_action_text {
  573. font-size: 11px;
  574. width: 100%;
  575. }
  576. /* 选择玩具 */
  577. .toy_list {
  578. width: 100%;
  579. overflow-x: auto;
  580. }
  581. .toy_list_content {
  582. width: 120%;
  583. display: inline-block;
  584. display: flex;
  585. flex-wrap: wrap;
  586. }
  587. </style>