AdAgentBili.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. /**
  7. * bili广告组件
  8. */
  9. @ccclass
  10. export default class AdAgentBili extends AdAgent {
  11. _videoCallback: Function = null;
  12. _isVideoLoaded: boolean = false;
  13. _videoAd: any = null;
  14. _bannerAd: any = null;
  15. //@ts-ignore
  16. bl = window.bl;
  17. public Init() {
  18. }
  19. _curBannerAd: any = null;
  20. _bannerSizePercent: number = 0.1;
  21. _bannerBottom: number = 0;
  22. public ShowBanner(location: BannerLocation, args: any = null) {
  23. let argsTmp: any = args;
  24. if (argsTmp && argsTmp.width) {
  25. this._bannerSizePercent = argsTmp.width < 0 ? 0 : argsTmp.width;
  26. this._bannerSizePercent = argsTmp.width > 1 ? 1 : argsTmp.width;
  27. }
  28. if (argsTmp && argsTmp.bottom) {
  29. this._bannerBottom = argsTmp.bottom < 0 ? 0 : argsTmp.bottom;
  30. this._bannerBottom = argsTmp.bottom > cc.winSize.height ? cc.winSize.height : argsTmp.bottom;
  31. }
  32. // let bannerId: string = utils.config.biliConfig.bannerId;
  33. // utils.showLog("显示Banner广告: bannerId=" + bannerId);
  34. console.log("显示Banner广告");
  35. let params = {
  36. left: 0,
  37. width: 300,
  38. top: 0
  39. };
  40. //@ts-ignore
  41. let curBannerAd = bl.createBannerAd(params);
  42. if (curBannerAd) {
  43. curBannerAd.onError((err) => {
  44. utils.showLog("广告条加载失败! ", JSON.stringify(err));
  45. if (curBannerAd) {
  46. curBannerAd.destroy();
  47. }
  48. });
  49. let self = this;
  50. curBannerAd.onLoad(() => {
  51. curBannerAd.show().then(() => {
  52. if (self._curBannerAd) {
  53. self._curBannerAd.destroy();
  54. }
  55. self._curBannerAd = curBannerAd;
  56. utils.showLog("Banner广告显示成功!");
  57. }).catch((err) => {
  58. utils.showLog("Banner广告出错", JSON.stringify(err));
  59. if (curBannerAd) {
  60. curBannerAd.destroy();
  61. }
  62. });
  63. });
  64. curBannerAd.onResize((res) => {
  65. curBannerAd.style.width = utils.Tool_Bili.SysInfo.screenWidth * self._bannerSizePercent;
  66. curBannerAd.style.left = (utils.Tool_Bili.SysInfo.screenWidth - res.width) * 0.5;
  67. if (self._bannerBottom == cc.winSize.height) {
  68. curBannerAd.style.top = 0;
  69. } else {
  70. curBannerAd.style.top = utils.Tool_Bili.SysInfo.screenHeight - res.height - self._bannerBottom;
  71. }
  72. });
  73. } else {
  74. utils.showLog("广告条创建失败!");
  75. }
  76. }
  77. public ShowInterstitial() {
  78. utils.showLog("哔哩平台暂时无插屏广告");
  79. }
  80. public HideBanner(location: BannerLocation = BannerLocation.Home) {
  81. if (PlatUtils.IsBili) {
  82. if (this._bannerAd) {
  83. this._bannerAd.hide();
  84. }
  85. }
  86. }
  87. public ShowVideo(callback: Function) {
  88. if (PlatUtils.IsBili) {
  89. this._videoCallback = callback;
  90. let self = this;
  91. this.showRewardedVideoAd({
  92. onSuccess(res) {
  93. // 广告成功展示
  94. utils.showLog("bili视频展示成功!");
  95. },
  96. onFail(e) {
  97. // 广告失败了
  98. utils.showLog("bili视频广告展示失败!");
  99. utils.Tool_Bili.share((res, msg) => {
  100. if (res) {
  101. self._videoCallback && self._videoCallback(true, "");
  102. self._videoCallback = null;
  103. } else {
  104. self._videoCallback && self._videoCallback(false, "分享成功才可以获取奖励哦!");
  105. self._videoCallback = null;
  106. }
  107. });
  108. },
  109. onClose(e) {
  110. // 用户关闭了广告
  111. if (e.isEnded) {
  112. // 用户看完了
  113. utils.showLog("bili视频广告观看完毕,发送奖励");
  114. self._videoCallback && self._videoCallback(true, "");
  115. } else {
  116. // 用户没看完
  117. self._videoCallback && self._videoCallback(false, "观看完视频才能获得奖励!");
  118. self._videoCallback = null;
  119. }
  120. }
  121. });
  122. }
  123. }
  124. private showRewardedVideoAd({ onSuccess, onFail, onClose }) {
  125. // 创建激励视频对象实例:
  126. const ad = this.bl.createRewardedVideoAd();
  127. function adLoadHandler(e) {
  128. // !!!!!!
  129. // 重要!此处必须先 off 本次事件回调。
  130. // !!!!!!
  131. ad.offLoad(adLoadHandler);
  132. // 加载成功
  133. ad.show()
  134. .catch((e) => {
  135. // 激励视频展示失败
  136. if (typeof onFail === 'function') {
  137. onFail(e);
  138. }
  139. return Promise.reject(e);
  140. })
  141. .then((res) => {
  142. // 成功展示激励视频
  143. if (typeof onSuccess === 'function') {
  144. onSuccess(res);
  145. }
  146. })
  147. }
  148. function adErrorHandler(e) {
  149. // 激励广告出错
  150. if (typeof onFail === 'function') {
  151. onFail(e);
  152. }
  153. ad.offLoad(adLoadHandler);
  154. ad.offClose(adCloseHandler);
  155. ad.offError(adErrorHandler);
  156. ad.destroy();
  157. }
  158. function adCloseHandler(e) {
  159. // 用户点击“关闭广告”
  160. if (typeof onClose === 'function') {
  161. onClose(e);
  162. }
  163. ad.offLoad(adLoadHandler);
  164. ad.offClose(adCloseHandler);
  165. ad.offError(adErrorHandler);
  166. ad.destroy();
  167. }
  168. // 监听加载成功事件
  169. ad.offLoad(adLoadHandler);
  170. ad.onLoad(adLoadHandler);
  171. // 监听错误事件
  172. ad.offError(adErrorHandler);
  173. ad.onError(adErrorHandler);
  174. // 监听用户关闭事件
  175. ad.offClose(adCloseHandler);
  176. ad.onClose(adCloseHandler);
  177. // 加载
  178. ad.load();
  179. return ad;
  180. }
  181. }