diff --git a/utils/tiktokAd.js b/utils/tiktokAd.js new file mode 100644 index 0000000..ff529e2 --- /dev/null +++ b/utils/tiktokAd.js @@ -0,0 +1,94 @@ +//激励视频广告组件 +var videoAd = null; +let rewarded = { + //加载激励视频广告 + load(id) { + if (tt.createRewardedVideoAd) { + const achieveStatus = false; + videoAd = tt.createRewardedVideoAd({ + adUnitId: id + }) + videoAd.onLoad(() => { + console.log('激励视频广告加载成功') + + }) + + //防止奖励重复发放 + // try { + // if (videoAd.onClose) { + // videoAd.offClose(videoAd.onClose); + // console.log("videoAd.offClose卸载成功"); + // } + // } catch (e) { + // console.log("videoAd.offClose 卸载失败"); + // console.error(e); + // } + videoAd.onError(err => { + console.log('激励视频广告加载失败,错误原因为:',err) + }) + videoAd.onClose((res) => { + // 用户点击了【关闭广告】按钮 + if (res && res.isEnded || res === undefined) { + // 正常播放结束,可以下发奖励 + videoAd.offClose() + this.achieveStatus = true; + console.log("激励视频广告播放完毕,可下发奖励",this.achieveStatus); + return this.achieveStatus; + } else { + // 播放中途退出,不下发奖励 + } + }) + } + }, + //展示激励视频广告 + show() { + if (videoAd) { + videoAd.onClose((status) => { + console.log(status + '视频激励广告关闭') + }) + videoAd.show().catch(() => { + // 失败重试 + videoAd.load() + .then(() => videoAd.show()) + .catch(err => { + console.log('激励视频 广告显示失败') + }) + }) + } + } +} + +//插屏广告组件 +var interstitialAd = null; +let insert = { + //加载插屏广告 + load(id) { + if (tt.createInterstitialAd) { + interstitialAd = tt.createInterstitialAd({ + adUnitId: id + }) + interstitialAd.onLoad(() => { + console.log('插屏广告加载中') + }) + interstitialAd.onError((err) => { + console.log('加载错误', err) + }) + interstitialAd.onClose((res) => { + console.log('插屏广告关闭', res) + }) + } + }, + //展示插屏广告 + show() { + if (interstitialAd) { + interstitialAd.show().catch((err) => { + console.error(err) + }) + } + } +} + +module.exports = { + insert, + rewarded +};