index.vue 9.7 KB

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