device.vue 20 KB

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