AdSystem.ts 4.1 KB

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