From 956e82e10c52c5c9a884ae936f71416ecca210f2 Mon Sep 17 00:00:00 2001 From: chenlimao Date: Tue, 12 May 2026 16:01:29 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=20onBeforeUnmount=20=E8=A1=A5?= =?UTF-8?q?=E5=85=85=20uni.$off("socket-inbox",=20onReceiveMessage)&ToSome?= =?UTF-8?q?oneShoot=20=E6=B6=88=E6=81=AF=E5=8E=BB=E9=87=8D=E9=98=B2?= =?UTF-8?q?=E6=8A=A4=EF=BC=88lastToSomeoneShootKey=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/HeaderProgress.vue | 3 ++- src/pages/team-battle.vue | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/components/HeaderProgress.vue b/src/components/HeaderProgress.vue index bad1920..887afc9 100644 --- a/src/components/HeaderProgress.vue +++ b/src/components/HeaderProgress.vue @@ -52,7 +52,8 @@ async function onReceiveMessage(message) { const { type, mode, current, shootData } = message; if (type === MESSAGETYPESV2.BattleStart) { melee.value = Boolean(mode > 3); - totalShot.value = mode === 1 ? 3 : 2; + // 优先使用后端返回的 shootNumber,降级则根据 mode 推算 + totalShot.value = message.shootNumber ?? (mode === 1 ? 3 : 2); currentRoundEnded.value = true; audioManager.play("比赛开始"); } else if (type === MESSAGETYPESV2.BattleEnd) { diff --git a/src/pages/team-battle.vue b/src/pages/team-battle.vue index 3f5e7ea..6c3a3ff 100644 --- a/src/pages/team-battle.vue +++ b/src/pages/team-battle.vue @@ -42,6 +42,8 @@ const matchStatus = ref(undefined); const updateRemainSecond = ref(0); /** 对战来源类型(1=好友约战,2=匹配对战),用于结算页分流 */ const battleWay = ref(0); +/** 上一次处理的 ToSomeoneShoot 关键字段,用于去重过滤重复消息 */ +const lastToSomeoneShootKey = ref(""); const recoverData = (battleInfo, {force = false, arrowOnly = false} = {}) => { try { @@ -183,6 +185,11 @@ async function onReceiveMessage(msg) { if (msg.type === MESSAGETYPESV2.BattleStart) { start.value = true; } else if (msg.type === MESSAGETYPESV2.ToSomeoneShoot) { + // 去重防护:如果 playerId + round 与上一次处理完全相同,则忽略重复推送的消息 + // (防止 onBeforeUnmount 未及时 off 和页面重创建导致同一事件被处理多次) + const key = `${msg?.current?.playerId}-${msg?.current?.round}`; + if (key === lastToSomeoneShootKey.value) return; + lastToSomeoneShootKey.value = key; recoverData(msg); } else if (msg.type === MESSAGETYPESV2.ShootResult) { uni.$emit("update-remain", {stop: true}) @@ -228,6 +235,8 @@ onBeforeUnmount(() => { uni.setKeepScreenOn({ keepScreenOn: false, }); + // 注销所有第三方事件盓听,防止页面重创建时监听叠加导致消息重复处理 + uni.$off("socket-inbox", onReceiveMessage); uni.$off("audioEnded", onAudioEnded); audioManager.stopAll(); }); @@ -242,6 +251,10 @@ onShow(async () => { url: `/pages/friend-battle-result?battleId=${result.matchId}`, }); } else { + // shootNumber 来自后端,恢复状态时同步 totalShot,防止 BattleStart 不重新推送导致显示错误 + if (result.shootNumber) { + uni.$emit("update-shot", { currentShot: 0, totalShot: result.shootNumber }); + } recoverData(result, {force: true}); } }