fix: 修复分享模式进入主线冲突的 bug

This commit is contained in:
richarjiang
2026-05-19 19:30:37 +08:00
parent d78c29000d
commit 165fef318f
2 changed files with 74 additions and 2 deletions

View File

@@ -7,6 +7,7 @@ import { ToastManager } from 'db://assets/scripts/utils/ToastManager';
import { StaminaInfo } from 'db://assets/scripts/types/ApiTypes';
import { AuthManager } from 'db://assets/scripts/utils/AuthManager';
import { AchievementTitleManager } from 'db://assets/scripts/utils/AchievementTitleManager';
import { ShareManager } from 'db://assets/scripts/utils/ShareManager';
const { ccclass, property } = _decorator;
/**
@@ -110,15 +111,23 @@ export class PageHome extends BaseView {
return;
}
// 兜底:清空可能残留的好友分享挑战状态,确保进入的是纯主线挑战
// 场景:用户从微信好友分享卡片进入挑战 → 退出回首页 → 再次点击开始游戏
// 若不清理,缓存的 PageLevel 仍会读到 ShareManager 的分享态数据
if (ShareManager.instance.isShareMode) {
console.log('[PageHome] 检测到残留的分享挑战状态,清理后再进入主线挑战');
ShareManager.instance.clearShareMode();
}
this._isAnimating = true;
this._playStaminaCostAnimation()
.then(() => {
ViewManager.instance.open('PageLevel');
ViewManager.instance.open('PageLevel', { params: { shareMode: false } });
})
.catch(err => {
console.error('[PageHome] 体力消耗动画异常:', err);
// 异常兜底:直接进入关卡
ViewManager.instance.open('PageLevel');
ViewManager.instance.open('PageLevel', { params: { shareMode: false } });
})
.finally(() => {
this._isAnimating = false;

View File

@@ -365,6 +365,19 @@ export class PageLevel extends BaseView {
*/
onViewShow(): void {
console.log('[PageLevel] onViewShow');
// PageLevel 注册时 cache: true缓存实例会被复用。
// 必须根据本次进入时携带的 params 重新派生 _isShareMode
// 否则上一场分享挑战的状态会残留到下一次主线挑战进入。
const params = this.getParams();
const desiredShareMode = params?.shareMode === true;
if (desiredShareMode !== this._isShareMode) {
console.log(`[PageLevel] 检测到模式切换 ${this._isShareMode}${desiredShareMode},重新初始化关卡会话`);
this._reinitLevelSession(desiredShareMode);
return;
}
this._refreshModeUI();
this.updateStaminaLabel();
if (!this._isShareMode) {
@@ -372,6 +385,56 @@ export class PageLevel extends BaseView {
}
}
/**
* 跨模式切换时(例如分享挑战 → 主线挑战)重置会话状态并重新加载关卡。
* 仅在 onViewShow 检测到模式发生变化时调用,避免对正常的同模式连续作答产生副作用。
*/
private _reinitLevelSession(shareMode: boolean): void {
this._isShareMode = shareMode;
// 上一场可能遗留的弹窗 / 倒计时一并清掉,避免主线模式还看到分享态弹窗
this._closePassModal();
this._closeWrongModal();
this._closeTimeoutModal();
this._closeCommonModal();
this.stopCountdown();
this._stopStaminaRecoverTimer();
// 复位分享态相关字段(无论切换到哪个模式,分享态都不应残留)
this._shareLevelIndex = 0;
this._shareSubmissions.clear();
this._isSubmittingShareResult = false;
this._hasRequestedShareUserInfo = false;
if (this._isShareMode) {
console.log('[PageLevel] 切换到分享挑战模式');
} else {
// 主线模式:从 AuthManager 拉取最新的 nextLevel
this._nextLevelData = null;
const nextLevel = AuthManager.instance.nextLevel;
if (nextLevel) {
this._currentLevelId = nextLevel.id;
this._currentLevelNumber = nextLevel.level;
console.log(`[PageLevel] 切换到主线挑战,进入关卡: 第 ${nextLevel.level} 关 (${nextLevel.id})`);
} else {
this._currentLevelId = '';
this._currentLevelNumber = 0;
console.warn('[PageLevel] 切换到主线挑战,但没有可用关卡');
}
}
this._refreshModeUI();
this.updateStaminaLabel();
if (!this._isShareMode) {
this._startStaminaRecoverTimer();
}
// 异步加载关卡资源并调用进入关卡接口,完成后启动倒计时
this._enterAndInitLevel().catch(err => {
console.error('[PageLevel] 模式切换后重新进入关卡失败:', err);
});
}
/**
* 页面隐藏时调用
*/