AdSystem.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. //初始化Topon广告SDK
  24. this.adSdk.initSDK();
  25. }
  26. /**
  27. * 观看广告
  28. * @param callBack 广告相关的回调
  29. */
  30. public watchAd(callBack: Function) {
  31. if (this.isAdCooling()) return;
  32. this.videoAdcallBack = callBack;
  33. if (!this.ifShowAd) {
  34. this.wacthAdSuccess()
  35. }
  36. else {
  37. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  38. if (cc.sys.os == cc.sys.OS_ANDROID) {
  39. // jsb.reflection.callStaticMethod("org/cocos2dx/javascript/config/TTAdManagerHolder", "showVideo", "(I)V", pop);
  40. this.adSdk.showVideoAd();
  41. }
  42. else if (cc.sys.os == cc.sys.OS_IOS) {
  43. jsb.reflection.callStaticMethod("AppController", "showVideo", "");
  44. }
  45. }
  46. else {
  47. // WxMgr.getInstance().showRewardAd(pop)
  48. }
  49. }
  50. }
  51. /** 观看广告失败 */
  52. public watchAdFail() {
  53. mk.console.log("watchAdFail");
  54. this.videoAdcallBack(false);
  55. }
  56. /** 观看广告取消 */
  57. public watchAdCancel() {
  58. mk.console.log("watchAdCancel");
  59. this.videoAdcallBack(false);
  60. }
  61. /**
  62. * 观看广告成功
  63. * @param placementId 广告位id
  64. */
  65. public wacthAdSuccess(placementId = '') {
  66. mk.console.log("wacthAdSuccess");
  67. this.videoAdcallBack(true);
  68. this.initLastTimeStamp();
  69. }
  70. /** 显示原生广告 */
  71. public showNativeAd(type = 0, show = true) {
  72. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  73. if (cc.sys.os == cc.sys.OS_ANDROID) {
  74. // jsb.reflection.callStaticMethod("org/cocos2dx/javascript/config/TTAdManagerHolder", "showVideo", "(I)V", pop);
  75. this.adSdk.loadNativeAd(type, show);
  76. }
  77. else if (cc.sys.os == cc.sys.OS_IOS) {
  78. }
  79. }
  80. else {
  81. // WxMgr.getInstance().showRewardAd(pop)
  82. }
  83. }
  84. /** 销毁原生广告 */
  85. public destroyNativeAd() {
  86. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  87. if (cc.sys.os == cc.sys.OS_ANDROID) {
  88. // jsb.reflection.callStaticMethod("org/cocos2dx/javascript/config/TTAdManagerHolder", "showVideo", "(I)V", pop);
  89. this.adSdk.destroyNativeAd();
  90. }
  91. else if (cc.sys.os == cc.sys.OS_IOS) {
  92. }
  93. }
  94. else {
  95. // WxMgr.getInstance().showRewardAd(pop)
  96. }
  97. }
  98. /** 上次播放视频时间戳,单位:毫秒 */
  99. private last_time_stamp: number = 0;
  100. private initLastTimeStamp() {
  101. let new_date = new Date();
  102. this.last_time_stamp = new_date.getTime();
  103. }
  104. /**
  105. * 视频是否冷却
  106. * @returns 视频冷却中
  107. */
  108. private isAdCooling(): boolean {
  109. let new_date = new Date();
  110. let new_time_stamp = new_date.getTime();
  111. let gap_time_stamp = new_time_stamp - this.last_time_stamp;
  112. if (gap_time_stamp <= 5000) {
  113. const time = 6 - Math.ceil(gap_time_stamp / 1000);
  114. mk.tip.pop('当前看视频太频繁了,请' + time + '秒后再试');
  115. return true;
  116. }
  117. return false;
  118. }
  119. }