diff --git a/src/apis.js b/src/apis.js index 913f505..742e84d 100644 --- a/src/apis.js +++ b/src/apis.js @@ -11,23 +11,26 @@ function request(method, url, data = {}) { }, data, success: (res) => { - if (res.data.code === 0) resolve(res.data.data); - else { - if (res.data.message.indexOf("登录身份已失效") !== -1) { - uni.removeStorageSync("token"); + if (res.data) { + const { code, data, message } = res.data; + if (code === 0) resolve(data); + else { + if (message.indexOf("登录身份已失效") !== -1) { + uni.removeStorageSync("token"); + } + if (url.indexOf("/user/room") !== -1 && method === "GET") { + resolve({}); + return; + } + if (message === "ERROR_BATTLE_GAMING") { + resolve({}); + return; + } + uni.showToast({ + title: message, + icon: "none", + }); } - if (url.indexOf("/user/room") !== -1 && method === "GET") { - resolve({}); - return; - } - if (res.data.message === "ERROR_BATTLE_GAMING") { - resolve({}); - return; - } - uni.showToast({ - title: res.data.message, - icon: "none", - }); } }, fail: (err) => { @@ -277,3 +280,8 @@ export const isGamingAPI = async () => { const result = await request("GET", "/user/isGaming"); return result.gaming || false; }; + +export const getCurrentGameAPI = async () => { + const result = await request("GET", "/user/join/battle"); + return result.currentGame || {}; +}; diff --git a/src/components/CreateRoom.vue b/src/components/CreateRoom.vue index 93aa5b5..9a7a8d4 100644 --- a/src/components/CreateRoom.vue +++ b/src/components/CreateRoom.vue @@ -18,7 +18,7 @@ const roomNumber = ref(""); const createRoom = async () => { const isGaming = await isGamingAPI(); if (isGaming) { - uni.$showHint("当前正在对战中"); + uni.$showHint(1); return; } if (loading.value === true) return; diff --git a/src/constants.js b/src/constants.js index c7f4498..77fdc69 100644 --- a/src/constants.js +++ b/src/constants.js @@ -17,6 +17,7 @@ export const MESSAGETYPES = { RoomDestroy: 3617539277, SomeoneComplete: 2921416944, HalfTimeOver: 388606440, + BackToGame: 1899960424, }; export const topThreeColors = ["#FFD947", "#D2D2D2", "#FFA515"]; diff --git a/src/pages/battle-room.vue b/src/pages/battle-room.vue index b923eda..be6ed8f 100644 --- a/src/pages/battle-room.vue +++ b/src/pages/battle-room.vue @@ -54,6 +54,21 @@ const halfTimeTip = ref(false); const total = ref(90); onLoad(async (options) => { + if (options.battleId) { + const battleInfo = uni.getStorageSync(`battle-${options.battleId}`); + console.log("----battleInfo", battleInfo); + if (battleInfo) { + battleId.value = battleInfo.id; + start.value = true; + redTeam.value = battleInfo.redTeam; + blueTeam.value = battleInfo.blueTeam; + currentRound.value = battleInfo.currentRound; + bluePoints.value = battleInfo.blueScore; + redPoints.value = battleInfo.redScore; + totalRounds.value = battleInfo.maxRound; + roundResults.value = battleInfo.roundResults; + } + } if (options.roomNumber) { roomNumber.value = options.roomNumber; const result = await getRoomAPI(options.roomNumber); diff --git a/src/pages/friend-battle.vue b/src/pages/friend-battle.vue index d937152..26b1b1e 100644 --- a/src/pages/friend-battle.vue +++ b/src/pages/friend-battle.vue @@ -19,7 +19,7 @@ const roomNumber = ref(""); const enterRoom = debounce(async () => { const isGaming = await isGamingAPI(); if (isGaming) { - uni.$showHint("当前正在对战中"); + uni.$showHint(1); return; } if (!roomNumber.value) { diff --git a/src/pages/ranking.vue b/src/pages/ranking.vue index 464451e..57d5184 100644 --- a/src/pages/ranking.vue +++ b/src/pages/ranking.vue @@ -75,7 +75,7 @@ const toTeamMatchPage = async (gameType, teamSize) => { } const isGaming = await isGamingAPI(); if (isGaming) { - uni.$showHint("当前正在对战中"); + uni.$showHint(1); return; } uni.navigateTo({ @@ -92,7 +92,7 @@ const toMeleeMatchPage = async (gameType, teamSize) => { } const isGaming = await isGamingAPI(); if (isGaming) { - uni.$showHint("当前正在对战中"); + uni.$showHint(1); return; } uni.navigateTo({ diff --git a/src/websocket.js b/src/websocket.js index 6c19046..7f7ed13 100644 --- a/src/websocket.js +++ b/src/websocket.js @@ -1,3 +1,4 @@ +import { MESSAGETYPES } from "@/constants"; let socket = null; let heartbeatInterval = null; let reconnectCount = 0; @@ -32,6 +33,29 @@ function createWebSocket(token, onMessage) { uni.onSocketMessage((res) => { const data = JSON.parse(res.data); if (onMessage) onMessage(data.data.updates); + const msg = data.data.updates[0]; + if (msg && msg.constructor === MESSAGETYPES.BackToGame) { + const { battleInfo } = msg; + uni.setStorageSync(`battle-${battleInfo.id}`, battleInfo); + // 约战 + if (battleInfo.config.battleMode === 1) { + uni.navigateTo({ + url: `/pages/team-match?battleId=${battleInfo.id}`, + }); + } + // 排位 + if (battleInfo.config.battleMode === 2) { + if (battleInfo.config.mode === 1) { + uni.navigateTo({ + url: `/pages/team-match?battleId=${battleInfo.id}`, + }); + } else if (battleInfo.config.mode === 2) { + uni.navigateTo({ + url: `/pages/melee-match?battleId=${battleInfo.id}`, + }); + } + } + } }); // 错误处理