添加模拟射箭按钮

This commit is contained in:
kron
2025-06-08 13:55:09 +08:00
parent deff79aa7b
commit 312906fec3
5 changed files with 46 additions and 7 deletions

View File

@@ -125,3 +125,14 @@ export const getGameAPI = (battleId) => {
id: 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);
};

View File

@@ -1,10 +1,22 @@
<script setup> <script setup>
import useStore from "@/store";
import { simulShootAPI } from "@/apis";
import { storeToRefs } from "pinia";
const store = useStore();
const { device } = storeToRefs(store);
defineProps({ defineProps({
title: { title: {
type: String, type: String,
default: "", default: "",
}, },
}); });
const simulShoot = async () => {
if (device.value.deviceId) {
await simulShootAPI(device.value.deviceId);
}
};
</script> </script>
<template> <template>
@@ -13,6 +25,7 @@ defineProps({
<image src="../static/back.png" mode="widthFix" /> <image src="../static/back.png" mode="widthFix" />
</navigator> </navigator>
<text>{{ title }}</text> <text>{{ title }}</text>
<view class="simul" @click="simulShoot">S</view>
</view> </view>
</template> </template>
@@ -38,4 +51,8 @@ defineProps({
.container > text { .container > text {
color: #fff; color: #fff;
} }
.simul {
color: #fff;
margin-left: 20px;
}
</style> </style>

View File

@@ -3,12 +3,12 @@ import { onMounted } from "vue";
import AppFooter from "@/components/AppFooter.vue"; import AppFooter from "@/components/AppFooter.vue";
import AppBackground from "@/components/AppBackground.vue"; import AppBackground from "@/components/AppBackground.vue";
import UserHeader from "@/components/UserHeader.vue"; import UserHeader from "@/components/UserHeader.vue";
import { getAppConfig, getHomeData } from "@/apis"; import { getAppConfig, getHomeData, getMyDevicesAPI } from "@/apis";
import useStore from "@/store"; import useStore from "@/store";
import { storeToRefs } from "pinia"; import { storeToRefs } from "pinia";
const store = useStore(); const store = useStore();
const { updateConfig, updateUser } = store; const { updateConfig, updateUser, updateDevice } = store;
// 使用storeToRefs用于UI里显示保持响应性 // 使用storeToRefs用于UI里显示保持响应性
const { user } = storeToRefs(store); const { user } = storeToRefs(store);
@@ -68,7 +68,16 @@ onMounted(async () => {
console.log("全局配置:", config); console.log("全局配置:", config);
updateConfig(config); updateConfig(config);
const result = await getHomeData(); 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); console.log("首页数据:", result);
} catch (error) { } catch (error) {
console.error("获取配置失败:", error); console.error("获取配置失败:", error);

View File

@@ -11,7 +11,7 @@ export default defineStore("store", {
trio: 0, // 大于1表示完成了新手引导 trio: 0, // 大于1表示完成了新手引导
}, },
device: { device: {
id: "", deviceId: "",
deviceName: "", deviceName: "",
}, },
config: {}, config: {},
@@ -30,7 +30,7 @@ export default defineStore("store", {
this.user = user; this.user = user;
}, },
updateDevice(deviceId, deviceName) { updateDevice(deviceId, deviceName) {
this.device.id = deviceId; this.device.deviceId = deviceId;
this.device.deviceName = deviceName; this.device.deviceName = deviceName;
}, },
updateConfig(config) { updateConfig(config) {

View File

@@ -20,10 +20,12 @@ function createWebSocket(token, onMessage) {
console.error("WebSocket 错误", err); console.error("WebSocket 错误", err);
}); });
// 关闭处理
uni.onSocketClose((result) => { uni.onSocketClose((result) => {
console.log("WebSocket 已关闭", result); console.log("WebSocket 已关闭", result);
stopHeartbeat(); socket = uni.connectSocket({
url: `ws://120.79.241.5:8000/socket?authorization=${token}`,
success: () => console.log("websocket 连接成功"),
});
}); });
// 启动心跳 // 启动心跳