diff --git a/src/apis.js b/src/apis.js index 7958e00..11fce7b 100644 --- a/src/apis.js +++ b/src/apis.js @@ -125,3 +125,14 @@ export const getGameAPI = (battleId) => { id: battleId, }); }; + +export const simulShootAPI = (device_id, x, y) => { + const data = { + device_id, + }; + if (x && y) { + data.x = x; + data.y = y; + } + return request("POST", "/index/arrow", data); +}; diff --git a/src/components/Header.vue b/src/components/Header.vue index 545ebd3..de31733 100644 --- a/src/components/Header.vue +++ b/src/components/Header.vue @@ -1,10 +1,22 @@ @@ -38,4 +51,8 @@ defineProps({ .container > text { color: #fff; } +.simul { + color: #fff; + margin-left: 20px; +} diff --git a/src/pages/index.vue b/src/pages/index.vue index 0346e80..1599434 100644 --- a/src/pages/index.vue +++ b/src/pages/index.vue @@ -3,12 +3,12 @@ import { onMounted } from "vue"; import AppFooter from "@/components/AppFooter.vue"; import AppBackground from "@/components/AppBackground.vue"; import UserHeader from "@/components/UserHeader.vue"; -import { getAppConfig, getHomeData } from "@/apis"; +import { getAppConfig, getHomeData, getMyDevicesAPI } from "@/apis"; import useStore from "@/store"; import { storeToRefs } from "pinia"; const store = useStore(); -const { updateConfig, updateUser } = store; +const { updateConfig, updateUser, updateDevice } = store; // 使用storeToRefs,用于UI里显示,保持响应性 const { user } = storeToRefs(store); @@ -68,7 +68,16 @@ onMounted(async () => { console.log("全局配置:", config); updateConfig(config); const result = await getHomeData(); - if (result.user) updateUser(result.user); + if (result.user) { + updateUser(result.user); + const devices = await getMyDevicesAPI(); + if (devices.bindings.length) { + updateDevice( + devices.bindings[0].deviceId, + devices.bindings[0].deviceName + ); + } + } console.log("首页数据:", result); } catch (error) { console.error("获取配置失败:", error); diff --git a/src/store.js b/src/store.js index a698800..2b5de93 100644 --- a/src/store.js +++ b/src/store.js @@ -11,7 +11,7 @@ export default defineStore("store", { trio: 0, // 大于1表示完成了新手引导 }, device: { - id: "", + deviceId: "", deviceName: "", }, config: {}, @@ -30,7 +30,7 @@ export default defineStore("store", { this.user = user; }, updateDevice(deviceId, deviceName) { - this.device.id = deviceId; + this.device.deviceId = deviceId; this.device.deviceName = deviceName; }, updateConfig(config) { diff --git a/src/websocket.js b/src/websocket.js index bf1dd9c..5e18132 100644 --- a/src/websocket.js +++ b/src/websocket.js @@ -20,10 +20,12 @@ function createWebSocket(token, onMessage) { console.error("WebSocket 错误", err); }); - // 关闭处理 uni.onSocketClose((result) => { console.log("WebSocket 已关闭", result); - stopHeartbeat(); + socket = uni.connectSocket({ + url: `ws://120.79.241.5:8000/socket?authorization=${token}`, + success: () => console.log("websocket 连接成功"), + }); }); // 启动心跳