diff --git a/src/App.vue b/src/App.vue index 302d1f1..df21a35 100644 --- a/src/App.vue +++ b/src/App.vue @@ -2,11 +2,12 @@ import { watch } from "vue"; import { onShow, onHide } from "@dcloudio/uni-app"; import websocket from "@/websocket"; +import { getDeviceBatteryAPI } from "@/apis"; import useStore from "@/store"; import { storeToRefs } from "pinia"; const store = useStore(); const { user } = storeToRefs(store); -const { updateUser } = store; +const { updateUser, updateOnline } = store; watch( () => user.value.id, @@ -33,8 +34,14 @@ function emitUpdateUser(value) { updateUser(value); } +async function emitUpdateOnline() { + const data = await getDeviceBatteryAPI(); + updateOnline(data.online); +} + onShow(() => { uni.$on("update-user", emitUpdateUser); + uni.$on("update-online", emitUpdateOnline); const token = uni.getStorageSync( `${uni.getAccountInfoSync().miniProgram.envVersion}_token` ); @@ -48,6 +55,7 @@ onShow(() => { onHide(() => { uni.$off("update-user", emitUpdateUser); + uni.$off("update-online", emitUpdateOnline); websocket.closeWebSocket(); }); diff --git a/src/components/BowPower.vue b/src/components/BowPower.vue index 79cf959..bfcfecf 100644 --- a/src/components/BowPower.vue +++ b/src/components/BowPower.vue @@ -20,9 +20,9 @@ onBeforeUnmount(() => { diff --git a/src/constants.js b/src/constants.js index fbdb42c..0f72b7a 100644 --- a/src/constants.js +++ b/src/constants.js @@ -25,6 +25,8 @@ export const MESSAGETYPES = { TeamUpdate: 4168086616, InvalidShot: 4168086617, Calibration: 4168086625, + DeviceOnline: 4168086626, + DeviceOffline: 4168086627, }; export const topThreeColors = ["#FFD947", "#D2D2D2", "#FFA515"]; diff --git a/src/pages/friend-battle.vue b/src/pages/friend-battle.vue index 335ad19..bdbfc53 100644 --- a/src/pages/friend-battle.vue +++ b/src/pages/friend-battle.vue @@ -11,7 +11,7 @@ import { getRoomAPI, joinRoomAPI, isGamingAPI, getBattleDataAPI } from "@/apis"; import useStore from "@/store"; import { storeToRefs } from "pinia"; const store = useStore(); -const { user } = storeToRefs(store); +const { user, device, online } = storeToRefs(store); import { debounce } from "@/util"; const showModal = ref(false); @@ -19,7 +19,33 @@ const warnning = ref(""); const roomNumber = ref(""); const data = ref({}); +const checkBeforeEnter = async () => { + if (!device.value.deviceId) { + uni.showToast({ + title: "请先绑定设备", + icon: "none", + }); + return true; + } + if (!online.value) { + uni.showToast({ + title: "智能弓未连接", + icon: "none", + }); + return true; + } + if (!user.value.trio) { + uni.showToast({ + title: "请先完成新手试炼", + icon: "none", + }); + return true; + } + return false; +}; + const enterRoom = debounce(async () => { + if (checkBeforeEnter()) return; const isGaming = await isGamingAPI(); if (isGaming) { uni.$showHint(1); @@ -54,6 +80,7 @@ const enterRoom = debounce(async () => { } }); const onCreateRoom = async () => { + if (checkBeforeEnter()) return; const isGaming = await isGamingAPI(); if (isGaming) { uni.$showHint(1); diff --git a/src/pages/index.vue b/src/pages/index.vue index cd6e827..19d3c34 100644 --- a/src/pages/index.vue +++ b/src/pages/index.vue @@ -15,18 +15,25 @@ import { getRankListAPI, getHomeData, getMyDevicesAPI, + getDeviceBatteryAPI, } from "@/apis"; import { topThreeColors } from "@/constants"; + import useStore from "@/store"; import { storeToRefs } from "pinia"; const store = useStore(); -const { updateConfig, updateUser, updateDevice, updateRank, getLvlName } = - store; -// 使用storeToRefs,用于UI里显示,保持响应性 -const { user, device, rankData } = storeToRefs(store); +const { + updateConfig, + updateUser, + updateDevice, + updateRank, + getLvlName, + updateOnline, +} = store; + +const { user, device, rankData, online } = storeToRefs(store); const showModal = ref(false); const showGuide = ref(false); -const calibration = ref(false); const showTheUser = ref(false); const toPage = async (path) => { @@ -34,34 +41,22 @@ const toPage = async (path) => { showModal.value = true; return; } - if ( - "/pages/first-try,/pages/practise,/pages/friend-battle".indexOf(path) !== -1 - ) { + if (path === "/pages/first-try") { if (!device.value.deviceId) { return uni.showToast({ title: "请先绑定设备", icon: "none", }); } - if ( - !calibration.value && - uni.getAccountInfoSync().miniProgram.envVersion === "release" - ) { - return uni.$showHint(4); - } - if ("/pages/first-try".indexOf(path) === -1 && !user.value.trio) { + if (!online.value) { return uni.showToast({ - title: "请先完成新手试炼", + title: "智能弓未连接", icon: "none", }); } - } - if (path === "/pages/first-try") { await uni.$checkAudio(); } - uni.navigateTo({ - url: path, - }); + uni.navigateTo({ url: path }); }; const toRankListPage = () => { @@ -133,10 +128,8 @@ onMounted(async () => { const config = await getAppConfig(); updateConfig(config); console.log("全局配置:", config); -}); - -onShow(() => { - calibration.value = uni.getStorageSync("calibration"); + const data = await getDeviceBatteryAPI(); + updateOnline(data.online); }); onShareAppMessage(() => { @@ -165,18 +158,22 @@ onShareTimeline(() => { - 我的弓箭 - 连接智能弓箭 - {{ device.deviceName }} + + + 绑定我的智能弓 + 设备离线 + 设备在线 + { +const goPractise = async (type) => { + if (!device.value.deviceId) { + return uni.showToast({ + title: "请先绑定设备", + icon: "none", + }); + } + if (!online.value) { + return uni.showToast({ + title: "智能弓未连接", + icon: "none", + }); + } + if (!user.value.trio) { + return uni.showToast({ + title: "请先完成新手试炼", + icon: "none", + }); + } await uni.$checkAudio(); uni.navigateTo({ - url: "/pages/practise-one", - }); -}; - -const toPractiseTwo = async () => { - await uni.$checkAudio(); - uni.navigateTo({ - url: "/pages/practise-two", + url: `/pages/practise-${type}`, }); }; @@ -78,14 +89,14 @@ onShow(async () => { - + - + { selectedIndex.value = index; @@ -48,11 +47,11 @@ const toMatchPage = async (gameType, teamSize) => { icon: "none", }); } - if ( - !calibration.value && - uni.getAccountInfoSync().miniProgram.envVersion === "release" - ) { - return uni.$showHint(4); + if (!online.value) { + return uni.showToast({ + title: "智能弓未连接", + icon: "none", + }); } if (!user.value.trio) { return uni.showToast({ @@ -125,7 +124,6 @@ const updateData = () => { } }; onShow(async () => { - calibration.value = uni.getStorageSync("calibration"); const result = await getHomeData(); rankData.value = result; handleSelect(selectedIndex.value); diff --git a/src/websocket.js b/src/websocket.js index 315796e..e07d642 100644 --- a/src/websocket.js +++ b/src/websocket.js @@ -79,6 +79,10 @@ function createWebSocket(token, onMessage) { uni.setStorageSync("latestRank", msg.lvl); } else if (msg.constructor === MESSAGETYPES.LvlUpdate) { uni.setStorageSync("latestLvl", msg.lvl); + } else if (msg.constructor === MESSAGETYPES.DeviceOnline) { + uni.$emit("update-online"); + } else if (msg.constructor === MESSAGETYPES.DeviceOffline) { + uni.$emit("update-online"); } });