AdAgentBaidu.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 AdAgentBaidu extends AdAgent {
  8. _sysData: any = null;
  9. _baiduVersion: string = "";
  10. _recorder: any = null;
  11. _bannerAd: any = null;
  12. _videoAd: any = null;
  13. _videoCallback: Function = null;
  14. _isVideoShow: boolean = false;
  15. _isVideoLoaded: boolean = false;
  16. //@ts-ignore
  17. swan: any = window.swan;
  18. public Init() {
  19. if (PlatUtils.IsBaidu) {
  20. this._sysData = this.swan.getSystemInfoSync();
  21. utils.registerServerInitEvent(() => {
  22. this._initVideoAd();
  23. }, this)
  24. }
  25. }
  26. _bannerSizePercent: number = 0.1;
  27. _bannerBottom: number = 0;
  28. public ShowBanner(location: BannerLocation = BannerLocation.Home, args: any = null) {
  29. if (PlatUtils.IsBaidu) {
  30. if (utils.isShowRecommondGamesBanner() && utils.isSupportnavigateToMiniGame()) {
  31. utils.showRecommendGamesBanner();
  32. utils.showLog(`服务器配置展示自定义banner`);
  33. return;
  34. }
  35. utils.showLog("显示banner广告...");
  36. let argsTmp = args;
  37. if (argsTmp && argsTmp.width) {
  38. if (cc.winSize.height / cc.winSize.width < 1) {
  39. this._bannerSizePercent = argsTmp.width;
  40. } else {
  41. this._bannerSizePercent = ((argsTmp.width <= 0.8) ? 0.8 : argsTmp.width);
  42. }
  43. this._bannerSizePercent = ((argsTmp.width > 1) ? 1 : this._bannerSizePercent);
  44. }
  45. if (argsTmp && argsTmp.bottom) {
  46. this._bannerBottom = argsTmp.bottom / this._sysData.pixelRatio;
  47. this._bannerBottom = ((this._bannerBottom < 0) ? 0 : this._bannerBottom);
  48. this._bannerBottom = ((this._bannerBottom > this._sysData.screenHeight) ? this._sysData.screenHeight : this._bannerBottom);
  49. }
  50. // banner 广告
  51. if (utils.config.baiduconfig
  52. && utils.config.baiduconfig.bannerId
  53. && utils.config.baiduconfig.appSID) {
  54. let left: number = (this._sysData.screenWidth - this._sysData.screenWidth * this._bannerSizePercent) / 2;
  55. let width: number = this._sysData.screenWidth * this._bannerSizePercent;
  56. let bannerOpts = {
  57. adUnitId: utils.config.baiduconfig.bannerId,
  58. appSid: utils.config.baiduconfig.appSID,
  59. style: {
  60. top: 0,
  61. left: left,
  62. width: width
  63. }
  64. };
  65. let bannerAd: any = null;
  66. let oldBannerAd: any = this._bannerAd;
  67. let onLoadFunc = () => {
  68. if (bannerAd) {
  69. bannerAd.show().then(() => {
  70. this._bannerAd = bannerAd;
  71. if (oldBannerAd) {
  72. oldBannerAd.destroy();
  73. oldBannerAd.offLoad(onLoadFunc);
  74. oldBannerAd.offError(onErrorFunc);
  75. }
  76. utils.showLog("Banner显示成功!");
  77. }).catch(() => {
  78. utils.showLog("Banner显示出错!");
  79. });
  80. }
  81. };
  82. let onErrorFunc = (err) => {
  83. if (err) {
  84. utils.showLog("Banner 广告出错 : ", err.errCode, err.errMsg);
  85. }
  86. };
  87. let onResizeFunc = (res) => {
  88. bannerAd.style.width = width;
  89. bannerAd.style.top = this._sysData.screenHeight - res.height - this._bannerBottom;
  90. };
  91. bannerAd = this.swan.createBannerAd(bannerOpts);
  92. if (bannerAd) {
  93. bannerAd.onLoad(onLoadFunc);
  94. bannerAd.onError(onErrorFunc);
  95. bannerAd.onResize(onResizeFunc);
  96. bannerAd.style.width = width + 1;
  97. if (PlatUtils.IsIOS) {
  98. bannerAd.style.top = this._sysData.screenHeight;
  99. }
  100. }
  101. } else {
  102. cc.warn("百度广告配置文件出错!");
  103. }
  104. }
  105. }
  106. /**
  107. * 显示结算广告
  108. * @param data 参数: closeBtn:
  109. * statement_type
  110. * 1:只显示小游戏插屏广告
  111. * 2:只显示6个互推广告
  112. * 3:显示插屏广告+6个互推
  113. */
  114. showStatementAds(): any {
  115. let result: any = { "type": 0, "node": null };
  116. let node: cc.Node = null;
  117. let resType: number = 0;
  118. utils.showLog("结算广告 >> 显示插屏广告+6个互推");
  119. this.ShowInterstitial();
  120. node = utils.showCrossWidget6();
  121. resType = 1;
  122. result.type = resType;
  123. result.node = node;
  124. return result;
  125. }
  126. public HideBanner(location: BannerLocation = BannerLocation.Home) {
  127. if (PlatUtils.IsBaidu) {
  128. if (this._bannerAd) {
  129. this._bannerAd.hide();
  130. }
  131. }
  132. }
  133. public ShowInterstitial(location: BannerLocation = BannerLocation.Home) {
  134. console.warn("百度没有插屏");
  135. }
  136. public ShowVideo(callback: Function) {
  137. if (PlatUtils.IsBaidu) {
  138. // 视频广告
  139. if (this._videoCallback) {
  140. return;
  141. } else {
  142. this._videoCallback = callback;
  143. }
  144. this._isVideoShow = true;
  145. if (!this._videoAd) {
  146. this._initVideoAd();
  147. } else {
  148. if (this._isVideoLoaded) {
  149. this._videoAd.show().then(() => {
  150. utils.showLog("视频显示成功!");
  151. this._isVideoLoaded = false;
  152. }).catch((err) => {
  153. });
  154. } else {
  155. this._videoAd.load().catch((err) => {
  156. });
  157. }
  158. }
  159. }
  160. }
  161. _initVideoAd() {
  162. if (!this._videoAd) {
  163. if (!(utils.config.baiduconfig
  164. && utils.config.baiduconfig.appSID
  165. && utils.config.baiduconfig.videoId)) {
  166. utils.showLog("视频ID配置不正确!");
  167. if (this._videoCallback) {
  168. this._videoCallback(false, "暂无视频广告!");
  169. this._videoCallback = null;
  170. }
  171. return;
  172. }
  173. let videoOpts = {
  174. adUnitId: utils.config.baiduconfig.videoId,
  175. appSid: utils.config.baiduconfig.appSID
  176. };
  177. utils.showLog("视频广告参数:", JSON.stringify(videoOpts));
  178. this._videoAd = this.swan.createRewardedVideoAd(videoOpts);
  179. if (this._videoAd) {
  180. this._videoAd.onLoad(() => {
  181. utils.showLog("视频加载成功");
  182. this._isVideoLoaded = true;
  183. if (this._isVideoShow) {
  184. this._videoAd.show().then(() => {
  185. this._isVideoLoaded = false;
  186. }).catch(() => {
  187. utils.showLog("视频播放失败!");
  188. });
  189. }
  190. });
  191. this._videoAd.onClose((res) => {
  192. this._isVideoShow = false;
  193. if (res && res.isEnded) {
  194. utils.showLog("正常播放结束,可以下发游戏奖励");
  195. if (this._videoCallback) {
  196. this._videoCallback(true, "");
  197. this._videoCallback = null;
  198. }
  199. } else {
  200. utils.showLog("播放中途退出,不下发游戏奖励");
  201. if (this._videoCallback) {
  202. this._videoCallback(false, "观看完视频才能获得奖励!");
  203. this._videoCallback = null;
  204. }
  205. }
  206. });
  207. this._videoAd.onError((err) => {
  208. utils.showLog("激励视频异常!", err.errCode);
  209. this._isVideoLoaded = false;
  210. if (this._videoCallback) {
  211. this._videoCallback(false, "暂无视频广告!");
  212. this._videoCallback = null;
  213. }
  214. });
  215. }
  216. }
  217. }
  218. }