fix: 修复一系列 bug

This commit is contained in:
richarjiang
2026-05-19 22:56:31 +08:00
parent 43afe6085d
commit 2a599b0356
15 changed files with 1321 additions and 39 deletions

View File

@@ -4,6 +4,7 @@ import { ViewManager } from './scripts/core/ViewManager';
import { LevelDataManager } from './scripts/utils/LevelDataManager';
import { AuthManager } from './scripts/utils/AuthManager';
import { ShareManager } from './scripts/utils/ShareManager';
import { ShareLaunchHandler } from './scripts/utils/ShareLaunchHandler';
import { WxSDK } from './scripts/utils/WxSDK';
const { ccclass, property } = _decorator;
@@ -92,6 +93,9 @@ export class PageLoading extends Component {
this._updateStatusLabel('正在加载挑战关卡...');
const joinSuccess = await ShareManager.instance.joinShare(shareCode);
if (joinSuccess) {
// 把启动 shareCode 同步给 ShareLaunchHandler
// 避免 wx.onShow 在初始展示时拿到同一个 code 又走一遍 join。
ShareLaunchHandler.instance.markActiveShareCode(shareCode);
this._updateProgress(1);
this._updateStatusLabel('加载完成');
// 跳过首页,直接进入分享挑战关卡
@@ -134,6 +138,20 @@ export class PageLoading extends Component {
this._updateProgress(1);
this._updateStatusLabel('加载完成');
// 兜底:如果在 PageLoading 还在 preload 期间wx.onShow 已经把游戏切到了分享态
// ShareLaunchHandler 已经 joinShare 成功),这里就不应再覆盖回首页,
// 否则用户点了好友分享卡片却看到 PageHome。
if (ShareManager.instance.isShareMode) {
console.log('[PageLoading] 检测到分享态已激活,跳过首页直达 PageLevel');
ViewManager.instance.open('PageLevel', {
params: { shareMode: true },
onComplete: () => {
this.node.destroy();
},
});
return;
}
ViewManager.instance.open('PageHome', {
onComplete: () => {
this.node.destroy();

View File

@@ -2,6 +2,7 @@ import { _decorator, Component, Prefab, AudioClip } from 'cc';
import { ViewManager } from './scripts/core/ViewManager';
import { ToastManager } from './scripts/utils/ToastManager';
import { AudioManager } from './scripts/utils/AudioManager';
import { ShareLaunchHandler } from './scripts/utils/ShareLaunchHandler';
const { ccclass, property } = _decorator;
/**
@@ -115,5 +116,10 @@ export class main extends Component {
}
AudioManager.instance.init(this.buttonClickAudio, this.node);
// 注册 wx.onShow / wx.onHide
// 用户把小游戏退到后台后再点击好友分享卡片,能拿到最新的 shareCode 并直达分享挑战关卡。
// 必须在 PageLoading 跑之前注册,这样初始 launch 中的 shareCode 也会被作为种子记下。
ShareLaunchHandler.instance.init();
}
}

View File

@@ -5507,7 +5507,7 @@
"b": 0,
"a": 255
},
"_string": "还差3题获得冷场小白2级",
"_string": "还差3题,解锁新成就等级",
"_horizontalAlign": 1,
"_verticalAlign": 1,
"_actualFontSize": 40,

View File

@@ -61,7 +61,7 @@ export class PageHome extends BaseView {
/** 是否正在播放体力消耗动画 */
private _isAnimating: boolean = false;
/** 进度游标 0% 时的本地 X 坐标,使用 prefab 当前摆放位置作为起点 */
/** 进度游标 0% 时的本地 X 坐标,根据 ProgressBar Bar 子节点的左端推导出来 */
private _progressAnchorStartX: number | null = null;
/**
@@ -363,11 +363,23 @@ export class PageHome extends BaseView {
}
private _cacheProgressAnchorStartX(): void {
if (this._progressAnchorStartX !== null || !this.progressAnchor) {
if (this._progressAnchorStartX !== null || !this.titleProgressBar) {
return;
}
this._progressAnchorStartX = this.progressAnchor.position.x;
const barSprite = this.titleProgressBar.barSprite;
if (!barSprite) {
return;
}
// Bar 节点 anchor 为 (0, 0.5),其本地 position.x 即为进度条可视左端。
// ProgressBar 与 ProgressAnchor 共享同一父节点TitleLevel
// 因此把 Bar 的本地 X 按 ProgressBar 自身的位移与缩放映射到父节点空间,
// 才是真正的「0% 起点」。直接拿 progressAnchor.position.x 当起点会导致
// 气泡始终被 prefab 摆放偏移量带跑(实测偏右 ~24px
const progressBarNode = this.titleProgressBar.node;
const barLocalX = barSprite.node.position.x;
this._progressAnchorStartX = progressBarNode.position.x + barLocalX * progressBarNode.scale.x - 30;
}
private _updateProgressAnchor(progress: number): void {

View File

@@ -292,6 +292,14 @@ export class PageLevel extends BaseView {
/** 是否处于分享挑战模式 */
private _isShareMode: boolean = false;
/**
* 当前 PageLevel 实例所处分享挑战的 shareCode 缓存。
* PageLevel 注册时 cache: true复用同一个实例。
* 当用户在后台切换好友分享卡片时ShareManager.shareCode 会发生变化,
* onViewShow 通过对比这个值与最新的 ShareManager.shareCode 来判断是否需要 _reinitLevelSession。
*/
private _activeShareCode: string | null = null;
/** 体力恢复倒计时定时器 */
private _staminaTimerId: ReturnType<typeof setInterval> | null = null;
@@ -335,8 +343,10 @@ export class PageLevel extends BaseView {
this._shareSubmissions.clear();
this._isSubmittingShareResult = false;
this._hasRequestedShareUserInfo = false;
this._activeShareCode = ShareManager.instance.shareCode;
console.log('[PageLevel] 进入分享挑战模式');
} else {
this._activeShareCode = null;
// 从 AuthManager 获取首关数据(由 PageLoading → game-data 提供)
const nextLevel = AuthManager.instance.nextLevel;
if (nextLevel) {
@@ -373,12 +383,41 @@ export class PageLevel extends BaseView {
const params = this.getParams();
const desiredShareMode = params?.shareMode === true;
if (desiredShareMode !== this._isShareMode) {
console.log(`[PageLevel] 检测到模式切换 ${this._isShareMode}${desiredShareMode},重新初始化关卡会话`);
// 当前 ShareManager 中的 shareCode(可能因为后台切到新的分享卡片而变化)
const latestShareCode = ShareManager.instance.shareCode;
const modeChanged = desiredShareMode !== this._isShareMode;
// 同样是分享模式,但 ShareManager 中的 shareCode 已经换了一份题单 —— 也必须重建会话
const shareCodeChanged = desiredShareMode && latestShareCode !== this._activeShareCode;
if (modeChanged || shareCodeChanged) {
console.log(
`[PageLevel] 检测到模式/分享码切换 mode:${this._isShareMode}->${desiredShareMode} ` +
`code:${this._activeShareCode}->${latestShareCode},重新初始化关卡会话`,
);
this._reinitLevelSession(desiredShareMode);
return;
}
// 上一次离场时如果停留在「答对后通关流程」_isTransitioning=true 由 showSuccess 置位、
// 而 _applyLevelConfig 才会重置),缓存的 PageLevel 实例会保留完成态:
// - 输入格已填入正确答案
// - 提交按钮被 _isTransitioning 锁住,无法重新提交
// - 倒计时已停、谐音梗已揭示
// 此时玩家从首页再次进入会看到一个无法操作的"死局"。必须把会话推进到下一关。
// 注意:这只可能在主线模式发生 —— 分享模式下点 iconSetting / PassModal 的 home
// 都会调用 ShareManager.clearShareMode + ViewManager.replace再次进入会被
// 上面的 modeChanged / shareCodeChanged 分支拦截走 _reinitLevelSession。
if (this._isTransitioning) {
console.log('[PageLevel] 上次离场时停留在通关后状态,自动推进到下一关');
this._closePassModal();
this._closeWrongModal();
this._closeTimeoutModal();
this._closeCommonModal();
void this.goToNextLevel();
return;
}
this._refreshModeUI();
this.updateStaminaLabel();
if (!this._isShareMode) {
@@ -408,8 +447,10 @@ export class PageLevel extends BaseView {
this._hasRequestedShareUserInfo = false;
if (this._isShareMode) {
console.log('[PageLevel] 切换到分享挑战模式');
this._activeShareCode = ShareManager.instance.shareCode;
console.log(`[PageLevel] 切换到分享挑战模式 (shareCode=${this._activeShareCode})`);
} else {
this._activeShareCode = null;
// 主线模式:从 AuthManager 拉取最新的 nextLevel
this._nextLevelData = null;
const nextLevel = AuthManager.instance.nextLevel;
@@ -866,6 +907,16 @@ export class PageLevel extends BaseView {
console.log('[PageLevel] IconSetting 点击,返回主页');
AudioManager.instance.playButtonClick();
// 离开 PageLevel 时把所有挂在 Canvas 上的关卡级弹窗一起清掉。
// PassModal / WrongModal / TimeoutModal / CommonModal 都是 addChild 到 this.node.parent
// 也就是 PageLevel 的兄弟节点PageLevel 被 ViewManager 隐藏后它们并不会自动消失,
// 否则会孤儿地盖在 PageHome 上。同时清掉 PassModal 也避免再次进入时缓存实例
// 残留 _passModalNode 引用让 _swapToNextLevelImagesIfReady 误判弹窗仍在打开。
this._closePassModal();
this._closeWrongModal();
this._closeTimeoutModal();
this._closeCommonModal();
// 分享模式下栈中没有 PageHome需要清除分享状态并直接打开首页
if (this._isShareMode) {
ShareManager.instance.clearShareMode();

View File

@@ -244,7 +244,7 @@
"__id__": 1
},
"_children": [],
"_active": true,
"_active": false,
"_components": [
{
"__id__": 11
@@ -426,7 +426,7 @@
"__id__": 25
}
],
"_active": true,
"_active": false,
"_components": [
{
"__id__": 71
@@ -2106,8 +2106,8 @@
},
"_lpos": {
"__type__": "cc.Vec3",
"x": -172.179,
"y": -613.418,
"x": -142.654,
"y": 760.825,
"z": 0
},
"_lrot": {
@@ -2147,7 +2147,7 @@
},
"_contentSize": {
"__type__": "cc.Size",
"width": 495,
"width": 660,
"height": 75.6
},
"_anchorPoint": {
@@ -2186,8 +2186,8 @@
"_string": "揭晓以下谐音梗的答案吧",
"_horizontalAlign": 1,
"_verticalAlign": 1,
"_actualFontSize": 45,
"_fontSize": 45,
"_actualFontSize": 60,
"_fontSize": 60,
"_fontFamily": "Arial",
"_lineHeight": 60,
"_overflow": 0,
@@ -2241,8 +2241,6 @@
"__id__": 0
},
"fileId": "90w8HRdbBPLYFqBkhPhWfM",
"instance": null,
"targetOverrides": null,
"nestedPrefabInstanceRoots": null
},
{
@@ -2276,7 +2274,7 @@
"_lpos": {
"__type__": "cc.Vec3",
"x": -8.201,
"y": -852.319,
"y": 492.399,
"z": 0
},
"_lrot": {
@@ -2333,7 +2331,7 @@
"_lpos": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"y": -80.522,
"z": 0
},
"_lrot": {
@@ -2345,9 +2343,9 @@
},
"_lscale": {
"__type__": "cc.Vec3",
"x": 1,
"y": 1,
"z": 1
"x": 1.363,
"y": 1.363,
"z": 1.363
},
"_mobility": 0,
"_layer": 1073741824,
@@ -2494,7 +2492,7 @@
},
"_lpos": {
"__type__": "cc.Vec3",
"x": -248.28,
"x": -202.789,
"y": -12.833,
"z": 0
},
@@ -2507,9 +2505,9 @@
},
"_lscale": {
"__type__": "cc.Vec3",
"x": 0.537,
"y": 0.537,
"z": 0.767
"x": 0.678,
"y": 0.678,
"z": 0.968
},
"_mobility": 0,
"_layer": 1073741824,
@@ -2789,9 +2787,9 @@
},
"_lscale": {
"__type__": "cc.Vec3",
"x": 0.488,
"y": 0.488,
"z": 0.488
"x": 0.422,
"y": 0.422,
"z": 0.422
},
"_mobility": 0,
"_layer": 1073741824,
@@ -3839,8 +3837,6 @@
"__id__": 0
},
"fileId": "bdqvu61fVFTIU1TQqs/qES",
"instance": null,
"targetOverrides": null,
"nestedPrefabInstanceRoots": null
},
{

View File

@@ -1668,7 +1668,7 @@
"b": 0,
"a": 255
},
"_string": "还差3题获得冷场小白2级",
"_string": "还差3题,解锁新成就等级",
"_horizontalAlign": 1,
"_verticalAlign": 1,
"_actualFontSize": 40,

View File

@@ -93,7 +93,7 @@ export class PassModal extends BaseModal {
private _titleInfo: PassModalTitleInfo = {
titleText: '冷场小白1级',
nextTitleProgress: 0,
progressText: '还差3题获得冷场小白2级'
progressText: '还差3题,解锁新成就等级'
};
/** 动画起点。为 null 表示不做进度动画,直接展示终态 */
@@ -105,7 +105,7 @@ export class PassModal extends BaseModal {
/** 下一步按钮文案,为 null 时保留 prefab 默认值 */
private _nextButtonText: string | null = null;
/** 进度游标 0% 时的本地 X 坐标,使用 prefab 当前摆放位置作为起点 */
/** 进度游标 0% 时的本地 X 坐标,根据 ProgressBar Bar 子节点的左端推导出来 */
private _progressAnchorStartX: number | null = null;
setParams(params: PassModalParams): void {
@@ -400,11 +400,24 @@ export class PassModal extends BaseModal {
}
private _cacheProgressAnchorStartX(): void {
if (this._progressAnchorStartX !== null || !this.progressAnchor) {
if (this._progressAnchorStartX !== null || !this.titleProgressBar) {
return;
}
this._progressAnchorStartX = this.progressAnchor.position.x;
const barSprite = this.titleProgressBar.barSprite;
if (!barSprite) {
return;
}
// Bar 节点 anchor 为 (0, 0.5),其本地 position.x 即为进度条可视左端。
// ProgressBar 与 ProgressAnchor 共享同一父节点TitleLevel
// 因此把 Bar 的本地 X 按 ProgressBar 自身的位移与缩放映射到父节点空间,
// 才是真正的「0% 起点」。直接拿 progressAnchor.position.x 当起点会导致
// 气泡始终被 prefab 摆放偏移量带跑(实测偏右 ~24px
// -40 为视觉微调,与 PageHome 保持一致。
const progressBarNode = this.titleProgressBar.node;
const barLocalX = barSprite.node.position.x;
this._progressAnchorStartX = progressBarNode.position.x + barLocalX * progressBarNode.scale.x - 30;
}
private _resolveProgressAnchor(): void {

View File

@@ -101,7 +101,7 @@ export class AchievementTitleManager {
titleText: currentStage.titleText,
nextTitleText: nextStage.titleText,
nextTitleProgress,
progressText: `还差${remainingToNextTitle}获得${nextStage.titleText}`,
progressText: `还差${remainingToNextTitle},解锁新成就等级`,
completedLevelCount,
currentTitleStartCount: currentStage.startCount,
nextTitleRequiredCount: nextStage.startCount,

View File

@@ -0,0 +1,147 @@
import { WxSDK } from './WxSDK';
import { ShareManager } from './ShareManager';
import { AuthManager } from './AuthManager';
import { ViewManager } from '../core/ViewManager';
/**
* 分享启动监听器
*
* 微信小游戏未被杀掉、只是退到后台时,再次通过好友分享卡片打开小游戏,
* 不会重新走启动链路PageLoading 不会再跑),因此 `wx.getLaunchOptionsSync()`
* 取到的 query 可能是上一次启动的旧值。这个 handler 通过 `wx.onShow`
* 拿到最新的 query检测 shareCode 变化后:
* 1. 清掉旧的分享态
* 2. 调用 `ShareManager.joinShare(code)` 拉取新的题单
* 3. 直接打开 `PageLevel` 进入分享挑战
*
* 对应在 PageLevel 那侧通过 `onViewShow` 检测到 ShareManager.shareCode
* 变化,重新走 `_reinitLevelSession`。
*/
export class ShareLaunchHandler {
private static _instance: ShareLaunchHandler | null = null;
static get instance(): ShareLaunchHandler {
if (!this._instance) {
this._instance = new ShareLaunchHandler();
}
return this._instance;
}
/** 已经处理过的 shareCode相同则不再重复 join */
private _activeShareCode: string | null = null;
/** 是否正在处理一次 onShow 触发的 join 流程,避免并发 */
private _isHandlingShow: boolean = false;
/** 是否已经初始化 */
private _initialized: boolean = false;
private _showHandler = (res: { query?: Record<string, any> } | undefined) => {
const code = WxSDK.extractShareCodeFromQuery(res?.query);
if (!code) {
return;
}
// 同一个 shareCode 且当前已经处于该分享态,无需重复处理
if (code === this._activeShareCode && ShareManager.instance.isShareMode) {
return;
}
// 即使不是新的 code但如果 ShareManager 已经丢失了分享态(例如挑战完成被 clear
// 用户重新点同一个分享卡片仍然应当重新加入。
void this._handleShareCode(code);
};
private _hideHandler = () => {
// 目前不在 onHide 时做任何破坏性操作;保留监听只为方便后续扩展
// (比如:暂停倒计时、上报埋点)。
console.log('[ShareLaunchHandler] 小游戏切到后台');
};
/**
* 在 main.onLoad 中调用,注册 wx.onShow / wx.onHide。
* 同时把当前启动参数中的 shareCode 作为种子,避免初次冷启动时
* 因 wx.onShow 也会被调用一次而重复触发分享流程。
*/
init(): void {
if (this._initialized) {
return;
}
this._initialized = true;
if (!WxSDK.isWechat()) {
return;
}
// 冷启动时先把当前 launch 中的 shareCode 标记成已处理,
// 避免 wx.onShow 在初始展示时拿到同一个 code 又走一遍 join。
this._activeShareCode = WxSDK.getShareCodeFromLaunch();
WxSDK.onAppShow(this._showHandler);
WxSDK.onAppHide(this._hideHandler);
console.log('[ShareLaunchHandler] 已注册 onShow/onHide 监听');
}
/**
* 由 PageLoading 在初始 join 之后调用,把已处理的 shareCode 显式同步过来,
* 让 onShow 收到相同 code 时不会重复 join。
*/
markActiveShareCode(code: string | null): void {
this._activeShareCode = code;
}
/**
* 主动取消监听(一般无需调用,留作扩展)
*/
dispose(): void {
if (!this._initialized) return;
this._initialized = false;
WxSDK.offAppShow(this._showHandler);
WxSDK.offAppHide(this._hideHandler);
}
private async _handleShareCode(code: string): Promise<void> {
if (this._isHandlingShow) {
console.log('[ShareLaunchHandler] 已有 onShow 分享流程在执行,跳过', code);
return;
}
this._isHandlingShow = true;
try {
console.log('[ShareLaunchHandler] 检测到新的 shareCode准备切换:', code);
// 确保已登录initialize 内部对已有 token 做了校验,幂等可重复调用)
const loginOk = await AuthManager.instance.initialize();
if (!loginOk) {
console.warn('[ShareLaunchHandler] 登录失败,放弃 onShow 分享切换');
return;
}
// 切到新的分享前清掉旧分享态,避免 ShareManager 内残留旧题单导致 PageLevel 错位
if (ShareManager.instance.isShareMode) {
ShareManager.instance.clearShareMode();
}
const joinOk = await ShareManager.instance.joinShare(code);
if (!joinOk) {
console.warn('[ShareLaunchHandler] 加入分享失败:', code);
return;
}
// 标记当前激活的分享码
this._activeShareCode = code;
// 跳过中间页,直接打开 PageLevel 进入分享挑战。
// PageLevel 会在 onViewShow 中根据 ShareManager.shareCode 与本地缓存比对,
// 决定是否需要 `_reinitLevelSession`。
ViewManager.instance.open('PageLevel', {
params: { shareMode: true },
});
console.log('[ShareLaunchHandler] 已切换到分享挑战:', code);
} catch (err) {
console.error('[ShareLaunchHandler] 处理 shareCode 异常:', err);
} finally {
this._isHandlingShow = false;
}
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "0709f849-66c8-4e4c-aec5-044c3c414c3e",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -398,15 +398,82 @@ export class WxSDK {
try {
const options = wxApi.getLaunchOptionsSync();
if (options?.query?.shareCode) {
console.log('[WxSDK] 检测到分享码:', options.query.shareCode);
return options.query.shareCode;
const code = WxSDK.extractShareCodeFromQuery(options?.query);
if (code) {
console.log('[WxSDK] 检测到分享码:', code);
return code;
}
} catch (err) {
console.warn('[WxSDK] 获取启动参数失败:', err);
}
return null;
}
/**
* 从查询对象(来自 launch options 或 onShow 回调)中提取 shareCode
*/
static extractShareCodeFromQuery(query: Record<string, any> | null | undefined): string | null {
const code = query?.shareCode;
return typeof code === 'string' && code.length > 0 ? code : null;
}
// ==================== 前后台生命周期 ====================
/**
* 监听小游戏切到前台事件。
* 同一个回调可重复注册多次:内部用 wx.onShow请确保业务层做幂等处理或在卸载时调用 offAppShow。
* @param callback 切前台时触发,包含本次显示对应的 query / scene 等参数
*/
static onAppShow(callback: (res: { query?: Record<string, any>; scene?: number; path?: string } | undefined) => void): void {
const wxApi = WxSDK.getWx();
if (!wxApi) return;
if (typeof wxApi.onShow !== 'function') {
console.warn('[WxSDK] 当前微信版本不支持 onShow');
return;
}
wxApi.onShow(callback);
}
/**
* 取消监听小游戏切到前台事件
*/
static offAppShow(callback: (res: any) => void): void {
const wxApi = WxSDK.getWx();
if (!wxApi) return;
if (typeof wxApi.offShow === 'function') {
wxApi.offShow(callback);
}
}
/**
* 监听小游戏切到后台事件
*/
static onAppHide(callback: () => void): void {
const wxApi = WxSDK.getWx();
if (!wxApi) return;
if (typeof wxApi.onHide !== 'function') {
console.warn('[WxSDK] 当前微信版本不支持 onHide');
return;
}
wxApi.onHide(callback);
}
/**
* 取消监听小游戏切到后台事件
*/
static offAppHide(callback: () => void): void {
const wxApi = WxSDK.getWx();
if (!wxApi) return;
if (typeof wxApi.offHide === 'function') {
wxApi.offHide(callback);
}
}
}
// ==================== 隐私授权相关 ====================