123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- import AdAgent from "./AdAgent";
- import { BannerLocation, LevelStatus, BeForGameOverAdId } from "./YZ_Constant";
- import PlatUtils from "./PlatUtils";
- import { utils } from "./Utils";
- const { ccclass, property } = cc._decorator;
- @ccclass
- export default class AdAgentDouyin extends AdAgent {
- _bannerAd: any = null;
- _videoAd: any = null;
- _sysData: any = null;
- _bannerAds: any[] = [];
- _isBannerShow: boolean = false;
- public get ServerConfig() {
- return utils.Tool_Douyin.ServerConfig;
- }
- //@ts-ignore
- tt: any = window.tt;
- public Init() {
- if (PlatUtils.IsDouyin) {
- //@ts-ignore
- this._sysData = utils.Tool_Douyin._sysInfo;
- // utils.registerServerInitEvent(() => {
- // // this._initVideoAd();
- // }, this)
- }
- }
- _bannerBottom: number = 0;
- public ShowBanner(location: BannerLocation, args: any = null) {
- if (PlatUtils.IsDouyin) {
- if (utils.isShowRecommondGamesBanner() && utils.Tool_Douyin.isShowMoreGamesModal()) {
- utils.showRecommendGamesBanner();
- utils.showLog(`服务器配置展示自定义banner`);
- return;
- }
- if (utils.Tool_Douyin.isNewsArticleLite) {
- utils.showLog("头条极速版不显示Banner广告");
- return;
- }
- if (this._isConfigValid()) {
- let argsTmp = args;
- let bannerSizePercent: number = 1;
- if (argsTmp && argsTmp.width) {
- bannerSizePercent = ((argsTmp.width < 0) ? 0.1 : argsTmp.width);
- bannerSizePercent = ((argsTmp.width > 1) ? 1 : bannerSizePercent);
- }
- if (argsTmp && argsTmp.bottom) {
- this._bannerBottom = argsTmp.bottom / this._sysData.pixelRatio;
- this._bannerBottom = ((this._bannerBottom < 0) ? 0 : this._bannerBottom);
- this._bannerBottom = ((this._bannerBottom > this._sysData.screenHeight) ? this._sysData.screenHeight : this._bannerBottom);
- }
- let targetBannerAdWidth = 60;
- let left: number = (this._sysData.screenWidth - targetBannerAdWidth) * 0.5;
- let top: number = this._sysData.screenHeight - (targetBannerAdWidth / 16 * 9 - this._bannerBottom);
- // 创建一个居于屏幕底部正中的广告
- let bannerId: string = utils.config.douyinconfig.bannerId;
- utils.showLog("显示Banner广告: bannerId=" + bannerId + " #targetBannerAdWidth=", targetBannerAdWidth);
- //@ts-ignore
- try {
- let bannerAd = this.tt.createBannerAd({
- adUnitId: bannerId,
- style: {
- width: targetBannerAdWidth,
- left: left,
- top: top
- }
- });
- if (bannerAd) {
- let self = this;
- bannerAd.onLoad(function () {
- bannerAd.show().then(() => {
- utils.showLog('广告显示成功');
- for (let i = 0; i < self._bannerAds.length; i++) {
- if (self._bannerAds[i] != bannerAd && self._bannerAds[i] != null) {
- self._bannerAds[i].destroy();
- }
- }
- self._bannerAds.length = 0;
- self._bannerAds.push(bannerAd);
- }).catch(err => {
- utils.showLog(`广告组件出现问题 ${err.errCode, err.errMsg}`);
- });
- });
- bannerAd.onError((err) => {
- if (err) {
- utils.showLog(`Banner 广告出错: errCode: ${err.errCode} errMsg:${err.errMsg}`);
- }
- });
- bannerAd.onResize((res) => {
- // 如果一开始设置的 banner 宽度超过了系统限制,可以在此处加以调整
- bannerAd.style.top = this._sysData.screenHeight - res.height - this._bannerBottom;
- if (res.width > 0) {
- bannerAd.style.left = (this._sysData.screenWidth - res.width) * 0.5;
- } else {
- bannerAd.style.left = (this._sysData.screenWidth - targetBannerAdWidth) / 2;
- }
- });
- this._bannerAds.push(bannerAd);
- }
- } catch (error) {
- }
- } else {
- utils.showLog("抖音小游戏配置文件出错!");
- }
- }
- }
- public HideBanner(location: BannerLocation) {
- if (PlatUtils.IsDouyin) {
- for (let i = 0; i < this._bannerAds.length; i++) {
- if (this._bannerAds[i] != null) {
- this._bannerAds[i].destroy();
- }
- }
- this._bannerAds.length = 0;
- }
- }
- private checkCanShowInterstitial() {
- if (this.tt.createInterstitialAd) {
- return true;
- }
- return false;
- }
- _lastShowInterstitialTime: number = 0
- public ShowInterstitial(location: BannerLocation) {
- if (PlatUtils.IsDouyin) {
- if (this.checkCanShowInterstitial()) {
- let curTime: number = new Date().getTime();
- let interval: number = (curTime - this._lastShowInterstitialTime) / 1000;
- if (interval < 30) {
- utils.showLog("距离插屏广告或者激励视频广告上次播放时间间隔不足30秒");
- return;
- }
- this._lastShowInterstitialTime = curTime;
- utils.delayCall(this._createMiniGameInsertAd.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
- } else {
- utils.showLog("当前客户端版本不支持插屏!");
- }
- }
- }
- private interstitialAd = null;
- public _createMiniGameInsertAd() {
- try {
- if (!utils.config.douyinconfig.insertId) {
- utils.showLog("插屏ID配置有误");
- return;
- }
- if (this.interstitialAd) {
- this.interstitialAd.destroy();
- this.interstitialAd = null;
- }
- this.interstitialAd = this.tt.createInterstitialAd({
- adUnitId: utils.config.douyinconfig.insertId
- });
- this.interstitialAd.load().then(() => {
- this.interstitialAd.show();
- }).catch(err => {
- utils.showLog(err);
- utils.showLog("err.errCode:" + err.errCode);
- switch (err.errCode) {
- case 2001:
- utils.showLog("小程序启动一定时间内不允许展示插屏广告")
- break;
- case 2002:
- utils.showLog("距离小程序插屏广告或者激励视频广告上次播放时间间隔不足,不允许展示插屏广告")
- break;
- case 2003:
- utils.showLog("当前正在播放激励视频广告或者插屏广告,不允许再次展示插屏广告")
- break;
- case 2004:
- utils.showLog("该项错误不是开发者的异常情况,或因小程序页面切换导致广告渲染失败")
- break;
- case 2005:
- utils.showLog("插屏广告实例不允许跨页面调用")
- break;
- default:
- // 参考 https://minigame.vivo.com.cn/documents/#/lesson/open-ability/ad?id=广告错误码信息 对错误码做分类处理
- utils.showLog("插屏广告展示失败")
- break;
- }
- });
- } catch (error) {
- utils.showLog(error);
- }
- }
- _videoCallback: Function = null;
- _isVideoLoaded: boolean = false;
- _isVideoShow: boolean = false;
- public ShowVideo(callback: Function) {
- if (PlatUtils.IsDouyin) {
- this._videoCallback = callback;
- this._isVideoShow = true;
- if (utils.Tool_Douyin.ServerConfig) {
- let posId: string = utils.config.douyinconfig.videoId.trim();
- utils.showLog("video广告ID:" + posId);
- if (!this._videoAd) {
- //@ts-ignore
- this._videoAd = tt.createRewardedVideoAd({
- adUnitId: posId
- });
- if (this._videoAd) {
- utils.showLog("初始化注册视频回调!");
- // this._videoAd.onLoad(() => {
- // utils.showLog("激励视频加载成功", this._isVideoShow);
- // // this._isVideoLoaded = true;
- // // if (this._isVideoShow) {
- // // this._isVideoShow = false;
- // // }
- // })
- this._videoAd.onError((err) => {
- utils.showLog(`激励视频出错: ${err.code, err.msg}`);
- this._isVideoLoaded = false;
- if (this._videoCallback) {
- this._videoCallback(false, "暂无视频广告!");
- this._videoCallback = null;
- }
- });
- this._videoAd.onClose((res) => {
- this._isVideoShow = false;
- this._isVideoLoaded = false;
- if (res.isEnded) {
- utils.showLog('激励视频广告完成,发放奖励');
- if (this._videoCallback) {
- this._videoCallback(true, "");
- this._videoCallback = null;
- }
- } else {
- utils.showLog('激励视频广告取消关闭,不发放奖励');
- if (this._videoCallback) {
- this._videoCallback(false, "观看完视频才能获得奖励!");
- this._videoCallback = null;
- }
- }
- });
- } else {
- utils.showLog("videoAd 对象创建失败,播放失败!");
- if (this._videoCallback) {
- this._videoCallback(false, "暂无视频广告!");
- this._videoCallback = null;
- }
- }
- }
- // else {
- // if (this._isVideoLoaded) {
- // this._videoAd.show().then(() => {
- // utils.showLog("激励视频播放成功!");
- // }).catch((ero) => {
- // utils.showLog("激励视频播放失败! >>>>" + ero);
- // if (this._videoCallback) {
- // this._videoCallback(false, "视频播放失败,请稍后再试!");
- // this._videoCallback = null;
- // }
- // });
- // this._isVideoShow = false;
- // } else {
- this._videoAd.load().then(() => {
- utils.showLog("激励视频加载成功");
- this._videoAd.show().then(() => {
- utils.showLog("激励视频播放成功!");
- this._lastShowInterstitialTime = new Date().getTime();
- }).catch(() => {
- utils.showLog("激励视频播放失败!");
- if (this._videoCallback) {
- this._videoCallback(false, "视频播放失败,请稍后再试!");
- this._videoCallback = null;
- }
- });
- }).catch(() => {
- utils.showLog("再次播放视频资源加载失败!");
- if (this._videoCallback) {
- this._videoCallback(false, "视频播放失败,请稍后再试!");
- this._videoCallback = null;
- }
- });
- }
- // }
- } else {
- utils.showLog("获取配置失败,视频无法播放!");
- if (this._videoCallback) {
- this._videoCallback(false, "暂无视频广告!");
- this._videoCallback = null;
- }
- }
- // }
- }
- _initVideoAd() {
- if (!this._videoAd) {
- if (utils.Tool_Douyin.ServerConfig) {
- let posId: string = utils.config.douyinconfig.videoId.trim();
- utils.showLog("video广告ID:" + posId);
- //@ts-ignore
- this._videoAd = tt.createRewardedVideoAd({
- adUnitId: posId
- });
- if (this._videoAd) {
- utils.showLog("初始化注册视频回调!");
- this._videoAd.onLoad(() => {
- utils.showLog("激励视频加载成功", this._isVideoShow);
- this._isVideoLoaded = true;
- if (this._isVideoShow) {
- this._videoAd.show().then(() => {
- utils.showLog("激励视频播放成功!");
- }).catch(() => {
- utils.showLog("激励视频播放失败!");
- if (this._videoCallback) {
- this._videoCallback(false, "视频播放失败,请稍后再试!");
- this._videoCallback = null;
- }
- });
- this._isVideoShow = false;
- }
- })
- this._videoAd.onError((err) => {
- utils.showLog(`激励视频出错: ${err.code, err.msg}`, err);
- this._isVideoLoaded = false;
- if (this._videoCallback) {
- this._videoCallback(false, "暂无视频广告!");
- this._videoCallback = null;
- }
- });
- this._videoAd.onClose((res) => {
- this._isVideoShow = false;
- this._isVideoLoaded = false;
- if (res.isEnded) {
- utils.showLog('激励视频广告完成,发放奖励');
- if (this._videoCallback) {
- this._videoCallback(true, "");
- this._videoCallback = null;
- }
- } else {
- utils.showLog('激励视频广告取消关闭,不发放奖励');
- if (this._videoCallback) {
- this._videoCallback(false, "观看完视频才能获得奖励!");
- this._videoCallback = null;
- }
- }
- this._videoAd.load().then(() => {
- utils.showLog("关闭视频后重新加载视频资源成功!");
- this._isVideoShow = false;
- this._isVideoLoaded = true;
- });
- });
- } else {
- utils.showLog("暂无视频广告!");
- if (this._videoCallback) {
- this._videoCallback(false, "暂无视频广告!");
- this._videoCallback = null;
- }
- }
- } else {
- utils.showLog("暂无视频广告!");
- if (this._videoCallback) {
- this._videoCallback(false, "暂无视频广告!");
- this._videoCallback = null;
- }
- }
- }
- }
- _isConfigValid() {
- if (PlatUtils.IsDouyin) {
- return (utils.config.douyinconfig
- && utils.config.douyinconfig.appID
- && utils.config.douyinconfig.bannerId
- && utils.config.douyinconfig.videoId);
- }
- return false;
- }
- public ShowCloseBtnBanner(location: BannerLocation = BannerLocation.Home, args: any) {
- utils.showLog("ShowCloseBtnBanner >>>>>>>>>.");
- let isMoveBtn = 0;
- let fadeInTime = 0;
- let btn: cc.Node = args.closeBtn;
- let winHeight = cc.winSize.height;
- btn.opacity = 0;
- if (this.ServerConfig) {
- if (this.ServerConfig.show_close_btn_delay) {
- fadeInTime = this.ServerConfig.show_close_btn_delay;
- }
- // setTimeout(() => {
- // utils.showLog("延迟调用关闭按钮的Banner >>>>");
- // this.ShowBanner(location, args);
- // var adY = 200;
- // utils.showLog('utils - adY:' + adY);
- // if (adY > 0 && btn) {
- // btn.y = -(winHeight / 2 - adY) + btn.height;
- // utils.showLog("btnClose.y" + btn.y);
- // }
- // }, isMoveBtn);
- setTimeout(() => {
- btn.runAction(cc.fadeIn(0.3));
- }, fadeInTime * 1000);
- }
- }
- }
|