From 7e184b9495952dce3a757a71dcc80898986c144d Mon Sep 17 00:00:00 2001 From: gongxh Date: Fri, 25 Apr 2025 10:33:35 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=BE=AE=E4=BF=A1=E5=92=8C?= =?UTF-8?q?=E6=8A=96=E9=9F=B3=E5=B0=8F=E6=B8=B8=E6=88=8F=E5=B9=BF=E5=91=8A?= =?UTF-8?q?=E5=86=85=E9=83=A8=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/lib.ali.api.d.ts | 2 +- src/minigame/alipay/AlipayAds.ts | 4 ++-- src/minigame/bytedance/BytedanceAds.ts | 25 +++++++++-------------- src/minigame/header.ts | 4 ++++ src/minigame/wechat/WechatAds.ts | 28 +++++++++++++++----------- 5 files changed, 33 insertions(+), 30 deletions(-) diff --git a/libs/lib.ali.api.d.ts b/libs/lib.ali.api.d.ts index 0399dc6..7d70c7b 100644 --- a/libs/lib.ali.api.d.ts +++ b/libs/lib.ali.api.d.ts @@ -2552,7 +2552,7 @@ declare namespace AliyMiniprogram { /** * 广告加载完成 */ - onLoad(listener: () => void); + onLoad(listener: (res: any) => void); /** * 监听激励广告加载错误的事件 2 参数类型错误。请确认创建广告时传入的广告单元 ID 合法。 - diff --git a/src/minigame/alipay/AlipayAds.ts b/src/minigame/alipay/AlipayAds.ts index 19420db..bbdb840 100644 --- a/src/minigame/alipay/AlipayAds.ts +++ b/src/minigame/alipay/AlipayAds.ts @@ -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(); }); } diff --git a/src/minigame/bytedance/BytedanceAds.ts b/src/minigame/bytedance/BytedanceAds.ts index d456628..9af31ee 100644 --- a/src/minigame/bytedance/BytedanceAds.ts +++ b/src/minigame/bytedance/BytedanceAds.ts @@ -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(); - }); + this._video_ad.show(); } 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; } diff --git a/src/minigame/header.ts b/src/minigame/header.ts index 7f85289..2b8e0ab 100644 --- a/src/minigame/header.ts +++ b/src/minigame/header.ts @@ -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: "广告正在播放中 不允许重复调用" }, } /** 统一价格限制列表 (微信、支付宝和字节 取交集) */ diff --git a/src/minigame/wechat/WechatAds.ts b/src/minigame/wechat/WechatAds.ts index b3e34f9..43e56f1 100644 --- a/src/minigame/wechat/WechatAds.ts +++ b/src/minigame/wechat/WechatAds.ts @@ -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.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.reset(); + this._video_ad.show().catch(() => { + this._video_ad.load().then(() => { + 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) { /** 广告播放完成 */