import { SDKPlatform } from "./SDKPlatform";
/**
* 微信广告管理器
* 单激励视频广告位版本
*/
class WXAdCore {
// 单例模式
private static _instance: WXAdCore = null!;
public static get instance(): WXAdCore {
if (!this._instance) {
this._instance = new WXAdCore();
}
return this._instance;
}
/*************************************************************** */
private isWxGame: boolean = false;
private isInitialized: boolean = false;
private successCallback: Function | null | undefined = null
private failCallback: Function | null | undefined = null
/*************************************************************** */
private rewardedAd: any = null;
/*************************************************************** */
// 广告ID配置
private config = {
rewardedId: 'adunit-a813b0f873de4b2f',
disableFallbackSharePage: true, // 是否禁用1004错误兜底分享页
};
/*************************************************************** */
// 广告重试配置
private retryConfig = {
maxRetries: 3, // 最大重试次数
retryDelay: 1000, // 重试延迟(毫秒)
};
/*************************************************************** */
// 初始化广告系统
public init(): void {
if (this.isInitialized) {
return;
}
// 检查是否在微信环境
//@ts-ignore
this.isWxGame = SDKPlatform.isWechat && typeof wx !== 'undefined';
if (!this.isWxGame) {
console.warn('当前环境不是微信小游戏,广告功能将不可用');
return;
}
this.isInitialized = true;
console.log('微信广告系统初始化完成');
// 预先创建广告实例
this.createRewardedAd();
// 预先加载广告
this.rewardedAd.load()
.then(() => {
console.log('预加载激励视频成功');
})
.catch((err: any) => {
console.error('预加载激励视频失败:', err);
});
}
// 创建激励视频广告实例
private createRewardedAd(): void {
if (!this.isInitialized || this.rewardedAd) {
return;
}
// @ts-ignore
this.rewardedAd = wx.createRewardedVideoAd({
adUnitId: this.config.rewardedId,
disableFallbackSharePage: this.config.disableFallbackSharePage // 禁用兜底分享页
});
// 只设置一次事件监听,避免重复注册
this.rewardedAd.onLoad((res: any) => {
if (res && res.useFallbackSharePage) {
console.log('使用兜底分享页');
} else {
console.log('激励视频广告加载成功');
}
});
this.rewardedAd.onError((err: any) => {
console.error('激励视频广告错误:', err);
});
this.rewardedAd.onClose((res: { isEnded: any; } | undefined) => {
// 用户点击了【关闭广告】按钮
if (res && res.isEnded || res === undefined) {
// 正常播放结束,可以下发奖励
console.log('激励视频广告播放完成');
if (this.successCallback) {
this.successCallback();
}
} else {
// 播放中途退出,不下发奖励
console.log('激励视频广告播放中途退出');
if (this.failCallback) {
this.failCallback(new Error('用户中途退出'));
}
}
});
}
// 显示激励视频广告(整合了重试逻辑)
public showRewarded(successCallback?: Function, failCallback?: Function, retryCount: number = 0): void {
this.successCallback = successCallback
this.failCallback = failCallback
// 非微信平台
if (!this.isWxGame) {
if (this.successCallback) {
this.successCallback();
}
return
}
if (!this.isInitialized) {
const error = new Error('广告系统未初始化');
console.error(error.message);
if (this.failCallback) {
this.failCallback(error);
}
return;
}
// 用户触发广告后,显示激励视频广告
this.rewardedAd.show().catch(() => {
// 失败重试
this.rewardedAd.load()
.then(() => this.rewardedAd.show())
.catch((err: any) => {
console.error('激励视频 广告显示失败', err)
if (this.failCallback) {
this.failCallback(err);
}
})
})
}
}
//@ts-ignore
/** 游戏实例 */
export const WXAdManager = WXAdCore.instance;
/**
// 示例:在按钮点击事件中调用
public onWatchVideoButtonClick() {
WXAdManager.showRewarded(
() => {
// 广告播放完成,发放奖励
this.grantReward();
},
(error) => {
// 广告播放失败或用户中途退出
console.error("激励视频广告错误:", error);
// 可以给用户提示,如"播放失败,请稍后再试"
}
);
}
private grantReward() {
// 实现奖励发放逻辑
console.log("发放钻石/道具/复活等奖励");
}
*/