diff --git a/src/apis.js b/src/apis.js index d675c01..2346adc 100644 --- a/src/apis.js +++ b/src/apis.js @@ -412,9 +412,8 @@ export const cancelOrderListAPI = async (id) => { return request("POST", "/user/order/cancelOrder", { id }); }; -export const isGamingAPI = async () => { - const result = await request("GET", "/user/isGaming"); - return result.gaming || false; +export const getUserGameState = () => { + return request("GET", "/user/state"); }; export const getCurrentGameAPI = async () => { @@ -546,3 +545,9 @@ export const getMyLikeList = (page = 1, pageSize = 10) => { `/user/score/sheet/week/shoot/rank/like/list?pageNum=${page}&pageSize=${pageSize}` ); }; + +export const getReadyAPI = (roomId) => { + return request("POST", `/user/room/ready`, { + roomId, + }); +}; diff --git a/src/components/BackToGame.vue b/src/components/BackToGame.vue index 5fe98f7..31167fb 100644 --- a/src/components/BackToGame.vue +++ b/src/components/BackToGame.vue @@ -2,12 +2,14 @@ import { ref, watch, onMounted, onBeforeUnmount } from "vue"; import { onShow } from "@dcloudio/uni-app"; -import { isGamingAPI, getCurrentGameAPI } from "@/apis"; +import { getCurrentGameAPI, getUserGameState } from "@/apis"; import { debounce } from "@/util"; import useStore from "@/store"; import { storeToRefs } from "pinia"; -const { user } = storeToRefs(useStore()); +const store = useStore(); +const { user, game } = storeToRefs(store); +const { updateGame } = store; const props = defineProps({ signin: { @@ -15,13 +17,14 @@ const props = defineProps({ default: () => {}, }, }); -const show = ref(false); const loading = ref(false); onShow(async () => { if (user.value.id) { - const isGaming = await isGamingAPI(); - show.value = isGaming; + setTimeout(async () => { + const state = await getUserGameState(); + updateGame(state.gaming, state.roomId); + }, 1000); } }); @@ -29,10 +32,10 @@ watch( () => user.value, async (value) => { if (!value.id) { - show.value = false; + updateGame(false, ""); } else { - const isGaming = await isGamingAPI(); - show.value = isGaming; + const state = await getUserGameState(); + updateGame(state.gaming, state.roomId); } } ); @@ -40,26 +43,23 @@ watch( const onClick = debounce(async () => { if (loading.value) return; try { - const isGaming = await isGamingAPI(); - show.value = isGaming; - - if (isGaming) { - loading.value = true; + loading.value = true; + if (game.value.inBattle) { await uni.$checkAudio(); const result = await getCurrentGameAPI(); - loading.value = false; - } else { - uni.showToast({ - title: "比赛已结束", - icon: "none", + } else if (game.value.roomID) { + uni.navigateTo({ + url: "/pages/battle-room?roomNumber=" + game.value.roomID, }); + } else { + updateGame(false, ""); } } finally { loading.value = false; } }); const gameOver = () => { - show.value = false; + updateGame(false, ""); }; onMounted(() => { uni.$on("game-over", gameOver); @@ -70,10 +70,19 @@ onBeforeUnmount(() => { @@ -93,17 +102,18 @@ onBeforeUnmount(() => { .back-to-game > image:first-child { position: absolute; width: 100%; + height: 100rpx; } .back-to-game > image:nth-child(2) { position: relative; width: 60px; height: 60px; } -.back-to-game > text:nth-child(3) { +.back-to-game > text { position: relative; font-size: 14px; } -.back-to-game > image:nth-child(4) { +.back-to-game > image:last-child { position: relative; width: 15px; margin-left: 5px; diff --git a/src/components/Container.vue b/src/components/Container.vue index d185cb0..ce5cd53 100644 --- a/src/components/Container.vue +++ b/src/components/Container.vue @@ -112,21 +112,8 @@ const backToGame = debounce(async () => { try { isLoading.value = true; - - // 设置请求超时 - const timeoutPromise = new Promise((_, reject) => { - setTimeout(() => reject(new Error("请求超时")), 10000); // 10秒超时 - }); - - const result = await Promise.race([getCurrentGameAPI(), timeoutPromise]); - - // 处理返回结果 - if (result && result.gameId) { - // 跳转到游戏页面 - uni.navigateTo({ - url: `/pages/battle-room?gameId=${result.gameId}`, - }); - } else { + const game = await getCurrentGameAPI(); + if (!game || !game.gameId) { uni.showToast({ title: "没有进行中的对局", icon: "none", @@ -136,10 +123,6 @@ const backToGame = debounce(async () => { showHint.value = false; } catch (error) { console.error("获取当前游戏失败:", error); - uni.showToast({ - title: error.message || "网络请求失败,请重试", - icon: "none", - }); } finally { isLoading.value = false; } diff --git a/src/components/CreateRoom.vue b/src/components/CreateRoom.vue index c9208ac..965dd46 100644 --- a/src/components/CreateRoom.vue +++ b/src/components/CreateRoom.vue @@ -1,7 +1,14 @@ @@ -142,42 +118,4 @@ const setClipboardData = () => { border: 4rpx solid #fff3; border-color: #fed847; } -/* .battle-close { - background-color: #8889; - color: #b3b3b3; -} -.battle-close > text:last-child { - font-size: 12px; -} */ -.room-info { - display: flex; - flex-direction: column; - align-items: center; - width: 100%; - padding-top: 40px; -} -.room-info > view:first-child { - font-size: 22px; - color: #fff; - margin-bottom: 20px; -} -.room-info > text { - color: #888686; - font-size: 14px; - margin: 10px 0; -} -.room-info > view:last-child { - color: #287fff; - margin: 20px 0; - font-size: 14px; -} -.copy-room-number { - width: calc(70vw - 20px); - color: #fed847; - border: 1px solid #fed847; - padding: 10px; - text-align: center; - border-radius: 10px; - margin-bottom: 20px; -} diff --git a/src/components/Signin.vue b/src/components/Signin.vue index 80f521a..394b33f 100644 --- a/src/components/Signin.vue +++ b/src/components/Signin.vue @@ -97,8 +97,10 @@ const handleLogin = async () => { const devices = await getMyDevicesAPI(); if (devices.bindings && devices.bindings.length) { updateDevice(devices.bindings[0].deviceId, devices.bindings[0].deviceName); - const data = await getDeviceBatteryAPI(); - updateOnline(data.online); + try { + const data = await getDeviceBatteryAPI(); + updateOnline(data.online); + } catch (error) {} } loading.value = false; props.onClose(); diff --git a/src/constants.js b/src/constants.js index 0f72b7a..7410841 100644 --- a/src/constants.js +++ b/src/constants.js @@ -27,15 +27,14 @@ export const MESSAGETYPES = { Calibration: 4168086625, DeviceOnline: 4168086626, DeviceOffline: 4168086627, + SomeoneIsReady: 4168086628, }; export const topThreeColors = ["#FFD947", "#D2D2D2", "#FFA515"]; export const getMessageTypeName = (id) => { for (let key in MESSAGETYPES) { - if (MESSAGETYPES[key] === id) { - return key; - } + if (MESSAGETYPES[key] === id) return key; } return null; }; diff --git a/src/pages.json b/src/pages.json index 3d50b34..4b12f02 100644 --- a/src/pages.json +++ b/src/pages.json @@ -3,6 +3,9 @@ { "path": "pages/index" }, + { + "path": "pages/friend-battle" + }, { "path": "pages/point-book" }, @@ -90,9 +93,6 @@ { "path": "pages/practise-two" }, - { - "path": "pages/friend-battle" - }, { "path": "pages/battle-room" }, diff --git a/src/pages/battle-result.vue b/src/pages/battle-result.vue index dd9ae83..5086a67 100644 --- a/src/pages/battle-result.vue +++ b/src/pages/battle-result.vue @@ -18,7 +18,18 @@ const totalPoints = ref(0); const rank = ref(0); function exit() { - uni.navigateBack(); + const battleInfo = uni.getStorageSync("last-battle"); + if (battleInfo && battleInfo.roomId) { + uni.redirectTo({ + url: `/pages/battle-room?roomNumber=${battleInfo.roomId}`, + }); + } else if (data.value.roomId) { + uni.redirectTo({ + url: `/pages/battle-room?roomNumber=${data.value.roomId}`, + }); + } else { + uni.navigateBack(); + } } onLoad(async (options) => { @@ -310,7 +321,7 @@ const checkBowData = () => { 查看成绩 - 退出 + 返回 @@ -420,6 +431,7 @@ const checkBowData = () => { border-radius: 20px; padding: 10px 0; text-align: center; + color: #000; } .op-btn > view:last-child { color: #fff; diff --git a/src/pages/battle-room.vue b/src/pages/battle-room.vue index a9cec72..0d41816 100644 --- a/src/pages/battle-room.vue +++ b/src/pages/battle-room.vue @@ -1,6 +1,6 @@ @@ -537,7 +558,7 @@ onHide(() => {}); align-items: center; justify-content: space-between; } -.battle-guide > view:last-child { +.battle-guide > button:last-child { color: #fed847; border: 1px solid #fed847; margin-right: 10px; diff --git a/src/pages/friend-battle.vue b/src/pages/friend-battle.vue index fe5fc37..ad16c58 100644 --- a/src/pages/friend-battle.vue +++ b/src/pages/friend-battle.vue @@ -1,52 +1,54 @@ diff --git a/src/pages/index.vue b/src/pages/index.vue index 96539b6..8044616 100644 --- a/src/pages/index.vue +++ b/src/pages/index.vue @@ -30,8 +30,8 @@ const { getLvlName, updateOnline, } = store; +const { user, device, rankData, online, game } = storeToRefs(store); -const { user, device, rankData, online } = storeToRefs(store); const showModal = ref(false); const showGuide = ref(false); diff --git a/src/pages/ranking.vue b/src/pages/ranking.vue index 3f7ac7f..81a8e5f 100644 --- a/src/pages/ranking.vue +++ b/src/pages/ranking.vue @@ -4,12 +4,12 @@ import { onShow } from "@dcloudio/uni-app"; import Container from "@/components/Container.vue"; import Avatar from "@/components/Avatar.vue"; import { topThreeColors } from "@/constants"; -import { isGamingAPI, getHomeData } from "@/apis"; +import { getHomeData } from "@/apis"; import { canEenter } from "@/util"; import useStore from "@/store"; import { storeToRefs } from "pinia"; const store = useStore(); -const { user, device, online } = storeToRefs(store); +const { user, device, online, game } = storeToRefs(store); const { getLvlName } = store; const defaultSeasonData = { @@ -43,8 +43,7 @@ const handleSelect = (index) => { const toMatchPage = async (gameType, teamSize) => { if (!canEenter(user.value, device.value, online.value)) return; - const isGaming = await isGamingAPI(); - if (isGaming) { + if (game.value.inBattle) { uni.$showHint(1); return; } @@ -64,14 +63,14 @@ const toRankListPage = () => { }); }; const onChangeSeason = async (seasonId, name) => { + showSeasonList.value = false; if (name !== seasonName.value) { + handleSelect(selectedIndex.value); const result = await getHomeData(seasonId); rankData.value = result; seasonName.value = name; - handleSelect(selectedIndex.value); updateData(); } - showSeasonList.value = false; }; const updateData = () => { const { userGameStats, seasonList } = rankData.value; @@ -502,10 +501,11 @@ onShow(async () => { } .ranking-data > view:first-of-type > view { width: 25%; - padding: 7px 10px; text-align: center; border-radius: 20px; font-size: 30rpx; + word-break: keep-all; + line-height: 70rpx; } .rank-item { width: calc(100% - 30px); @@ -595,13 +595,19 @@ onShow(async () => { .season-list > view { display: flex; align-items: center; - padding: 10px 20px; word-break: keep-all; + padding: 20rpx 0; +} +.season-list > view > text { + width: 140rpx; + text-align: right; } .season-list > view > image { - width: 12px; - height: 12px; - margin-left: 10px; + width: 24rpx; + height: 24rpx; + min-width: 24rpx; + min-height: 24rpx; + margin-left: 20rpx; } .my-rank-score { position: absolute !important; diff --git a/src/store.js b/src/store.js index f9a8014..2b62f0f 100644 --- a/src/store.js +++ b/src/store.js @@ -2,7 +2,7 @@ import { defineStore } from "pinia"; const defaultUser = { id: "", - nickName: "游客", + nickName: "", avatar: "../static/user-icon.png", trio: 0, // 大于1表示完成了新手引导 lvlName: "", @@ -50,6 +50,10 @@ export default defineStore("store", { ringRank: [], }, online: false, + game: { + roomID: "", + inBattle: false, + }, }), // 计算属性 @@ -103,6 +107,10 @@ export default defineStore("store", { this.config.randInfos ); }, + updateGame(inBattle = false, roomID = "") { + this.game.roomID = roomID; + this.game.inBattle = inBattle; + }, }, // 开启数据持久化 diff --git a/src/util.js b/src/util.js index 2393913..dc628c2 100644 --- a/src/util.js +++ b/src/util.js @@ -1,4 +1,4 @@ -import { isGamingAPI, getGameAPI } from "@/apis"; +import { getUserGameState, getGameAPI } from "@/apis"; export const formatTimestamp = (timestamp) => { const date = new Date(timestamp * 1000); @@ -90,8 +90,8 @@ export const wxShare = async (canvasId = "shareCanvas") => { }; export const isGameEnded = async (battleId) => { - const isGaming = await isGamingAPI(); - if (!isGaming) { + const state = await getUserGameState(); + if (!state.gaming) { const result = await getGameAPI(battleId); if (result.mode) { uni.redirectTo({ @@ -107,7 +107,7 @@ export const isGameEnded = async (battleId) => { }, 1000); } } - return !isGaming; + return !state.gaming; }; // 获取元素尺寸和位置信息