index.vue 9.5 KB

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