调整微信和抖音小游戏广告内部实现

This commit is contained in:
gongxh 2025-04-25 10:33:35 +08:00
parent d4d71825a1
commit 7e184b9495
5 changed files with 33 additions and 30 deletions

View File

@ -2552,7 +2552,7 @@ declare namespace AliyMiniprogram {
/**
* 广
*/
onLoad(listener: () => void);
onLoad(listener: (res: any) => void);
/**
* 广
2 广广 ID -

View File

@ -39,8 +39,8 @@ export class AlipayAds implements IMiniRewardAds {
}
this._video_ad.load().then(() => {
this._video_ad.show();
}).catch((res: { errMsg: string; errNo: number }) => {
this._fail(res.errNo, res.errMsg);
}).catch((res: { errorMessage: string; error: number }) => {
this._fail(res.error, res.errorMessage);
this.reset();
});
}

View File

@ -4,7 +4,7 @@
* @Description: 广
*/
import { log, warn } from "../../tool/log";
import { warn } from "../../tool/log";
import { MiniErrorCode } from "../header";
import { IMiniRewardAds } from "../interface/IMiniAds";
@ -34,41 +34,36 @@ export class BytedanceAds implements IMiniRewardAds {
res.fail(MiniErrorCode.AD_NOT_INIT.code, MiniErrorCode.AD_NOT_INIT.msg);
return;
}
if (this._success) {
warn(MiniErrorCode.AD_PLAYING.msg);
res.fail(MiniErrorCode.AD_PLAYING.code, MiniErrorCode.AD_PLAYING.msg);
return;
}
this._success = res.success;
this._fail = res.fail;
if (!this._video_ad) {
this._video_ad = this.createVideoAd();
}
log("加载广告");
this._video_ad.load().then(() => {
log("广告加载成功");
this._video_ad.show();
}).catch((res: { errMsg: string; errNo: number }) => {
warn(`广告加载失败 errCode:${res.errNo} errMsg:${res.errMsg}`);
this._fail(res.errNo, res.errMsg);
this.reset();
});
}
private createVideoAd(): BytedanceMiniprogram.RewardedVideoAd {
let videoAd = tt.createRewardedVideoAd({ adUnitId: this._adUnitId, multiton: false });
/** 激励视频错误事件的监听函数 */
videoAd.onError((res: { errMsg: string; errCode: number }) => {
warn(`激励视频广告 onError:${res.errCode}:${res.errMsg}`);
this._fail(res.errCode, res.errMsg);
this._fail?.(res.errCode, res.errMsg);
this.reset();
});
videoAd.onClose((res: { isEnded: boolean, count?: number }) => {
if ((res && res.isEnded) || res === undefined) {
if (res.count || res.isEnded) {
/** 广告播放完成 */
this?._success();
this.reset();
} else {
/** 中途退出,不发放奖励 */
this?._fail(MiniErrorCode.AD_EXIT.code, MiniErrorCode.AD_EXIT.msg);
this.reset();
}
this.reset();
});
return videoAd;
}

View File

@ -14,10 +14,14 @@ export const MiniErrorCode = {
PAY_NOT_IMPLEMENTED: { code: -96003, msg: "当前平台未实现支付" },
/** 版本号低 */
VERSION_LOW: { code: -96004, msg: "版本号过低" },
/** 广告未初始化 */
AD_NOT_INIT: { code: -97001, msg: "广告未初始化, 需要先调用init方法初始化" },
/** 广告中途退出*/
AD_EXIT: { code: -97002, msg: "广告中途退出" },
/** 广告正在播放中 不允许重复调用 */
AD_PLAYING: { code: -97003, msg: "广告正在播放中 不允许重复调用" },
}
/** 统一价格限制列表 (微信、支付宝和字节 取交集) */

View File

@ -34,29 +34,33 @@ export class WechatAds implements IMiniRewardAds {
res.fail(MiniErrorCode.AD_NOT_INIT.code, MiniErrorCode.AD_NOT_INIT.msg);
return;
}
if (this._success) {
warn(MiniErrorCode.AD_PLAYING.msg);
res.fail(MiniErrorCode.AD_PLAYING.code, MiniErrorCode.AD_PLAYING.msg);
return;
}
this._success = res.success;
this._fail = res.fail;
if (!this._video_ad) {
this._video_ad = this.createVideoAd();
}
this._video_ad.show().catch(() => {
this._video_ad.load().then(() => {
this._video_ad.show();
}).catch((res: { errMsg: string; errNo: number }) => {
warn(`广告加载失败 errCode:${res.errNo} errMsg:${res.errMsg}`);
this._fail(res.errNo, res.errMsg);
this._video_ad.show().catch((res) => {
this._fail(res.errCode, res.errMsg);
this.reset();
});
}).catch((res) => {
this._fail(res.errCode, res.errMsg);
this.reset();
});
});
}
private createVideoAd(): WechatMiniprogram.RewardedVideoAd {
let videoAd = wx.createRewardedVideoAd({ adUnitId: this._adUnitId });
/** 激励视频错误事件的监听函数 */
videoAd.onError((res: WechatMiniprogram.RewardedVideoAdOnErrorListenerResult) => {
warn(`激励视频广告 onError:${res.errCode}:${res.errMsg}`);
this._fail(res.errCode, res.errMsg);
this.reset();
});
videoAd.onClose((res: WechatMiniprogram.RewardedVideoAdOnCloseListenerResult) => {
if ((res && res.isEnded) || res === undefined) {
/** 广告播放完成 */