AdAgentQTT.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. /**
  7. * 趣头条广告组件
  8. */
  9. @ccclass
  10. export default class AdAgentQTT extends AdAgent {
  11. _bannerAd: any = null;
  12. _videoAd: any = null;
  13. _isInsertAdShow: boolean = false;
  14. _isInsertAdLoaded: boolean = false;
  15. _videoCallback: Function = null;
  16. _isVideoLoaded: boolean = false;
  17. _isVideoShow: boolean = false;
  18. //@ts-ignore
  19. qttGame = window.qttGame;
  20. public get ServerConfig() {
  21. if (utils._tool_QTT && utils._tool_QTT.ServerConfig) {
  22. return utils._tool_QTT.ServerConfig;
  23. }
  24. return {};
  25. }
  26. /**
  27. * 当视频广告资源不足
  28. * 备选互动广告
  29. * -- 平台强制添加 ---
  30. */
  31. options: any = null;
  32. public Init() {
  33. if (PlatUtils.IsQTT) {
  34. if (!utils.config.qttconfig.showAd) {
  35. cc.warn("广告开关关闭状态,所有广告不显示!要显示广告,请打开 CommonUtils 组件上VIVIO 配置下的广告开关!");
  36. }
  37. // this.options = {};
  38. // this.options.gametype = 1;//互动游戏类型,1(砸金蛋) 2(laba) 3(大转盘)
  39. // this.options.rewardtype = 1;//互动广告框,只有 1
  40. // this.options.data = {};
  41. // this.options.data.title = "获得奖励";//互动抽中奖后的道具提示文字
  42. // this.options.data.url = "//newidea4-gamecenter-frontend.1sapp.com/game/prod/fkxxl_img/1.png";//互动抽中奖后的道具图标(可选)
  43. // this.options.callback = (res) => {
  44. // //回调函数
  45. // utils.showLog("播放互动广告>> #res=", res)
  46. // if (res == 1) {
  47. // //播放完成,发放奖励
  48. // if (this._videoCallback) {
  49. // this._videoCallback(true, "");
  50. // this._videoCallback = null;
  51. // }
  52. // } else {
  53. // //res = 0 填充不足
  54. // if (this._videoCallback) {
  55. // this._videoCallback(false, "广告加载失败,请稍后再试!");
  56. // this._videoCallback = null;
  57. // }
  58. // }
  59. // };
  60. }
  61. }
  62. /**
  63. * 创建互动广告
  64. */
  65. private createOption() {
  66. let options: any = {};
  67. options.gametype = (Math.floor(Math.random() * 3 + 1));//互动游戏类型,1(砸金蛋) 2(laba) 3(大转盘)
  68. options.rewardtype = 1;//互动广告框,只有 1
  69. options.data = {};
  70. options.data.title = "获得奖励";//互动抽中奖后的道具提示文字
  71. options.data.url = "//newidea4-gamecenter-frontend.1sapp.com/game/prod/fkxxl_img/1.png";//互动抽中奖后的道具图标(可选)
  72. options.callback = (res) => {
  73. //回调函数
  74. utils.showLog("播放互动广告>> #res=", res)
  75. if (res == 1) {
  76. //播放完成,发放奖励
  77. if (this._videoCallback) {
  78. this._videoCallback(true, "");
  79. this._videoCallback = null;
  80. }
  81. } else {
  82. //res = 0 填充不足
  83. if (this._videoCallback) {
  84. this._videoCallback(false, "广告加载失败,请稍后再试!");
  85. this._videoCallback = null;
  86. }
  87. }
  88. };
  89. return options;
  90. }
  91. /**
  92. * 显示banner
  93. */
  94. public ShowBanner(): void {
  95. if (PlatUtils.IsQTT) {
  96. if (!utils.config.qttconfig.showAd) {
  97. return;
  98. }
  99. var options: any = {};
  100. options.index = location; //
  101. if (cc.winSize.height < cc.winSize.width) {
  102. //横屏游戏
  103. options.x = 1;
  104. options.y = 1;
  105. options.w = cc.winSize.width;
  106. options.stage_width = cc.winSize.width;
  107. options.stage_height = cc.winSize.height;
  108. }
  109. this.qttGame.showBanner(options);
  110. }
  111. }
  112. /**
  113. * 隐藏banner
  114. */
  115. public HideBanner(): void {
  116. if (PlatUtils.IsQTT) {
  117. if (!utils.config.qttconfig.showAd) {
  118. return;
  119. }
  120. this.qttGame.hideBanner();
  121. }
  122. }
  123. /**
  124. * 暂时没有插屏广告
  125. * @param location
  126. */
  127. public ShowInterstitial(location: BannerLocation = BannerLocation.Home): void {
  128. // if (PlatUtils.IsQTT) {
  129. // if (!utils.config.qttconfig.showAd) {
  130. // return;
  131. // }
  132. // }
  133. return;
  134. }
  135. /**
  136. * 显示互动直弹广告
  137. */
  138. public showInteractiveAd(): void {
  139. if (PlatUtils.IsQTT) {
  140. if (!this.checkInsertAdShow()) {
  141. return;
  142. }
  143. var options: any = {};
  144. options.rewardtype = 1; //互动广告框,只有 1
  145. utils.showLog("互动直弹时间间隔开始");
  146. this._insertLastShowTime = new Date().getTime();
  147. this.qttGame.showHDReward(options);
  148. }
  149. }
  150. _insertLastShowTime = 0;
  151. /**
  152. * 验证插屏是否能展示
  153. * 2、时间限制 默认30秒
  154. */
  155. private checkInsertAdShow(): boolean {
  156. let intervalTime = this.ServerConfig.intersititial_interval_time ? this.ServerConfig.intersititial_interval_time : 30;
  157. let curTime: number = new Date().getTime();
  158. let interval: number = (curTime - this._insertLastShowTime) / 1000;
  159. utils.showLog("qtt服务器插屏间隔显示时间为:" + intervalTime + "秒!");
  160. utils.showLog("qtt插屏当前广告间隔时间:" + interval + "秒!");
  161. if (intervalTime > 0 && interval < intervalTime) {
  162. utils.showLog("qtt插屏广告显示的间隔少于" + intervalTime + "秒。插屏不显示");
  163. return false;
  164. }
  165. return true;
  166. }
  167. /**
  168. * 显示视频广告
  169. * @param callback 回调函数
  170. */
  171. public ShowVideo(callback: Function) {
  172. if (PlatUtils.IsQTT) {
  173. this._videoCallback = callback;
  174. if (!utils.config.qttconfig.showAd) {
  175. if (this._videoCallback) {
  176. this._videoCallback(false, "暂无视频广告!");
  177. this._videoCallback = null;
  178. }
  179. return;
  180. }
  181. this._isVideoShow = true;
  182. let opt = this.createOption();
  183. this.qttGame.showVideo((res) => {
  184. utils.showLog("播放视频广告>> #res=", res)
  185. if (res == 1) {
  186. if (this._videoCallback) {
  187. this._videoCallback(true, "");
  188. this._videoCallback = null;
  189. }
  190. } else {
  191. if (this._videoCallback) {
  192. if (res == 0) {
  193. this._videoCallback(false, "广告加载失败,请稍后再试!");
  194. } else if (res == 2) {
  195. this._videoCallback(false, "视频播放完毕才能够获取奖励!");
  196. }
  197. this._videoCallback = null;
  198. }
  199. }
  200. }, opt);
  201. }
  202. }
  203. }