AdAgentFaceBook.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import AdAgent from "./AdAgent";
  2. import { BannerLocation } from "./YZ_Constant";
  3. import PlatUtils from "./PlatUtils";
  4. import { utils } from "./Utils";
  5. import FBAdManager from "./FaceBookSdk/FBAdManager";
  6. const { ccclass, property } = cc._decorator;
  7. @ccclass
  8. export default class AdAgentFaceBook extends AdAgent {
  9. _sysData: any = null;
  10. _curBannerAd: any = null;
  11. _videoAd: any = null;
  12. _insertAd: any = null;
  13. _oldAd: any = null;
  14. _appBox: any = null; //游戏盒子广告
  15. _videoCallback: Function = null;
  16. _isVideoLoaded: boolean = false;
  17. _isVideoShow: boolean = false;
  18. //@ts-ignore
  19. qq: any = window.qq;
  20. public Init() {
  21. if (PlatUtils.IsFaceBook) {
  22. utils.registerServerInitEvent(() => {
  23. this._initAd();
  24. }, this);
  25. }
  26. }
  27. public get ServerConfig() {
  28. return utils.Tool_Facebook.ServerConfig;
  29. }
  30. _initAd() {
  31. utils.showLog("facebook init ad>>>>>>>>")
  32. FBAdManager.addInterstitial(utils.config.faceBookConfig.insertId, 3);
  33. FBAdManager.addRewardedVideo(utils.config.faceBookConfig.videoId, 3);
  34. FBAdManager.addBanner(utils.config.faceBookConfig.bannerId);
  35. setTimeout(() => {
  36. FBAdManager.loadAll();
  37. }, 3000);
  38. }
  39. _bannerShow: boolean = false;
  40. _bannerSizePercent: number = 0.5;
  41. _bannerBottom: number = 0;
  42. _oldBannerLocation: BannerLocation = BannerLocation.None;
  43. _curBannerHeight: number = 240;
  44. _moveBtn: cc.Node = null;
  45. _cur_level: any = null;
  46. _createBanner(location: BannerLocation, args: any = null) {
  47. if (PlatUtils.IsFaceBook) {
  48. FBAdManager.showBannerAsync().then(() => {
  49. utils.showLog("显示Banner广告: 成功");
  50. }).catch(e => {
  51. utils.showLog("显示Banner广告: 失败,原因: " + e.message);
  52. });
  53. }
  54. }
  55. _showBannerTimer(location: BannerLocation, args: any) {
  56. let locationTmp: BannerLocation = location;
  57. let argsTmp: any = args;
  58. utils.showLog(`显示Banner广告xxx!location:${locationTmp}; 间隔时间:${utils.Tool_Facebook.ServerConfig.refresh_ad_time}:优先级:${this.ServerConfig.banner_first_ad}`);
  59. this._createBanner(locationTmp, argsTmp)
  60. }
  61. _showBannerTimerId: number = 0;
  62. public ShowBanner(location: BannerLocation = BannerLocation.Home, args: any = null) {
  63. if (PlatUtils.IsFaceBook) {
  64. if (utils.ServerConfig) {
  65. let locationTmp: BannerLocation = location;
  66. let argsTmp: any = args;
  67. this._moveBtn = args ? args.moveBtn : null;
  68. this._cur_level = args ? args.cur_level : null;
  69. this._showBannerTimer(locationTmp, argsTmp);
  70. } else {
  71. utils.showLog("服务器配置数据未初始化!");
  72. }
  73. }
  74. }
  75. public HideBanner(location: BannerLocation = BannerLocation.Home) {
  76. if (PlatUtils.IsFaceBook) {
  77. utils.showLog("隐藏广告条");
  78. this._bannerShow = false;
  79. clearInterval(this._showBannerTimerId);
  80. FBAdManager.hideBannerAsync().then(() => {
  81. utils.showLog("隐藏Banner广告: 成功");
  82. }).catch(e => {
  83. utils.showLog("隐藏Banner广告: 失败,原因: " + e.message);
  84. });
  85. }
  86. }
  87. public ShowVideo(callback: Function) {
  88. if (PlatUtils.IsFaceBook) {
  89. this._videoCallback = callback;
  90. if (FBAdManager.isRewardedVideoReady()) {
  91. FBAdManager.showRewardedVideo().then(() => {
  92. utils.showLog("播放激励视频广告: 成功");
  93. if (this._videoCallback) {
  94. this._videoCallback(true, "");
  95. this._videoCallback = null;
  96. }
  97. }).catch(e => {
  98. utils.showLog("视频播放失败:" + e.message);
  99. if (this._videoCallback) {
  100. this._videoCallback(false, "Ad playback failed!");
  101. this._videoCallback = null;
  102. }
  103. });
  104. } else {
  105. utils.showLog("激励视频广告未加载!");
  106. if (this._videoCallback) {
  107. this._videoCallback(false, "Video ad not loaded successfully!");
  108. this._videoCallback = null;
  109. }
  110. }
  111. }
  112. }
  113. /**
  114. * 显示插屏
  115. * 2001 触发频率限制 小程序启动一定时间内不允许展示插屏广告
  116. * 2002 触发频率限制 距离小程序插屏广告或者激励视频广告上次播放时间间隔不足,不允许展示插屏广告
  117. * 2003 触发频率限制 当前正在播放激励视频广告或者插屏广告,不允许再次展示插屏广告
  118. * 2004 广告渲染失败 该项错误不是开发者的异常情况,或因小程序页面切换导致广告渲染失败
  119. * 2005 广告调用异常 插屏广告实例不允许跨页面调用
  120. * 销毁插屏广告后才能重新创建
  121. * @param location
  122. */
  123. public ShowInterstitial(location: BannerLocation = null) {
  124. if (PlatUtils.IsFaceBook) {
  125. if (this.ServerConfig) {
  126. utils.delayCall(this._createInsterstitial.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  127. }else{
  128. this._createInsterstitial.bind(this)
  129. }
  130. }
  131. }
  132. _isShow: boolean = false;
  133. public _createInsterstitial() {
  134. if (FBAdManager.isInterstitialAdReady()) {
  135. FBAdManager.showInterstitialAd().then(() => {
  136. utils.showLog("播放插屏广告: 成功");
  137. }).catch(e => {
  138. utils.showLog("播放插屏广告: 失败,原因: " + e.message);
  139. });
  140. } else {
  141. utils.showLog("插屏广告没有加载成功!")
  142. }
  143. }
  144. }