AdSystem.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import { BannerAdType, InterAdType, InterFullAdType, VideoAdType } from "../../game/data/GameData";
  2. import { IADSDKMgr } from "../sdk/ad/IADSDKMgr";
  3. import JsbSystem from "./JsbSystem";
  4. /**
  5. * 广告管理类脚本
  6. * @description 显示/播放广告
  7. * @author 冯聪
  8. */
  9. export default class AdSystem {
  10. /** 构造函数
  11. * @param adSdk 传入当前使用广告sdk
  12. */
  13. constructor(adSdk: IADSDKMgr) {
  14. this.adSdk = adSdk;
  15. // this.init();
  16. }
  17. /** 广告sdk */
  18. private adSdk: IADSDKMgr = null;
  19. /** 是否显示广告 */
  20. public ifShowAd: boolean = true;
  21. /** 视频广告类型 */
  22. public videoAdType: VideoAdType = VideoAdType.video_init_1;
  23. /** 视频广告回调 */
  24. public videoAdcallBack: Function = null;
  25. /** 全屏插屏广告类型 */
  26. public interFullAdType: InterFullAdType = InterFullAdType.interstitial2_init_1;
  27. /** 插屏广告类型 */
  28. public interAdType: InterAdType = InterAdType.interstitial1_click_1;
  29. /** banner广告类型 */
  30. public bannerAdType: BannerAdType = BannerAdType.banner_click_1;
  31. /** 初始化广告 */
  32. public init() {
  33. //判定下平台
  34. if (cc.sys.os == cc.sys.OS_ANDROID) {
  35. this.ifShowAd = gData.gameData.configs.ServerConfig.ifShowAd == '1';
  36. }
  37. else {
  38. this.ifShowAd = false;
  39. //test
  40. //this.ifShowAd = gData.gameData.configs.ServerConfig.ifShowAd == '1';
  41. }
  42. //初始化Topon广告SDK
  43. //this.adSdk.initSDK();
  44. }
  45. /**
  46. * 观看广告
  47. * @param callBack 广告相关的回调
  48. */
  49. public watchAd(callBack: Function) {
  50. if (this.isAdCooling()) {
  51. return;
  52. }
  53. this.videoAdcallBack = callBack;
  54. if (!this.ifShowAd) {
  55. this.wacthAdSuccess()
  56. }
  57. else {
  58. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  59. if (cc.sys.os == cc.sys.OS_ANDROID) {
  60. //this.adSdk.showVideo();
  61. JsbSystem.showVideo();
  62. }
  63. else if (cc.sys.os == cc.sys.OS_IOS) {
  64. jsb.reflection.callStaticMethod("AppController", "showVideo", "");
  65. }
  66. }
  67. else {
  68. // WxMgr.getInstance().showRewardAd(pop)
  69. }
  70. }
  71. }
  72. /** 观看广告失败 */
  73. public watchAdFail() {
  74. mk.console.log("=== watchAdFail");
  75. this.videoAdcallBack(false);
  76. }
  77. /** 观看广告取消 */
  78. public watchAdCancel() {
  79. mk.console.log("=== watchAdCancel");
  80. this.videoAdcallBack(false);
  81. }
  82. /**
  83. * 观看广告成功
  84. * @param placementId 广告位id
  85. */
  86. public wacthAdSuccess(placementId = '') {
  87. mk.console.log("=== wacthAdSuccess");
  88. //累计视频次数
  89. mk.data.setTAEventUser(1, 'video_time', 1);
  90. //广告过后,渲染会有问题,统一下一帧再处理逻辑
  91. mk.ui.scheduleOnce(() => {
  92. if (!gData.adData.checkAdMax()) {
  93. this.videoAdcallBack(true);
  94. }
  95. this.initLastTimeStamp();
  96. }, 0)
  97. }
  98. /** 显示原生广告 */
  99. public showNative(type = 4) {
  100. if (!this.ifShowAd) {
  101. return;
  102. }
  103. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  104. if (cc.sys.os == cc.sys.OS_ANDROID) {
  105. this.adSdk.showNative(type);
  106. }
  107. else if (cc.sys.os == cc.sys.OS_IOS) {
  108. }
  109. }
  110. else {
  111. // WxMgr.getInstance().showRewardAd(pop)
  112. }
  113. }
  114. /** 销毁原生广告 */
  115. public destroyNativeAd() {
  116. if (!this.ifShowAd) {
  117. return;
  118. }
  119. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  120. if (cc.sys.os == cc.sys.OS_ANDROID) {
  121. this.adSdk.destroyNative();
  122. }
  123. else if (cc.sys.os == cc.sys.OS_IOS) {
  124. }
  125. }
  126. else {
  127. // WxMgr.getInstance().showRewardAd(pop)
  128. }
  129. }
  130. /** 上次播放视频时间戳,单位:毫秒 */
  131. private last_time_stamp: number = 0;
  132. private initLastTimeStamp() {
  133. let new_date = new Date();
  134. this.last_time_stamp = new_date.getTime();
  135. }
  136. /**
  137. * 视频是否冷却
  138. * @returns 视频冷却中
  139. */
  140. private isAdCooling(): boolean {
  141. let new_date = new Date();
  142. let new_time_stamp = new_date.getTime();
  143. let gap_time_stamp = new_time_stamp - this.last_time_stamp;
  144. if (gap_time_stamp <= 3000) {
  145. const time = 4 - Math.ceil(gap_time_stamp / 1000);
  146. mk.tip.pop('当前看视频太频繁了,请' + time + '秒后再试');
  147. return true;
  148. }
  149. return false;
  150. }
  151. public showBanner(bannerAdType: BannerAdType) {
  152. if (CC_DEBUG) {
  153. return;
  154. }
  155. if (!this.ifShowAd) {
  156. return;
  157. }
  158. this.bannerAdType = bannerAdType;
  159. mk.console.logSingle('banner', 'showBanner');
  160. // console.log("===[AdSystem gameData.adShowConfig", gData.gameData.adShowConfig.is_show_banner);
  161. // if (gData.gameData.adShowConfig && gData.gameData.adShowConfig.is_show_banner == 1) {
  162. this.adSdk.showBanner();
  163. //}
  164. }
  165. public destoryBanner() {
  166. if (CC_DEBUG) {
  167. return;
  168. }
  169. if (!this.ifShowAd) {
  170. return;
  171. }
  172. this.adSdk.destoryBanner();
  173. }
  174. /**
  175. * showInterAd
  176. * @param type 0 插屏 1 全屏 2 大插屏
  177. * @param des 广告类型
  178. */
  179. public showInterAd(type: number, des = null) {
  180. if (CC_DEBUG) {
  181. return;
  182. }
  183. if (!this.ifShowAd) {
  184. return;
  185. }
  186. if (type == 1) {
  187. if (des as InterFullAdType) {
  188. this.interFullAdType = des;
  189. }
  190. }
  191. else {
  192. if (des as InterAdType) {
  193. this.interAdType = des;
  194. }
  195. }
  196. this.adSdk.showInter(type);
  197. }
  198. }