index.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <script>
  2. import ble_store from "@/store/bluetooth";
  3. import game_store from "@/store/game";
  4. import ConnectionScan from "@/components/connection/brains/scan";
  5. import Connecting from "@/components/connection/brains/connecting";
  6. import Connected from "@/components/connection/brains/connected";
  7. import ToySelected from "@/components/connection/toys/selected";
  8. import ToySelection from "@/components/connection/toys/selection";
  9. let $this;
  10. export default {
  11. name: "ConnectionIndex",
  12. components: {
  13. ConnectionScan,
  14. Connecting,
  15. Connected,
  16. ToySelected,
  17. ToySelection
  18. },
  19. data() {
  20. return {
  21. // 设备状态 0:为未连接,1:连接中,2:已连接 3:连接失败 5:取消连接
  22. device_status: 0,
  23. // 教具选择弹框
  24. popup_show: false,
  25. // 选择的教具
  26. toy_item: {
  27. id: 0,
  28. name: "",
  29. img: "",
  30. hex: "",
  31. },
  32. // $this.$connection 设置的参数
  33. // 连接教具 0:未连接 1:连接中 2:已连接 3:连接失败 4:游戏中
  34. connect_toy: 0,
  35. // 设置图标的颜色
  36. device_bg: false,
  37. // 脑机电量
  38. device_power: 0,
  39. // 教具电量
  40. toy_power: 0,
  41. toy_sn:"",
  42. }
  43. },
  44. created() {
  45. $this = this;
  46. },
  47. watch: {
  48. 'connect_toy': {
  49. handler: function($status) {
  50. console.log("监听connect_toy状态变化",$status);
  51. }, immediate: true
  52. }
  53. },
  54. methods: {
  55. /**
  56. * 获取设备扫码连接状态
  57. */
  58. getScanStatus($status) {
  59. // 扫码成功连接中1,失败未连接0
  60. $this.device_status = $status * 1;
  61. console.log("接收到设备扫码连接状态:", $status);
  62. },
  63. /**
  64. * 获取设备连接状态
  65. * @param $status 设备状态 0:为未连接,1:连接中,2:已连接 3:连接失败 5:取消连接
  66. */
  67. setDeviceStatus($status) {
  68. // 重置教具连接状态
  69. $this.connect_toy = 0;
  70. console.log("接收到设备连接状态:", $status);
  71. if ($status * 1 === 2) {
  72. $this.device_status = 2;
  73. // 已连接
  74. let $checkServices = setInterval(() => {
  75. let $serviceId = ble_store.getters.getServiceId();
  76. if ($serviceId) {
  77. clearInterval($checkServices);
  78. // 监听数据
  79. //$this.$connection.openNotify($this);
  80. $this.$connection.notifyDatas($this);
  81. $this.$connection.watchBLEstatus($this);
  82. }
  83. }, 1500);
  84. } else {
  85. if ($status * 1 > 0) {
  86. // 取消连接-关闭脑机蓝牙连接
  87. $this.$connection.closeConnection($this);
  88. }
  89. $this.device_status = 0;
  90. }
  91. },
  92. /**
  93. * 打开教具选择
  94. */
  95. openToyList(){
  96. this.popup_show = true;
  97. this.toy_sn = "教具";
  98. if ($this.device_status*1 === 2) {
  99. // 关闭脑控
  100. this.$connection.SendOrder("09");
  101. }
  102. },
  103. //关闭窗口
  104. closePopup() {
  105. $this.popup_show = false;
  106. },
  107. // 选好教具
  108. chooseOK() {
  109. $this.popup_show = false;
  110. $this.toy_item = ble_store.getters.getToyItem();
  111. // 初始化教具连接的id
  112. ble_store.setters.setCurrentToyId("00");
  113. if($this.toy_item && $this.toy_item.id > 0) {
  114. $this.setToyStatus(1);
  115. $this.$connection.sendToyConnection($this.toy_item);
  116. //2022-5-25 09:07:59 设置10秒后是否已经连接
  117. // setTimeout(() => {
  118. // if ($this.connect_toy !== 2) {
  119. // $this.setToyStatus(3);
  120. // }
  121. // }, 10000);
  122. }
  123. },
  124. /**
  125. * 修改教具连接状态 0:未连接 1:连接中 2:已连接 3:连接失败 4:游戏中
  126. * @param $status
  127. */
  128. setToyStatus($status = 0) {
  129. $this.connect_toy = $status;
  130. },
  131. gameStart() {
  132. $this.game_status = 1;
  133. $this.connect_toy = 4;
  134. },
  135. },
  136. onShow() {
  137. // 未连接蓝牙状态
  138. if($this.device_status*1 === 0){
  139. // 打开蓝牙扫描 重置游戏状态
  140. game_store.setters.setGameStatus(0);
  141. }
  142. //判断是否游戏中
  143. let $game_status = game_store.getters.getGameStatus();
  144. // 游戏过程中关闭脑机状态
  145. let $game_close_status = game_store.getters.getGameCloseStatus();
  146. if ($game_status === 3) {
  147. // getBluetoothLinkStatus
  148. let $ble_status = ble_store.getters.getBluetoothLinkStatus();
  149. if ($ble_status === false) {
  150. //断开蓝牙连接
  151. $this.setDeviceStatus(0);
  152. }
  153. //不在游戏状态
  154. $this.connect_toy = 0;
  155. //$this.connect_show = true;
  156. // 监听数据
  157. $this.$connection.notifyDatas($this);
  158. $this.$connection.watchBLEstatus($this);
  159. //$this.toy_UUID = "";
  160. // 状态为1的时候重置为1 小乌龟
  161. if ($game_close_status === 1) {
  162. // 重置默认条件
  163. $this.connect_toy = 0;
  164. //$this.connect_show = false;
  165. $this.device_status = 0;
  166. $this.$connection.watchBLEstatus($this);
  167. //$this.toy_UUID = "";
  168. // 清空链接的设备
  169. ble_store.setters.clearDeviceToy();
  170. }
  171. $this.$forceUpdate();
  172. }
  173. },
  174. };
  175. </script>
  176. <template>
  177. <div>
  178. <div id="device_container">
  179. <view>
  180. <text class="cuIcon-titles text-primary"></text>
  181. <text class="">我的设备</text>
  182. </view>
  183. <div class="device padding">
  184. <!-- 脑机模块 -->
  185. <div v-if="connect_toy === 0">
  186. <!-- 扫码连接 -->
  187. <div v-if="device_status === 0" class="connect_box">
  188. <ConnectionScan @scanStatus="getScanStatus"></ConnectionScan>
  189. </div>
  190. <template v-else>
  191. <!-- 连接中 -->
  192. <div v-if="device_status !== 2" class="connecting_box">
  193. <Connecting :status="device_status" @deviceStatus="setDeviceStatus"></Connecting>
  194. </div>
  195. <!-- 已链接 -->
  196. <div v-if="device_status === 2" class="connected_box">
  197. <Connected :device_bg="device_bg" :device_power="device_power" @deviceStatus="setDeviceStatus" @openToys="openToyList"></Connected>
  198. </div>
  199. </template>
  200. </div>
  201. <!-- 教具模块 -->
  202. <div v-else>
  203. <div class="connecting_toy">
  204. <ToySelected :connect_toy="connect_toy"
  205. :toy="toy_item"
  206. :toy_sn="toy_sn"
  207. :device_bg="device_bg"
  208. :device_power="device_power"
  209. :toy_power="toy_power"
  210. @openToys="openToyList"
  211. @deviceStatus="setDeviceStatus"
  212. @gameStart="gameStart"
  213. ></ToySelected>
  214. </div>
  215. </div>
  216. <!-- 选择教具 -->
  217. <van-popup :show="popup_show" @close="closePopup" position="bottom" round closeable safe-area-inset-bottom>
  218. <ToySelection></ToySelection>
  219. <div class="padding">
  220. <button class="cu-btn lg cu-btn-primary text-white text-center padding" @click="chooseOK">
  221. 选好了
  222. </button>
  223. </div>
  224. </van-popup>
  225. </div>
  226. </div>
  227. <van-dialog id="van-dialog"/>
  228. <van-toast id="van-toast"/>
  229. <van-notify id="van-notify"/>
  230. </div>
  231. </template>
  232. <!--共有样式-->
  233. <style>
  234. .second_device_text {
  235. position: relative;
  236. bottom: 5px;
  237. }
  238. .connect_img {
  239. width: 85px;
  240. height: 80px;
  241. }
  242. /*教具不同背景*/
  243. .toy_item_normal_bg {
  244. background-image: url("https://img.shuimuai.com/web/toy_bg.png");
  245. background-position: center;
  246. background-size: 100% 100%;
  247. }
  248. /*教具选中背景*/
  249. .toy_item_action_bg {
  250. background-image: url("https://img.shuimuai.com/web/toy_bg_action.png");
  251. background-position: center;
  252. background-size: 100% 100%;
  253. }
  254. .ring_2 {
  255. width: 199px;
  256. height: 203px;
  257. background: rgba(93, 77, 184, 0);
  258. border: 2px solid #f7f7f7;
  259. opacity: 0.43;
  260. border-radius: 50%;
  261. }
  262. .ring_3 {
  263. width: 158px;
  264. height: 158px;
  265. background: rgba(93, 77, 184, 0);
  266. border: 3px solid #f6f6f6;
  267. opacity: 0.54;
  268. border-radius: 50%;
  269. }
  270. .dot_container {
  271. height: 100px;
  272. }
  273. .dot_wait {
  274. height: 5px;
  275. width: 80px;
  276. background-image: url("https://img.shuimuai.com/web/connect_line.png");
  277. background-position: center;
  278. background-size: 100% 100%;
  279. }
  280. .device_phone {
  281. width: 30px;
  282. height: 40px;
  283. bottom: 5px;
  284. }
  285. .device_brain {
  286. width: 40px;
  287. height: 40px;
  288. bottom: 10px;
  289. }
  290. .device_text {
  291. padding: 3px;
  292. font-size: 9px;
  293. }
  294. .moving_dot {
  295. width: 18px;
  296. height: 18px;
  297. position: relative;
  298. left: 15px;
  299. bottom: 7px;
  300. }
  301. .moving {
  302. animation: moving 2s linear infinite;
  303. }
  304. /*左右移动动画*/
  305. @keyframes moving {
  306. 0% {
  307. left: 0px;
  308. }
  309. 50% {
  310. left: 35px;
  311. }
  312. 100% {
  313. left: 0px;
  314. }
  315. }
  316. .cut_brain_icon {
  317. width: 11px;
  318. height: 11px;
  319. }
  320. .cut_text {
  321. font-size: 11px;
  322. }
  323. /*设备绿色信号灯*/
  324. .sign_green {
  325. width: 20px;
  326. height: 10px;
  327. position: relative;
  328. top: 9px;
  329. left: 0;
  330. }
  331. /*水母男孩*/
  332. .connected_boy {
  333. width: 110px;
  334. height: 110px;
  335. position: absolute;
  336. right: -60px;
  337. top: 63px;
  338. }
  339. .boy_session {
  340. background-image: url("https://img.shuimuai.com/web/boy_session.png");
  341. background-position: center;
  342. background-size: 100% 100%;
  343. width: 120px;
  344. height: 100px;
  345. position: absolute;
  346. top: 18px;
  347. right: 25px;
  348. z-index: 4;
  349. }
  350. .boy_session_text {
  351. font-size: 12px;
  352. color: #6b6b6b;
  353. }
  354. .device_electric {
  355. position: relative;
  356. width: 16px;
  357. height: 16px;
  358. top: 0px;
  359. right: 0px;
  360. z-index: 5;
  361. }
  362. /*设备连接模块*/
  363. .device_bg {
  364. width: 90px;
  365. height: 100px;
  366. background-position: center;
  367. background-size: 100% 100%;
  368. background-image: url("https://img.shuimuai.com/web/device_bg.png");
  369. }
  370. .left {
  371. line-height: 32px;
  372. }
  373. </style>
  374. <!--私有样式-->
  375. <style scoped>
  376. #device_container {
  377. position: relative;
  378. bottom: 80px;
  379. }
  380. </style>