fix: onBeforeUnmount 补充 uni.$off("socket-inbox", onReceiveMessage)&ToSomeoneShoot 消息去重防护(lastToSomeoneShootKey)

This commit is contained in:
2026-05-12 16:01:29 +08:00
parent cf8d6135ff
commit 956e82e10c
2 changed files with 15 additions and 1 deletions

View File

@@ -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});
}
}