AdAgentWiFi.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import AdAgent from "./AdAgent";
  2. import { BannerLocation } from "./YZ_Constant";
  3. import PlatUtils from "./PlatUtils";
  4. import { utils } from "./Utils";
  5. const { ccclass, property } = cc._decorator;
  6. @ccclass
  7. export default class AdAgentWiFi extends AdAgent {
  8. _sysData: any = null;
  9. _curBannerAd: any = null;
  10. _videoAd: any = null;
  11. _insertAd: any = null;
  12. _oldAd: any = null;
  13. _appBox: any = null; //游戏盒子广告
  14. _videoCallback: Function = null;
  15. _isVideoLoaded: boolean = false;
  16. _isVideoShow: boolean = false;
  17. //@ts-ignore
  18. wuji: any = window.wuji;
  19. public Init() {
  20. if (PlatUtils.IsWiFi) {
  21. utils.registerServerInitEvent(() => {
  22. this._sysData = utils.wifiTool.getSystemInfo();
  23. }, this);
  24. }
  25. }
  26. public get ServerConfig() {
  27. return utils.wifiTool.ServerConfig;
  28. }
  29. _initVideoAd() {
  30. if (!this._videoAd) {
  31. utils.showLog("初始化视频!");
  32. var example = {
  33. adUnitId: '', // 该字段必传,值可以先为空
  34. }
  35. this._videoAd = this.wuji.createRewardedVideoAd(example);
  36. this._videoAd.onLoad(() => {
  37. utils.showLog('激励视频加载完成');
  38. this._videoAd
  39. .show()
  40. .then(() => utils.showLog('激励视频展示成功'))
  41. .catch(err => {
  42. utils.showLog('激励视频展示失败', err);
  43. // 可以手动加载一次
  44. this._videoAd
  45. .load()
  46. .then(() => {
  47. utils.showLog("手动加载成功");
  48. // 加载成功后需要再显示广告
  49. return this._videoAd.show();
  50. });
  51. });
  52. });
  53. this._videoAd.onError((err) => {
  54. utils.showLog('激励视频错误', err);
  55. if (this._videoCallback) {
  56. this._videoCallback(false, "暂无视频广告!");
  57. this._videoCallback = null;
  58. }
  59. });
  60. this._videoAd.onClose((res) => {
  61. this._isVideoShow = false;
  62. if (res.isEnded) {
  63. // 正常播放结束,可以下发游戏奖励
  64. utils.showLog('激励视频广告完成,发放奖励');
  65. if (this._videoCallback) {
  66. this._videoCallback(true, "");
  67. this._videoCallback = null;
  68. }
  69. } else {
  70. // 播放中途退出,不下发游戏奖励
  71. utils.showLog('激励视频广告取消关闭,不发放奖励');
  72. if (this._videoCallback) {
  73. this._videoCallback(false, "观看完视频才能获得奖励!");
  74. this._videoCallback = null;
  75. }
  76. }
  77. });
  78. }
  79. }
  80. _createBanner(location: BannerLocation, args: any = null) {
  81. if (PlatUtils.IsWiFi) {
  82. var { windowWidth, windowHeight } = this._sysData;
  83. var targetBannerAdWidth = windowWidth - 20;
  84. var example = {
  85. adUnitId: '', // 该字段必传,但是值可以先为空
  86. style: {
  87. top: windowHeight, // 置于屏幕底部
  88. left: (windowWidth - targetBannerAdWidth) / 2, // 居中
  89. width: targetBannerAdWidth,
  90. //height: 200, // 宽高比是固定的,height值可不传
  91. }
  92. }
  93. this._curBannerAd = this.wuji.createBannerAd(example);
  94. this._curBannerAd.onLoad(() => {
  95. this._curBannerAd.show()
  96. .then(() => utils.showLog('banner ad 展示成功'))
  97. .catch(err => utils.showLog(err));
  98. })
  99. }
  100. }
  101. _showBannerTimer(location: BannerLocation, args: any) {
  102. let locationTmp: BannerLocation = location;
  103. let argsTmp: any = args;
  104. this._createBanner(locationTmp, argsTmp)
  105. }
  106. ShowInterstitial() {
  107. utils.showLog("连尚小游戏没有插屏广告!");
  108. }
  109. _bannerShow: boolean = true;
  110. _showBannerTimerId: number = 0;
  111. public ShowBanner(location: BannerLocation = BannerLocation.Home, args: any = null) {
  112. if (PlatUtils.IsWiFi) {
  113. if (this.ServerConfig) {
  114. let locationTmp: BannerLocation = location;
  115. let argsTmp: any = args;
  116. let interval: number = 30;
  117. if (this.ServerConfig.refresh_ad_time) {
  118. interval = this.ServerConfig.refresh_ad_time;
  119. }
  120. utils.showLog(`显示Banner广告!location:${locationTmp}; args:${JSON.stringify(argsTmp)}; 间隔时间:${this.ServerConfig.refresh_ad_time}`);
  121. var { windowWidth, windowHeight } = this._sysData;
  122. var targetBannerAdWidth = windowWidth - 20;
  123. var example = {
  124. adUnitId: '', // 该字段必传,但是值可以先为空
  125. adIntervals: interval,
  126. style: {
  127. top: windowHeight, // 置于屏幕底部
  128. left: (windowWidth - targetBannerAdWidth) / 2, // 居中
  129. width: targetBannerAdWidth,
  130. //height: 200, // 宽高比是固定的,height值可不传
  131. }
  132. }
  133. if (this._curBannerAd) {
  134. this._curBannerAd.offLoad();
  135. this._curBannerAd.destroy();
  136. }
  137. this._curBannerAd = this.wuji.createBannerAd(example);
  138. this._curBannerAd.onLoad(() => {
  139. this._curBannerAd.show()
  140. .then(() => utils.showLog('banner ad 展示成功'))
  141. .catch(err => utils.showLog(err));
  142. })
  143. } else {
  144. utils.showLog("服务器配置数据未初始化!");
  145. }
  146. }
  147. }
  148. public HideBanner(location: BannerLocation = BannerLocation.Home) {
  149. if (PlatUtils.IsWiFi) {
  150. utils.showLog("隐藏广告条");
  151. this._bannerShow = false;
  152. clearInterval(this._showBannerTimerId);
  153. if (this._curBannerAd) {
  154. this._curBannerAd.hide();
  155. }
  156. }
  157. }
  158. public ShowVideo(callback: Function) {
  159. if (PlatUtils.IsWiFi) {
  160. this._videoCallback = callback;
  161. this._isVideoShow = true;
  162. if (!this._videoAd) {
  163. this._initVideoAd();
  164. } else {
  165. this._videoAd.load();
  166. }
  167. }
  168. }
  169. }