AdSystem.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { IADSDKMgr } from "../sdk/ad/IADSDKMgr";
  2. import JsbSystem from "./JsbSystem";
  3. /**
  4. * 广告管理类脚本
  5. * @description 显示/播放广告
  6. * @author 冯聪
  7. */
  8. export default class AdSystem {
  9. /** 构造函数
  10. * @param adSdk 传入当前使用广告sdk
  11. */
  12. constructor(adSdk: IADSDKMgr) {
  13. this.adSdk = adSdk;
  14. // this.init();
  15. }
  16. /** 广告sdk */
  17. private adSdk: IADSDKMgr = null;
  18. /** 是否显示广告 */
  19. public ifShowAd: boolean = true;
  20. /** 视频广告回调 */
  21. public videoAdcallBack: Function = null;
  22. /** 初始化广告 */
  23. public init() {
  24. //判定下平台
  25. if (cc.sys.os == cc.sys.OS_ANDROID) {
  26. this.ifShowAd = true;
  27. }
  28. else {
  29. this.ifShowAd = false;
  30. }
  31. //初始化Topon广告SDK
  32. this.adSdk.initSDK();
  33. }
  34. /**
  35. * 观看广告
  36. * @param callBack 广告相关的回调
  37. */
  38. public watchAd(callBack: Function) {
  39. if (this.isAdCooling()) return;
  40. this.videoAdcallBack = callBack;
  41. if (!this.ifShowAd) {
  42. this.wacthAdSuccess()
  43. }
  44. else {
  45. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  46. if (cc.sys.os == cc.sys.OS_ANDROID) {
  47. // jsb.reflection.callStaticMethod("org/cocos2dx/javascript/config/TTAdManagerHolder", "showVideo", "(I)V", pop);
  48. this.adSdk.showVideoAd();
  49. }
  50. else if (cc.sys.os == cc.sys.OS_IOS) {
  51. jsb.reflection.callStaticMethod("AppController", "showVideo", "");
  52. }
  53. }
  54. else {
  55. // WxMgr.getInstance().showRewardAd(pop)
  56. }
  57. }
  58. }
  59. /** 观看广告失败 */
  60. public watchAdFail() {
  61. mk.console.log("=== watchAdFail");
  62. this.videoAdcallBack(false);
  63. }
  64. /** 观看广告取消 */
  65. public watchAdCancel() {
  66. mk.console.log("=== watchAdCancel");
  67. this.videoAdcallBack(false);
  68. }
  69. /**
  70. * 观看广告成功
  71. * @param placementId 广告位id
  72. */
  73. public wacthAdSuccess(placementId = '') {
  74. mk.console.log("=== wacthAdSuccess");
  75. this.videoAdcallBack(true);
  76. this.initLastTimeStamp();
  77. }
  78. /** 显示原生广告 */
  79. public showNativeAd(type = 0, show = true) {
  80. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  81. if (cc.sys.os == cc.sys.OS_ANDROID) {
  82. // jsb.reflection.callStaticMethod("org/cocos2dx/javascript/config/TTAdManagerHolder", "showVideo", "(I)V", pop);
  83. this.adSdk.loadNativeAd(type, show);
  84. }
  85. else if (cc.sys.os == cc.sys.OS_IOS) {
  86. }
  87. }
  88. else {
  89. // WxMgr.getInstance().showRewardAd(pop)
  90. }
  91. }
  92. /** 销毁原生广告 */
  93. public destroyNativeAd() {
  94. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  95. if (cc.sys.os == cc.sys.OS_ANDROID) {
  96. // jsb.reflection.callStaticMethod("org/cocos2dx/javascript/config/TTAdManagerHolder", "showVideo", "(I)V", pop);
  97. this.adSdk.destroyNativeAd();
  98. }
  99. else if (cc.sys.os == cc.sys.OS_IOS) {
  100. }
  101. }
  102. else {
  103. // WxMgr.getInstance().showRewardAd(pop)
  104. }
  105. }
  106. /** 上次播放视频时间戳,单位:毫秒 */
  107. private last_time_stamp: number = 0;
  108. private initLastTimeStamp() {
  109. let new_date = new Date();
  110. this.last_time_stamp = new_date.getTime();
  111. }
  112. /**
  113. * 视频是否冷却
  114. * @returns 视频冷却中
  115. */
  116. private isAdCooling(): boolean {
  117. let new_date = new Date();
  118. let new_time_stamp = new_date.getTime();
  119. let gap_time_stamp = new_time_stamp - this.last_time_stamp;
  120. if (gap_time_stamp <= 5000) {
  121. const time = 6 - Math.ceil(gap_time_stamp / 1000);
  122. mk.tip.pop('当前看视频太频繁了,请' + time + '秒后再试');
  123. return true;
  124. }
  125. return false;
  126. }
  127. public showBannerAd() {
  128. if (!this.ifShowAd) {
  129. return;
  130. }
  131. this.adSdk.showBannerAd();
  132. }
  133. /**
  134. * showInterAd
  135. * @param type
  136. */
  137. public showInterAd(type: number) {
  138. if (!this.ifShowAd) {
  139. return;
  140. }
  141. this.adSdk.showInter();
  142. }
  143. /**
  144. * 根据几率检测弹出插屏
  145. */
  146. public checkShowInterByChance() {
  147. let random = Math.random();
  148. if (random < 0.5) {
  149. this.showInterAd(0);
  150. }
  151. }
  152. }