feat: 进入关卡时 toast 提示体力消耗,修复 StorageManager 接口位置和 WxSDK 访问级别

- 进入关卡成功后显示 toast 提示消耗体力及剩余体力
- 将 StorageManager 中 UserInfo 接口移至模块顶层,修复嵌套接口语法问题
- WxSDK.getWx() 改为 static 公开方法,便于外部调用
This commit is contained in:
richarjiang
2026-04-10 10:10:19 +08:00
parent 447e7a944a
commit 69c0986996
16 changed files with 3523 additions and 503 deletions

View File

@@ -8,27 +8,38 @@ export const API_BASE = 'https://ilookai.cn/api/v1';
/** API 端点 */
export const API_ENDPOINTS = {
/** 微信登录 */
WX_LOGIN: `${API_BASE}/auth/wx-login`,
USER_ASSETS: `${API_BASE}/user/assets`,
USER_ASSETS_CONSUME: `${API_BASE}/user/assets/consume`,
USER_ASSETS_EARN: `${API_BASE}/user/assets/earn`,
/** 用户资料(含实时体力) */
USER_PROFILE: `${API_BASE}/user/profile`,
/** 游戏数据(体力 + 通关进度) */
USER_GAME_DATA: `${API_BASE}/user/game-data`,
LEVELS: `${API_BASE}/wechat-game/levels`,
/** 关卡列表 */
LEVELS: `${API_BASE}/levels`,
/** 游戏配置 */
GAME_CONFIGS: `${API_BASE}/game-configs`,
/** 分享相关 */
SHARE_CREATE: `${API_BASE}/share`,
SHARE_PROGRESS: `${API_BASE}/share/progress`,
/** 用户信息 */
USER_INFO: `${API_BASE}/user/info`,
} as const;
/** 构建加入分享的 URL */
export function getLevelEnterUrl(levelId: string): string {
return `${API_BASE}/levels/${levelId}/enter`;
}
export function getLevelCompleteUrl(levelId: string): string {
return `${API_BASE}/levels/${levelId}/complete`;
}
export function getShareJoinUrl(code: string): string {
return `${API_BASE}/share/${code}/join`;
}
/** 积分操作原因 */
export const POINT_REASONS = {
HINT_UNLOCK: 'hint_unlock',
LEVEL_COMPLETE: 'level_complete',
} as const;
export function getGameConfigUrl(key: string): string {
return `${API_BASE}/game-configs/${key}`;
}
/** 请求超时时间(毫秒) */
export const API_TIMEOUT = {

View File

@@ -7,6 +7,17 @@ export interface ApiEnvelope<T> {
success: boolean;
data: T | null;
message: string | null;
timestamp: string;
}
/** 体力值信息 */
export interface StaminaInfo {
/** 当前体力值(已计算恢复) */
current: number;
/** 体力上限,固定为 5 */
max: number;
/** 下一点体力恢复的时间ISO 8601满体力时为 null */
nextRecoverAt: string | null;
}
/** 登录响应数据 */
@@ -15,21 +26,64 @@ export interface WxLoginData {
user: {
id: string;
nickname: string | null;
points: number;
stamina: number;
};
}
/** 积分响应数据 */
export interface UserAssetsData {
points: number;
/** 用户资料响应数据 */
export interface UserProfileData {
id: string;
nickname: string | null;
stamina: StaminaInfo;
}
/** 游戏数据响应Loading 页面) */
export interface GameData {
user: { id: string; points: number };
user: {
id: string;
stamina: StaminaInfo;
};
completedLevelIds: string[];
}
/** 关卡列表项 */
export interface LevelListItem {
id: string;
level: number;
imageUrl: string;
answer: string | null;
hint1: string | null;
hint2: string | null;
hint3: string | null;
completed: boolean;
timeSpent: number | null;
}
/** 关卡列表响应 */
export interface LevelListData {
levels: LevelListItem[];
total: number;
}
/** 进入关卡响应 */
export interface EnterLevelData {
id: string;
level: number;
imageUrl: string;
answer: string;
hint1: string | null;
hint2: string | null;
hint3: string | null;
stamina: StaminaInfo;
}
/** 通关上报响应 */
export interface CompleteLevelData {
firstClear: boolean;
levelId: string;
timeSpent: number;
}
/** 创建分享响应 */
export interface CreateShareData {
shareCode: string;

View File

@@ -1,7 +1,7 @@
import { SpriteFrame } from 'cc';
/**
* API 返回的单个关卡数据结构
* API 返回的单个关卡数据结构(关卡列表)
*/
export interface ApiLevelData {
/** 关卡 ID (UUID) */
@@ -10,20 +10,22 @@ export interface ApiLevelData {
level: number;
/** 主图 URL */
imageUrl: string;
/** 线索1 */
hint1: string;
/** 线索2 */
hint2: string;
/** 线索3 */
hint3: string;
/** 答案 */
answer: string;
/** 排序 */
sortOrder: number;
/** 线索1(未通关时为 null */
hint1: string | null;
/** 线索2(未通关时为 null */
hint2: string | null;
/** 线索3(未通关时为 null */
hint3: string | null;
/** 答案(未通关时为 null */
answer: string | null;
/** 是否已通关 */
completed: boolean;
/** 通关时长(秒),未通关时为 null */
timeSpent: number | null;
}
/**
* API 响应结构
* API 响应结构(关卡列表)
*/
export interface ApiResponse {
/** 是否成功 */
@@ -47,12 +49,14 @@ export interface RuntimeLevelConfig {
name: string;
/** 主图 SpriteFrame可能为 null 如果加载失败) */
spriteFrame: SpriteFrame | null;
/** 线索1 */
clue1: string;
/** 线索2 */
clue2: string;
/** 线索3 */
clue3: string;
/** 答案 */
answer: string;
/** 线索1(未通关时为 null进入关卡后由 enter 接口获取) */
clue1: string | null;
/** 线索2(未通关时为 null */
clue2: string | null;
/** 线索3(未通关时为 null */
clue3: string | null;
/** 答案(未通关时为 null进入关卡后由 enter 接口获取) */
answer: string | null;
/** 是否已通关 */
completed: boolean;
}

View File

@@ -15,8 +15,6 @@ export class AuthManager {
private _isLoggedIn: boolean = false;
/** 服务端返回的已完成关卡 ID登录后暂存等 LevelDataManager 就绪后同步) */
private _completedLevelIds: string[] = [];
/** 服务端返回的已完成关卡 ID登录后暂存等 LevelDataManager 就绪后同步) */
private _completedLevelIds: string[] = [];
static get instance(): AuthManager {
if (!this._instance) {
@@ -88,12 +86,14 @@ export class AuthManager {
this._userId = user.id;
this._isLoggedIn = true;
StorageManager.setPoints(user.points);
// 获取通关进度
await this.fetchCompletedLevels();
// 登录响应中 stamina 是原始数值(不含实时恢复),先存储默认体力
// 后续通过 game-data 接口获取完整 StaminaInfo
console.log(`[AuthManager] 登录成功,用户: ${user.id},体力: ${user.stamina}`);
// 获取通关进度和完整体力信息
await this.fetchGameData();
console.log(`[AuthManager] 登录成功,用户: ${user.id},积分: ${user.points}`);
return true;
} catch (err) {
console.error('[AuthManager] 登录异常:', err);
@@ -102,45 +102,42 @@ export class AuthManager {
}
private async validateToken(): Promise<boolean> {
try {
const response = await HttpUtil.get<ApiEnvelope<GameData>>(
API_ENDPOINTS.USER_GAME_DATA,
API_TIMEOUT.SHORT
);
const gameData = await this._fetchGameData();
if (!gameData) return false;
if (!response.success || !response.data) {
return false;
}
this._userId = gameData.user.id;
this._isLoggedIn = true;
StorageManager.setStamina(gameData.user.stamina);
this._completedLevelIds = gameData.completedLevelIds;
this._userId = response.data.user.id;
this._isLoggedIn = true;
StorageManager.setPoints(response.data.user.points);
this._completedLevelIds = response.data.completedLevelIds;
console.log(`[AuthManager] Token 验证成功,体力: ${gameData.user.stamina.current}/${gameData.user.stamina.max},已完成: ${this._completedLevelIds.length}`);
return true;
}
console.log(`[AuthManager] Token 验证成功,积分: ${response.data.user.points},已完成: ${this._completedLevelIds.length}`);
return true;
} catch {
return false;
/**
* 登录成功后获取游戏数据(体力 + 通关进度)
*/
private async fetchGameData(): Promise<void> {
const gameData = await this._fetchGameData();
if (gameData) {
this._completedLevelIds = gameData.completedLevelIds;
StorageManager.setStamina(gameData.user.stamina);
}
}
/**
* 登录成功后获取通关进度
* 从服务端获取游戏数据(共用方法)
*/
private async fetchCompletedLevels(): Promise<void> {
private async _fetchGameData(): Promise<GameData | null> {
try {
const response = await HttpUtil.get<ApiEnvelope<GameData>>(
API_ENDPOINTS.USER_GAME_DATA,
API_TIMEOUT.SHORT
);
if (response.success && response.data) {
this._completedLevelIds = response.data.completedLevelIds;
// 同步最新积分
StorageManager.setPoints(response.data.user.points);
}
return (response.success && response.data) ? response.data : null;
} catch {
console.warn('[AuthManager] 获取通关进度失败');
console.warn('[AuthManager] 获取游戏数据失败');
return null;
}
}
}

View File

@@ -109,6 +109,45 @@ export class LevelDataManager {
return this._apiData.length;
}
/**
* 获取指定索引的关卡 ID
* @param index 关卡索引
*/
getLevelId(index: number): string | null {
if (index < 0 || index >= this._apiData.length) {
return null;
}
return this._apiData[index].id;
}
/**
* 检查指定索引的关卡是否已通关
* @param index 关卡索引
*/
isLevelCompleted(index: number): boolean {
if (index < 0 || index >= this._apiData.length) {
return false;
}
return this._apiData[index].completed;
}
/**
* 标记指定关卡为已通关(本地缓存更新)
* @param index 关卡索引
*/
markLevelCompleted(index: number): void {
if (index < 0 || index >= this._apiData.length) {
return;
}
this._apiData[index].completed = true;
// 同时更新运行时配置的 completed 状态
const config = this._levelConfigs.get(index);
if (config) {
this._levelConfigs.set(index, { ...config, completed: true });
}
}
/**
* 根据已完成的关卡 ID 列表,计算最高已完成关卡索引
* @param completedLevelIds 服务端返回的已完成关卡 ID
@@ -191,6 +230,27 @@ export class LevelDataManager {
return config;
}
/**
* 用 enter 接口返回的数据更新运行时关卡配置(填充答案和线索)
*/
updateLevelDetails(index: number, details: { answer: string; hint1: string | null; hint2: string | null; hint3: string | null }): void {
const config = this._levelConfigs.get(index);
if (!config) {
console.warn(`[LevelDataManager] 关卡 ${index} 配置不存在,无法更新详情`);
return;
}
this._levelConfigs.set(index, {
...config,
answer: details.answer,
clue1: details.hint1 ?? null,
clue2: details.hint2 ?? null,
clue3: details.hint3 ?? null,
});
console.log(`[LevelDataManager] 关卡 ${index} 详情已更新`);
}
/**
* 预加载下一关图片(静默加载,不阻塞)
* 在进入当前关卡后调用,提前加载下一关资源
@@ -278,7 +338,8 @@ export class LevelDataManager {
clue1: data.hint1,
clue2: data.hint2,
clue3: data.hint3,
answer: data.answer
answer: data.answer,
completed: data.completed,
};
}

View File

@@ -0,0 +1,110 @@
import { HttpUtil } from './HttpUtil';
import { StorageManager } from './StorageManager';
import { AuthManager } from './AuthManager';
import { API_TIMEOUT, getLevelEnterUrl, getLevelCompleteUrl } from '../config/ApiConfig';
import { ApiEnvelope, StaminaInfo, EnterLevelData, CompleteLevelData } from '../types/ApiTypes';
/**
* 体力值管理器
* 单例模式,负责体力值的服务端同步、进入关卡和通关上报
* 以服务端为准,本地 StorageManager 作为缓存
*/
export class StaminaManager {
private static _instance: StaminaManager | null = null;
static get instance(): StaminaManager {
if (!this._instance) {
this._instance = new StaminaManager();
}
return this._instance;
}
private constructor() {}
/**
* 获取当前体力信息(从本地缓存)
*/
getStamina(): StaminaInfo {
return StorageManager.getStamina();
}
/**
* 更新本地缓存的体力信息
* @param stamina 服务端返回的体力信息
*/
updateStamina(stamina: StaminaInfo): void {
StorageManager.setStamina(stamina);
}
/**
* 检查当前是否有足够的体力
*/
hasStamina(): boolean {
return StorageManager.hasStamina();
}
/**
* 进入关卡
* 消耗 1 点体力(未通关关卡),获取完整关卡详情(含答案和线索)
* @param levelId 关卡 ID
* @returns 关卡详情,失败时返回 null
*/
async enterLevel(levelId: string): Promise<EnterLevelData | null> {
if (!AuthManager.instance.isLoggedIn) {
console.warn('[StaminaManager] 未登录,无法进入关卡');
return null;
}
try {
const response = await HttpUtil.post<ApiEnvelope<EnterLevelData>>(
getLevelEnterUrl(levelId),
{},
API_TIMEOUT.DEFAULT
);
if (response.success && response.data) {
StorageManager.setStamina(response.data.stamina);
console.log(`[StaminaManager] 进入关卡 ${levelId},体力: ${response.data.stamina.current}/${response.data.stamina.max}`);
return response.data;
}
console.warn('[StaminaManager] 进入关卡失败:', response.message);
return null;
} catch (err) {
console.error('[StaminaManager] 进入关卡请求失败:', err);
return null;
}
}
/**
* 通关上报
* @param levelId 关卡 ID
* @param timeSpent 通关耗时(秒)
* @returns 通关响应,失败时返回 null
*/
async completeLevel(levelId: string, timeSpent: number): Promise<CompleteLevelData | null> {
if (!AuthManager.instance.isLoggedIn) {
console.warn('[StaminaManager] 未登录,无法上报通关');
return null;
}
try {
const response = await HttpUtil.post<ApiEnvelope<CompleteLevelData>>(
getLevelCompleteUrl(levelId),
{ timeSpent },
API_TIMEOUT.DEFAULT
);
if (response.success && response.data) {
console.log(`[StaminaManager] 通关上报成功: ${levelId}, 首次: ${response.data.firstClear}, 耗时: ${response.data.timeSpent}s`);
return response.data;
}
console.warn('[StaminaManager] 通关上报失败:', response.message);
return null;
} catch (err) {
console.error('[StaminaManager] 通关上报请求失败:', err);
return null;
}
}
}

View File

@@ -2,7 +2,7 @@
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "ddba71db-75ed-468d-ac99-b4632c0b2ae4",
"uuid": "7fb84423-af68-468c-a17b-7bdd195eb815",
"files": [],
"subMetas": {},
"userData": {}

View File

@@ -1,4 +1,5 @@
import { sys } from 'cc';
import { StaminaInfo } from '../types/ApiTypes';
/**
* 用户进度数据结构
@@ -10,13 +11,21 @@ interface UserProgress {
maxUnlockedLevelIndex: number;
}
/**
* 用户信息结构
*/
interface UserInfo {
avatarUrl: string;
nickName: string;
}
/**
* 本地存储管理器
* 统一管理用户数据的本地持久化存储
*/
export class StorageManager {
/** 积分存储键 */
private static readonly KEY_POINTS = 'game_points';
/** 体力值存储键 */
private static readonly KEY_STAMINA = 'game_stamina';
/** 用户进度存储键 */
private static readonly KEY_PROGRESS = 'game_progress';
@@ -27,11 +36,12 @@ export class StorageManager {
/** 用户信息存储键 */
private static readonly KEY_USER_INFO = 'user_info';
/** 默认积分 */
private static readonly DEFAULT_POINTS = 10;
/** 最小积分 */
private static readonly MIN_POINTS = 0;
/** 默认体力值 */
private static readonly DEFAULT_STAMINA: StaminaInfo = {
current: 5,
max: 5,
nextRecoverAt: null,
};
/** 默认进度 */
private static readonly DEFAULT_PROGRESS: UserProgress = {
@@ -42,75 +52,52 @@ export class StorageManager {
/** 进度缓存(避免重复读取 localStorage */
private static _progressCache: UserProgress | null = null;
// ==================== 积分管理 ====================
/** 体力缓存(避免重复 JSON 解析) */
private static _staminaCache: StaminaInfo | null = null;
// ==================== 体力值管理 ====================
/**
* 获取当前积分
* @returns 当前积分,新用户返回默认值 10
* 获取当前体力信息(带内存缓存,避免重复 JSON 解析)
*/
static getPoints(): number {
const stored = sys.localStorage.getItem(StorageManager.KEY_POINTS);
static getStamina(): StaminaInfo {
if (StorageManager._staminaCache) {
return { ...StorageManager._staminaCache };
}
const stored = sys.localStorage.getItem(StorageManager.KEY_STAMINA);
if (stored === null || stored === '') {
// 新用户,设置默认值
StorageManager.setPoints(StorageManager.DEFAULT_POINTS);
return StorageManager.DEFAULT_POINTS;
StorageManager.setStamina(StorageManager.DEFAULT_STAMINA);
return { ...StorageManager.DEFAULT_STAMINA };
}
const points = parseInt(stored, 10);
// 防止异常数据
if (isNaN(points) || points < 0) {
StorageManager.setPoints(StorageManager.DEFAULT_POINTS);
return StorageManager.DEFAULT_POINTS;
try {
const stamina = JSON.parse(stored) as StaminaInfo;
if (typeof stamina.current !== 'number' || typeof stamina.max !== 'number') {
StorageManager.setStamina(StorageManager.DEFAULT_STAMINA);
return { ...StorageManager.DEFAULT_STAMINA };
}
StorageManager._staminaCache = stamina;
return { ...stamina };
} catch {
StorageManager.setStamina(StorageManager.DEFAULT_STAMINA);
return { ...StorageManager.DEFAULT_STAMINA };
}
return points;
}
/**
* 设置积分
* @param points 积分
* 设置体力信息(同时更新缓存)
*/
static setPoints(points: number): void {
const validPoints = Math.max(StorageManager.MIN_POINTS, points);
sys.localStorage.setItem(StorageManager.KEY_POINTS, validPoints.toString());
console.log(`[StorageManager] 积分已更新: ${validPoints}`);
static setStamina(stamina: StaminaInfo): void {
StorageManager._staminaCache = stamina;
sys.localStorage.setItem(StorageManager.KEY_STAMINA, JSON.stringify(stamina));
console.log(`[StorageManager] 体力已更新: ${stamina.current}/${stamina.max}`);
}
/**
* 消耗一个积分
* @returns 是否消耗成功(积分不足时返回 false
* 检查是否有足够的体力
*/
static consumePoint(): boolean {
const currentPoints = StorageManager.getPoints();
if (currentPoints <= 0) {
console.warn('[StorageManager] 积分不足,无法消耗');
return false;
}
StorageManager.setPoints(currentPoints - 1);
return true;
}
/**
* 增加一个积分
*/
static addPoint(): void {
const currentPoints = StorageManager.getPoints();
StorageManager.setPoints(currentPoints + 1);
console.log(`[StorageManager] 获得一个积分,当前积分: ${currentPoints + 1}`);
}
/**
* 检查是否有足够的积分
* @returns 是否有积分
*/
static hasPoints(): boolean {
return StorageManager.getPoints() > 0;
}
/**
* 重置积分为默认值
*/
static resetPoints(): void {
StorageManager.setPoints(StorageManager.DEFAULT_POINTS);
console.log('[StorageManager] 积分已重置为默认值');
static hasStamina(): boolean {
return StorageManager.getStamina().current > 0;
}
// ==================== 认证 Token 管理 ====================
@@ -261,10 +248,10 @@ export class StorageManager {
}
/**
* 重置所有数据(积分 + 进度)
* 重置所有数据(体力 + 进度)
*/
static resetAll(): void {
StorageManager.resetPoints();
StorageManager.setStamina(StorageManager.DEFAULT_STAMINA);
StorageManager.resetProgress();
StorageManager.clearToken();
StorageManager.clearUserInfo();
@@ -273,14 +260,6 @@ export class StorageManager {
// ==================== 用户信息管理 ====================
/**
* 用户信息结构
*/
interface UserInfo {
avatarUrl: string;
nickName: string;
}
/**
* 保存用户信息(头像、昵称)
* @param userInfo 用户信息对象

View File

@@ -1,120 +0,0 @@
import { HttpUtil } from './HttpUtil';
import { StorageManager } from './StorageManager';
import { AuthManager } from './AuthManager';
import { API_ENDPOINTS, API_TIMEOUT, POINT_REASONS } from '../config/ApiConfig';
import { ApiEnvelope, UserAssetsData } from '../types/ApiTypes';
/**
* 用户资产管理器
* 单例模式,负责积分的服务端同步
* 以服务端为准,本地 StorageManager 作为缓存
*/
export class UserAssetsManager {
private static _instance: UserAssetsManager | null = null;
static get instance(): UserAssetsManager {
if (!this._instance) {
this._instance = new UserAssetsManager();
}
return this._instance;
}
private constructor() {}
/**
* 从服务端获取最新积分
*/
async fetchPoints(): Promise<number> {
if (!AuthManager.instance.isLoggedIn) {
return StorageManager.getPoints();
}
try {
const response = await HttpUtil.get<ApiEnvelope<UserAssetsData>>(
API_ENDPOINTS.USER_ASSETS,
API_TIMEOUT.SHORT
);
if (response.success && response.data) {
StorageManager.setPoints(response.data.points);
return response.data.points;
}
} catch (err) {
console.error('[UserAssetsManager] 获取积分失败:', err);
}
return StorageManager.getPoints();
}
/**
* 消耗积分(解锁提示)
* @returns 是否消耗成功
*/
async consumePoint(levelId?: string, hintIndex?: number): Promise<boolean> {
if (!StorageManager.hasPoints()) {
return false;
}
if (!AuthManager.instance.isLoggedIn) {
return StorageManager.consumePoint();
}
try {
const response = await HttpUtil.post<ApiEnvelope<UserAssetsData>>(
API_ENDPOINTS.USER_ASSETS_CONSUME,
{
reason: POINT_REASONS.HINT_UNLOCK,
levelId,
hintIndex,
},
API_TIMEOUT.SHORT
);
if (response.success && response.data) {
StorageManager.setPoints(response.data.points);
return true;
} else {
console.warn('[UserAssetsManager] 消耗积分失败:', response.message);
return false;
}
} catch (err) {
console.error('[UserAssetsManager] 消耗积分请求失败,降级本地处理:', err);
return StorageManager.consumePoint();
}
}
/**
* 获得积分(通关奖励)
* @param levelId 关卡ID
* @param timeSpent 通关耗时(秒)
* @returns 获得后的积分数
*/
async earnPoint(levelId: string, timeSpent: number): Promise<number> {
if (!AuthManager.instance.isLoggedIn) {
StorageManager.addPoint();
return StorageManager.getPoints();
}
try {
const response = await HttpUtil.post<ApiEnvelope<UserAssetsData>>(
API_ENDPOINTS.USER_ASSETS_EARN,
{
reason: POINT_REASONS.LEVEL_COMPLETE,
levelId,
timeSpent,
},
API_TIMEOUT.SHORT
);
if (response.success && response.data) {
StorageManager.setPoints(response.data.points);
return response.data.points;
}
} catch (err) {
console.error('[UserAssetsManager] 获得积分请求失败,降级本地处理:', err);
}
StorageManager.addPoint();
return StorageManager.getPoints();
}
}

View File

@@ -39,7 +39,7 @@ export class WxSDK {
/**
* 获取 wx 全局对象(仅微信环境下可用)
*/
private static getWx(): any {
static getWx(): any {
if (!WxSDK.isWechat()) return null;
return typeof wx !== 'undefined' ? wx : null;
}
@@ -217,6 +217,84 @@ export class WxSDK {
});
}
// ==================== 激励视频广告 ====================
/** 激励视频广告实例(复用) */
private static _rewardedVideoAd: any = null;
/**
* 展示激励视频广告
* 用户看完广告后返回 true中途退出或失败返回 false
* 非微信环境直接返回 true开发模式直接通过
* @param adUnitId 广告单元 ID默认使用项目配置的 ID
* @returns Promise<boolean> 是否看完广告
*/
static showRewardedVideoAd(adUnitId: string = ''): Promise<boolean> {
return new Promise((resolve) => {
const wxApi = WxSDK.getWx();
if (!wxApi) {
console.log('[WxSDK] 非微信环境,跳过激励视频广告');
resolve(true);
return;
}
if (typeof wxApi.createRewardedVideoAd !== 'function') {
console.warn('[WxSDK] 当前微信版本不支持激励视频广告');
resolve(true);
return;
}
try {
// 复用或创建广告实例
if (!WxSDK._rewardedVideoAd) {
WxSDK._rewardedVideoAd = wxApi.createRewardedVideoAd({
adUnitId: adUnitId,
});
}
const ad = WxSDK._rewardedVideoAd;
// 定义关闭回调(一次性)
const onClose = (res: any) => {
ad.offClose(onClose);
if (res && res.isEnded) {
console.log('[WxSDK] 激励视频广告观看完成');
resolve(true);
} else {
console.log('[WxSDK] 激励视频广告中途退出');
resolve(false);
}
};
// 定义错误回调(一次性)
const onError = (err: any) => {
ad.offError(onError);
ad.offClose(onClose);
console.error('[WxSDK] 激励视频广告错误:', err);
resolve(false);
};
ad.onClose(onClose);
ad.onError(onError);
// 先尝试 show如果广告未加载则先 load
ad.show().catch(() => {
ad.load().then(() => ad.show()).catch((loadErr: any) => {
ad.offClose(onClose);
ad.offError(onError);
console.error('[WxSDK] 激励视频广告加载失败:', loadErr);
resolve(false);
});
});
} catch (err) {
console.error('[WxSDK] 激励视频广告异常:', err);
resolve(false);
}
});
}
// ==================== 启动参数 ====================
/**
* 从启动参数中获取分享码
* @returns 分享码,不存在则返回 null