AdAgentDouyin.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. import AdAgent from "./AdAgent";
  2. import { BannerLocation, LevelStatus, BeForGameOverAdId } from "./YZ_Constant";
  3. import PlatUtils from "./PlatUtils";
  4. import { utils } from "./Utils";
  5. const { ccclass, property } = cc._decorator;
  6. @ccclass
  7. export default class AdAgentDouyin extends AdAgent {
  8. _bannerAd: any = null;
  9. _videoAd: any = null;
  10. _sysData: any = null;
  11. _bannerAds: any[] = [];
  12. _isBannerShow: boolean = false;
  13. public get ServerConfig() {
  14. return utils.Tool_Douyin.ServerConfig;
  15. }
  16. //@ts-ignore
  17. tt: any = window.tt;
  18. public Init() {
  19. if (PlatUtils.IsDouyin) {
  20. //@ts-ignore
  21. this._sysData = utils.Tool_Douyin._sysInfo;
  22. // utils.registerServerInitEvent(() => {
  23. // // this._initVideoAd();
  24. // }, this)
  25. }
  26. }
  27. _bannerBottom: number = 0;
  28. public ShowBanner(location: BannerLocation, args: any = null) {
  29. if (PlatUtils.IsDouyin) {
  30. if (utils.isShowRecommondGamesBanner() && utils.Tool_Douyin.isShowMoreGamesModal()) {
  31. utils.showRecommendGamesBanner();
  32. utils.showLog(`服务器配置展示自定义banner`);
  33. return;
  34. }
  35. if (utils.Tool_Douyin.isNewsArticleLite) {
  36. utils.showLog("头条极速版不显示Banner广告");
  37. return;
  38. }
  39. if (this._isConfigValid()) {
  40. let argsTmp = args;
  41. let bannerSizePercent: number = 1;
  42. if (argsTmp && argsTmp.width) {
  43. bannerSizePercent = ((argsTmp.width < 0) ? 0.1 : argsTmp.width);
  44. bannerSizePercent = ((argsTmp.width > 1) ? 1 : bannerSizePercent);
  45. }
  46. if (argsTmp && argsTmp.bottom) {
  47. this._bannerBottom = argsTmp.bottom / this._sysData.pixelRatio;
  48. this._bannerBottom = ((this._bannerBottom < 0) ? 0 : this._bannerBottom);
  49. this._bannerBottom = ((this._bannerBottom > this._sysData.screenHeight) ? this._sysData.screenHeight : this._bannerBottom);
  50. }
  51. let targetBannerAdWidth = 60;
  52. let left: number = (this._sysData.screenWidth - targetBannerAdWidth) * 0.5;
  53. let top: number = this._sysData.screenHeight - (targetBannerAdWidth / 16 * 9 - this._bannerBottom);
  54. // 创建一个居于屏幕底部正中的广告
  55. let bannerId: string = utils.config.douyinconfig.bannerId;
  56. utils.showLog("显示Banner广告: bannerId=" + bannerId + " #targetBannerAdWidth=", targetBannerAdWidth);
  57. //@ts-ignore
  58. try {
  59. let bannerAd = this.tt.createBannerAd({
  60. adUnitId: bannerId,
  61. style: {
  62. width: targetBannerAdWidth,
  63. left: left,
  64. top: top
  65. }
  66. });
  67. if (bannerAd) {
  68. let self = this;
  69. bannerAd.onLoad(function () {
  70. bannerAd.show().then(() => {
  71. utils.showLog('广告显示成功');
  72. for (let i = 0; i < self._bannerAds.length; i++) {
  73. if (self._bannerAds[i] != bannerAd && self._bannerAds[i] != null) {
  74. self._bannerAds[i].destroy();
  75. }
  76. }
  77. self._bannerAds.length = 0;
  78. self._bannerAds.push(bannerAd);
  79. }).catch(err => {
  80. utils.showLog(`广告组件出现问题 ${err.errCode, err.errMsg}`);
  81. });
  82. });
  83. bannerAd.onError((err) => {
  84. if (err) {
  85. utils.showLog(`Banner 广告出错: errCode: ${err.errCode} errMsg:${err.errMsg}`);
  86. }
  87. });
  88. bannerAd.onResize((res) => {
  89. // 如果一开始设置的 banner 宽度超过了系统限制,可以在此处加以调整
  90. bannerAd.style.top = this._sysData.screenHeight - res.height - this._bannerBottom;
  91. if (res.width > 0) {
  92. bannerAd.style.left = (this._sysData.screenWidth - res.width) * 0.5;
  93. } else {
  94. bannerAd.style.left = (this._sysData.screenWidth - targetBannerAdWidth) / 2;
  95. }
  96. });
  97. this._bannerAds.push(bannerAd);
  98. }
  99. } catch (error) {
  100. }
  101. } else {
  102. utils.showLog("抖音小游戏配置文件出错!");
  103. }
  104. }
  105. }
  106. public HideBanner(location: BannerLocation) {
  107. if (PlatUtils.IsDouyin) {
  108. for (let i = 0; i < this._bannerAds.length; i++) {
  109. if (this._bannerAds[i] != null) {
  110. this._bannerAds[i].destroy();
  111. }
  112. }
  113. this._bannerAds.length = 0;
  114. }
  115. }
  116. private checkCanShowInterstitial() {
  117. if (this.tt.createInterstitialAd) {
  118. return true;
  119. }
  120. return false;
  121. }
  122. _lastShowInterstitialTime: number = 0
  123. public ShowInterstitial(location: BannerLocation) {
  124. if (PlatUtils.IsDouyin) {
  125. if (this.checkCanShowInterstitial()) {
  126. let curTime: number = new Date().getTime();
  127. let interval: number = (curTime - this._lastShowInterstitialTime) / 1000;
  128. if (interval < 30) {
  129. utils.showLog("距离插屏广告或者激励视频广告上次播放时间间隔不足30秒");
  130. return;
  131. }
  132. this._lastShowInterstitialTime = curTime;
  133. utils.delayCall(this._createMiniGameInsertAd.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  134. } else {
  135. utils.showLog("当前客户端版本不支持插屏!");
  136. }
  137. }
  138. }
  139. private interstitialAd = null;
  140. public _createMiniGameInsertAd() {
  141. try {
  142. if (!utils.config.douyinconfig.insertId) {
  143. utils.showLog("插屏ID配置有误");
  144. return;
  145. }
  146. if (this.interstitialAd) {
  147. this.interstitialAd.destroy();
  148. this.interstitialAd = null;
  149. }
  150. this.interstitialAd = this.tt.createInterstitialAd({
  151. adUnitId: utils.config.douyinconfig.insertId
  152. });
  153. this.interstitialAd.load().then(() => {
  154. this.interstitialAd.show();
  155. }).catch(err => {
  156. utils.showLog(err);
  157. utils.showLog("err.errCode:" + err.errCode);
  158. switch (err.errCode) {
  159. case 2001:
  160. utils.showLog("小程序启动一定时间内不允许展示插屏广告")
  161. break;
  162. case 2002:
  163. utils.showLog("距离小程序插屏广告或者激励视频广告上次播放时间间隔不足,不允许展示插屏广告")
  164. break;
  165. case 2003:
  166. utils.showLog("当前正在播放激励视频广告或者插屏广告,不允许再次展示插屏广告")
  167. break;
  168. case 2004:
  169. utils.showLog("该项错误不是开发者的异常情况,或因小程序页面切换导致广告渲染失败")
  170. break;
  171. case 2005:
  172. utils.showLog("插屏广告实例不允许跨页面调用")
  173. break;
  174. default:
  175. // 参考 https://minigame.vivo.com.cn/documents/#/lesson/open-ability/ad?id=广告错误码信息 对错误码做分类处理
  176. utils.showLog("插屏广告展示失败")
  177. break;
  178. }
  179. });
  180. } catch (error) {
  181. utils.showLog(error);
  182. }
  183. }
  184. _videoCallback: Function = null;
  185. _isVideoLoaded: boolean = false;
  186. _isVideoShow: boolean = false;
  187. public ShowVideo(callback: Function) {
  188. if (PlatUtils.IsDouyin) {
  189. this._videoCallback = callback;
  190. this._isVideoShow = true;
  191. if (utils.Tool_Douyin.ServerConfig) {
  192. let posId: string = utils.config.douyinconfig.videoId.trim();
  193. utils.showLog("video广告ID:" + posId);
  194. if (!this._videoAd) {
  195. //@ts-ignore
  196. this._videoAd = tt.createRewardedVideoAd({
  197. adUnitId: posId
  198. });
  199. if (this._videoAd) {
  200. utils.showLog("初始化注册视频回调!");
  201. // this._videoAd.onLoad(() => {
  202. // utils.showLog("激励视频加载成功", this._isVideoShow);
  203. // // this._isVideoLoaded = true;
  204. // // if (this._isVideoShow) {
  205. // // this._isVideoShow = false;
  206. // // }
  207. // })
  208. this._videoAd.onError((err) => {
  209. utils.showLog(`激励视频出错: ${err.code, err.msg}`);
  210. this._isVideoLoaded = false;
  211. if (this._videoCallback) {
  212. this._videoCallback(false, "暂无视频广告!");
  213. this._videoCallback = null;
  214. }
  215. });
  216. this._videoAd.onClose((res) => {
  217. this._isVideoShow = false;
  218. this._isVideoLoaded = false;
  219. if (res.isEnded) {
  220. utils.showLog('激励视频广告完成,发放奖励');
  221. if (this._videoCallback) {
  222. this._videoCallback(true, "");
  223. this._videoCallback = null;
  224. }
  225. } else {
  226. utils.showLog('激励视频广告取消关闭,不发放奖励');
  227. if (this._videoCallback) {
  228. this._videoCallback(false, "观看完视频才能获得奖励!");
  229. this._videoCallback = null;
  230. }
  231. }
  232. });
  233. } else {
  234. utils.showLog("videoAd 对象创建失败,播放失败!");
  235. if (this._videoCallback) {
  236. this._videoCallback(false, "暂无视频广告!");
  237. this._videoCallback = null;
  238. }
  239. }
  240. }
  241. // else {
  242. // if (this._isVideoLoaded) {
  243. // this._videoAd.show().then(() => {
  244. // utils.showLog("激励视频播放成功!");
  245. // }).catch((ero) => {
  246. // utils.showLog("激励视频播放失败! >>>>" + ero);
  247. // if (this._videoCallback) {
  248. // this._videoCallback(false, "视频播放失败,请稍后再试!");
  249. // this._videoCallback = null;
  250. // }
  251. // });
  252. // this._isVideoShow = false;
  253. // } else {
  254. this._videoAd.load().then(() => {
  255. utils.showLog("激励视频加载成功");
  256. this._videoAd.show().then(() => {
  257. utils.showLog("激励视频播放成功!");
  258. this._lastShowInterstitialTime = new Date().getTime();
  259. }).catch(() => {
  260. utils.showLog("激励视频播放失败!");
  261. if (this._videoCallback) {
  262. this._videoCallback(false, "视频播放失败,请稍后再试!");
  263. this._videoCallback = null;
  264. }
  265. });
  266. }).catch(() => {
  267. utils.showLog("再次播放视频资源加载失败!");
  268. if (this._videoCallback) {
  269. this._videoCallback(false, "视频播放失败,请稍后再试!");
  270. this._videoCallback = null;
  271. }
  272. });
  273. }
  274. // }
  275. } else {
  276. utils.showLog("获取配置失败,视频无法播放!");
  277. if (this._videoCallback) {
  278. this._videoCallback(false, "暂无视频广告!");
  279. this._videoCallback = null;
  280. }
  281. }
  282. // }
  283. }
  284. _initVideoAd() {
  285. if (!this._videoAd) {
  286. if (utils.Tool_Douyin.ServerConfig) {
  287. let posId: string = utils.config.douyinconfig.videoId.trim();
  288. utils.showLog("video广告ID:" + posId);
  289. //@ts-ignore
  290. this._videoAd = tt.createRewardedVideoAd({
  291. adUnitId: posId
  292. });
  293. if (this._videoAd) {
  294. utils.showLog("初始化注册视频回调!");
  295. this._videoAd.onLoad(() => {
  296. utils.showLog("激励视频加载成功", this._isVideoShow);
  297. this._isVideoLoaded = true;
  298. if (this._isVideoShow) {
  299. this._videoAd.show().then(() => {
  300. utils.showLog("激励视频播放成功!");
  301. }).catch(() => {
  302. utils.showLog("激励视频播放失败!");
  303. if (this._videoCallback) {
  304. this._videoCallback(false, "视频播放失败,请稍后再试!");
  305. this._videoCallback = null;
  306. }
  307. });
  308. this._isVideoShow = false;
  309. }
  310. })
  311. this._videoAd.onError((err) => {
  312. utils.showLog(`激励视频出错: ${err.code, err.msg}`, err);
  313. this._isVideoLoaded = false;
  314. if (this._videoCallback) {
  315. this._videoCallback(false, "暂无视频广告!");
  316. this._videoCallback = null;
  317. }
  318. });
  319. this._videoAd.onClose((res) => {
  320. this._isVideoShow = false;
  321. this._isVideoLoaded = false;
  322. if (res.isEnded) {
  323. utils.showLog('激励视频广告完成,发放奖励');
  324. if (this._videoCallback) {
  325. this._videoCallback(true, "");
  326. this._videoCallback = null;
  327. }
  328. } else {
  329. utils.showLog('激励视频广告取消关闭,不发放奖励');
  330. if (this._videoCallback) {
  331. this._videoCallback(false, "观看完视频才能获得奖励!");
  332. this._videoCallback = null;
  333. }
  334. }
  335. this._videoAd.load().then(() => {
  336. utils.showLog("关闭视频后重新加载视频资源成功!");
  337. this._isVideoShow = false;
  338. this._isVideoLoaded = true;
  339. });
  340. });
  341. } else {
  342. utils.showLog("暂无视频广告!");
  343. if (this._videoCallback) {
  344. this._videoCallback(false, "暂无视频广告!");
  345. this._videoCallback = null;
  346. }
  347. }
  348. } else {
  349. utils.showLog("暂无视频广告!");
  350. if (this._videoCallback) {
  351. this._videoCallback(false, "暂无视频广告!");
  352. this._videoCallback = null;
  353. }
  354. }
  355. }
  356. }
  357. _isConfigValid() {
  358. if (PlatUtils.IsDouyin) {
  359. return (utils.config.douyinconfig
  360. && utils.config.douyinconfig.appID
  361. && utils.config.douyinconfig.bannerId
  362. && utils.config.douyinconfig.videoId);
  363. }
  364. return false;
  365. }
  366. public ShowCloseBtnBanner(location: BannerLocation = BannerLocation.Home, args: any) {
  367. utils.showLog("ShowCloseBtnBanner >>>>>>>>>.");
  368. let isMoveBtn = 0;
  369. let fadeInTime = 0;
  370. let btn: cc.Node = args.closeBtn;
  371. let winHeight = cc.winSize.height;
  372. btn.opacity = 0;
  373. if (this.ServerConfig) {
  374. if (this.ServerConfig.show_close_btn_delay) {
  375. fadeInTime = this.ServerConfig.show_close_btn_delay;
  376. }
  377. // setTimeout(() => {
  378. // utils.showLog("延迟调用关闭按钮的Banner >>>>");
  379. // this.ShowBanner(location, args);
  380. // var adY = 200;
  381. // utils.showLog('utils - adY:' + adY);
  382. // if (adY > 0 && btn) {
  383. // btn.y = -(winHeight / 2 - adY) + btn.height;
  384. // utils.showLog("btnClose.y" + btn.y);
  385. // }
  386. // }, isMoveBtn);
  387. setTimeout(() => {
  388. btn.runAction(cc.fadeIn(0.3));
  389. }, fadeInTime * 1000);
  390. }
  391. }
  392. }