AdSystem.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. return;
  101. if (!this.ifShowAd) {
  102. return;
  103. }
  104. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  105. if (cc.sys.os == cc.sys.OS_ANDROID) {
  106. this.adSdk.showNative(type);
  107. }
  108. else if (cc.sys.os == cc.sys.OS_IOS) {
  109. }
  110. }
  111. else {
  112. // WxMgr.getInstance().showRewardAd(pop)
  113. }
  114. }
  115. /** 销毁原生广告 */
  116. public destroyNativeAd() {
  117. return;
  118. if (!this.ifShowAd) {
  119. return;
  120. }
  121. if (cc.sys.platform != cc.sys.WECHAT_GAME) {
  122. if (cc.sys.os == cc.sys.OS_ANDROID) {
  123. this.adSdk.destroyNative();
  124. }
  125. else if (cc.sys.os == cc.sys.OS_IOS) {
  126. }
  127. }
  128. else {
  129. // WxMgr.getInstance().showRewardAd(pop)
  130. }
  131. }
  132. /** 上次播放视频时间戳,单位:毫秒 */
  133. private last_time_stamp: number = 0;
  134. private initLastTimeStamp() {
  135. let new_date = new Date();
  136. this.last_time_stamp = new_date.getTime();
  137. }
  138. /**
  139. * 视频是否冷却
  140. * @returns 视频冷却中
  141. */
  142. private isAdCooling(): boolean {
  143. let new_date = new Date();
  144. let new_time_stamp = new_date.getTime();
  145. let gap_time_stamp = new_time_stamp - this.last_time_stamp;
  146. if (gap_time_stamp <= 3000) {
  147. const time = 4 - Math.ceil(gap_time_stamp / 1000);
  148. mk.tip.pop('当前看视频太频繁了,请' + time + '秒后再试');
  149. return true;
  150. }
  151. return false;
  152. }
  153. public showBanner(bannerAdType: BannerAdType) {
  154. return;
  155. if (CC_DEBUG) {
  156. return;
  157. }
  158. if (!this.ifShowAd) {
  159. return;
  160. }
  161. this.bannerAdType = bannerAdType;
  162. mk.console.logSingle('banner', 'showBanner');
  163. // console.log("===[AdSystem gameData.adShowConfig", gData.gameData.adShowConfig.is_show_banner);
  164. // if (gData.gameData.adShowConfig && gData.gameData.adShowConfig.is_show_banner == 1) {
  165. this.adSdk.showBanner();
  166. //}
  167. }
  168. public destoryBanner() {
  169. return;
  170. if (CC_DEBUG) {
  171. return;
  172. }
  173. if (!this.ifShowAd) {
  174. return;
  175. }
  176. this.adSdk.destoryBanner();
  177. }
  178. /**
  179. * showInterAd
  180. * @param type 0 插屏 1 全屏 2 大插屏
  181. * @param des 广告类型
  182. */
  183. public showInterAd(type: number, des = null) {
  184. return;
  185. if (CC_DEBUG) {
  186. return;
  187. }
  188. if (!this.ifShowAd) {
  189. return;
  190. }
  191. if (type == 1) {
  192. if (des as InterFullAdType) {
  193. this.interFullAdType = des;
  194. }
  195. }
  196. else {
  197. if (des as InterAdType) {
  198. this.interAdType = des;
  199. }
  200. }
  201. this.adSdk.showInter(type);
  202. }
  203. }