AdAgentKwai.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import AdAgent from "./AdAgent";
  2. import { BannerLocation } 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 AdAgentKwai extends AdAgent {
  8. _sysData: any = null;
  9. _bannerAd: any = null;
  10. _videoAd: any = null;
  11. _videoCallback: Function = null;
  12. _isVideoShow: boolean = false;
  13. _isVideoLoaded: boolean = false;
  14. //@ts-ignore
  15. kwaigame: any = window.kwaigame;
  16. canShowVideo: boolean = false;
  17. public Init() {
  18. if (PlatUtils.IsKwai) {
  19. // this.kwaigame.getSystemInfo({
  20. // response: (result) => {
  21. // this._sysData = result;
  22. // utils.showLog("快手>>>获取系统信息:" + JSON.stringify(result));
  23. // }
  24. // });
  25. // utils.showLog(this.kwaigame.isSupport({ feature: this.kwaigame.Support.features.RewardVideo }) + "<<<<<");
  26. // this.canShowVideo = this.kwaigame.isSupport({ feature: this.kwaigame.Support.features.RewardVideo });
  27. // utils.showLog(`当前平台:${this.canShowVideo == true ? "支持" : "不支持"}视频广告!`);
  28. // if (this.canShowVideo) {
  29. utils.registerServerInitEvent(() => {
  30. this._initVideoAd();
  31. }, this)
  32. // }
  33. }
  34. }
  35. public ShowBanner(location: BannerLocation = BannerLocation.Home, args: any = null) {
  36. utils.showLog("快手平台没有banner广告!");
  37. }
  38. /**
  39. * 显示结算广告
  40. * @param data 参数: closeBtn:
  41. * statement_type
  42. * 1:只显示小游戏插屏广告
  43. * 2:只显示6个互推广告
  44. * 3:显示插屏广告+6个互推
  45. */
  46. showStatementAds(): any {
  47. let result: any = { "type": 0, "node": null };
  48. let node: cc.Node = null;
  49. let resType: number = 0;
  50. utils.showLog("结算广告 >> 显示插屏广告+6个互推");
  51. this.ShowInterstitial();
  52. node = utils.showCrossWidget6();
  53. resType = 1;
  54. result.type = resType;
  55. result.node = node;
  56. return result;
  57. }
  58. public HideBanner(location: BannerLocation = BannerLocation.Home) {
  59. }
  60. _interstitialAd: any = null;
  61. public ShowInterstitial(location: BannerLocation = BannerLocation.Home) {
  62. utils.showLog("显示快手插屏");
  63. if (utils.config.kwaiConfig.insertId) {
  64. if (!this._interstitialAd) {
  65. this._interstitialAd = this.kwaigame.createInterstitialAd({ adUnitId: utils.config.kwaiConfig.insertId })
  66. this._interstitialAd.onError(err => {
  67. utils.showLog("插屏广告显示异常:" + JSON.stringify(err));
  68. })
  69. this._interstitialAd.onClose(res => {
  70. utils.showLog("用户点击了【关闭广告】按钮");
  71. })
  72. }
  73. this._interstitialAd && this._interstitialAd.show()
  74. .then(() => utils.showLog('插屏 广告显示成功!')).catch((err) => {
  75. utils.showLog("插屏 广告显示失败 >>" + JSON.stringify(err));
  76. })
  77. } else {
  78. utils.showLog("未配置插屏广告ID");
  79. }
  80. }
  81. public ShowVideo(callback: Function) {
  82. if (PlatUtils.IsKwai) {
  83. // 视频广告
  84. if (this._videoCallback) {
  85. return;
  86. } else {
  87. this._videoCallback = callback;
  88. }
  89. this._isVideoShow = true;
  90. if (!this._videoAd) {
  91. this._initVideoAd();
  92. if (this._videoCallback) {
  93. this._videoCallback(false, "激励视频加载失败!");
  94. this._videoCallback = null;
  95. }
  96. } else {
  97. this._videoAd.show({
  98. success: () => {
  99. utils.showLog("激励视频播放成功");
  100. },
  101. fail: (error) => {
  102. utils.showLog("激励视频播放失败: " + JSON.stringify(error));
  103. if (this._videoCallback) {
  104. this._videoCallback(false, "激励视频加载失败!");
  105. this._videoCallback = null;
  106. }
  107. }
  108. })
  109. }
  110. }
  111. }
  112. _initVideoAd() {
  113. if (!this._videoAd) {
  114. let param: any = {};
  115. param.adUnitId = utils.config.kwaiConfig.videoId;
  116. this._videoAd = this.kwaigame.createRewardedVideoAd(param);
  117. if (this._videoAd) {
  118. utils.showLog("激励广告组件获取成功!");
  119. this._videoAd.onClose((result) => {
  120. utils.showLog("激励视频关闭回调: " + JSON.stringify(result));
  121. if (this._videoCallback) {
  122. this._videoCallback(false, "视频播放完毕才能够获取奖励!");
  123. this._videoCallback = null;
  124. }
  125. });
  126. this._videoAd.onReward((result) => {
  127. utils.showLog("激励视频奖励回调: " + JSON.stringify(result));
  128. if (this._videoCallback) {
  129. this._videoCallback(true, "");
  130. this._videoCallback = null;
  131. }
  132. });
  133. } else {
  134. utils.showLog("激励广告组件获取失败");
  135. }
  136. }
  137. }
  138. }