ecaa7333-0781-4d52-8682-a702e9c6df98.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. "use strict";
  2. cc._RF.push(module, 'ecaa7MzB4FNUoaCpwLpxt+Y', 'AdAgentBili');
  3. // common-plugin/Scripts/AdAgentBili.ts
  4. "use strict";
  5. var __extends = (this && this.__extends) || (function () {
  6. var extendStatics = function (d, b) {
  7. extendStatics = Object.setPrototypeOf ||
  8. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  9. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  10. return extendStatics(d, b);
  11. };
  12. return function (d, b) {
  13. extendStatics(d, b);
  14. function __() { this.constructor = d; }
  15. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  16. };
  17. })();
  18. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  19. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  20. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  21. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  22. return c > 3 && r && Object.defineProperty(target, key, r), r;
  23. };
  24. Object.defineProperty(exports, "__esModule", { value: true });
  25. var AdAgent_1 = require("./AdAgent");
  26. var YZ_Constant_1 = require("./YZ_Constant");
  27. var PlatUtils_1 = require("./PlatUtils");
  28. var Utils_1 = require("./Utils");
  29. var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
  30. /**
  31. * bili广告组件
  32. */
  33. var AdAgentBili = /** @class */ (function (_super) {
  34. __extends(AdAgentBili, _super);
  35. function AdAgentBili() {
  36. var _this = _super !== null && _super.apply(this, arguments) || this;
  37. _this._videoCallback = null;
  38. _this._isVideoLoaded = false;
  39. _this._videoAd = null;
  40. _this._bannerAd = null;
  41. //@ts-ignore
  42. _this.bl = window.bl;
  43. _this._curBannerAd = null;
  44. _this._bannerSizePercent = 0.1;
  45. _this._bannerBottom = 0;
  46. return _this;
  47. }
  48. AdAgentBili.prototype.Init = function () {
  49. };
  50. AdAgentBili.prototype.ShowBanner = function (location, args) {
  51. if (args === void 0) { args = null; }
  52. var argsTmp = args;
  53. if (argsTmp && argsTmp.width) {
  54. this._bannerSizePercent = argsTmp.width < 0 ? 0 : argsTmp.width;
  55. this._bannerSizePercent = argsTmp.width > 1 ? 1 : argsTmp.width;
  56. }
  57. if (argsTmp && argsTmp.bottom) {
  58. this._bannerBottom = argsTmp.bottom < 0 ? 0 : argsTmp.bottom;
  59. this._bannerBottom = argsTmp.bottom > cc.winSize.height ? cc.winSize.height : argsTmp.bottom;
  60. }
  61. // let bannerId: string = utils.config.biliConfig.bannerId;
  62. // utils.showLog("显示Banner广告: bannerId=" + bannerId);
  63. console.log("显示Banner广告");
  64. var params = {
  65. left: 0,
  66. width: 300,
  67. top: 0
  68. };
  69. //@ts-ignore
  70. var curBannerAd = bl.createBannerAd(params);
  71. if (curBannerAd) {
  72. curBannerAd.onError(function (err) {
  73. Utils_1.utils.showLog("广告条加载失败! ", JSON.stringify(err));
  74. if (curBannerAd) {
  75. curBannerAd.destroy();
  76. }
  77. });
  78. var self_1 = this;
  79. curBannerAd.onLoad(function () {
  80. curBannerAd.show().then(function () {
  81. if (self_1._curBannerAd) {
  82. self_1._curBannerAd.destroy();
  83. }
  84. self_1._curBannerAd = curBannerAd;
  85. Utils_1.utils.showLog("Banner广告显示成功!");
  86. }).catch(function (err) {
  87. Utils_1.utils.showLog("Banner广告出错", JSON.stringify(err));
  88. if (curBannerAd) {
  89. curBannerAd.destroy();
  90. }
  91. });
  92. });
  93. curBannerAd.onResize(function (res) {
  94. curBannerAd.style.width = Utils_1.utils.Tool_Bili.SysInfo.screenWidth * self_1._bannerSizePercent;
  95. curBannerAd.style.left = (Utils_1.utils.Tool_Bili.SysInfo.screenWidth - res.width) * 0.5;
  96. if (self_1._bannerBottom == cc.winSize.height) {
  97. curBannerAd.style.top = 0;
  98. }
  99. else {
  100. curBannerAd.style.top = Utils_1.utils.Tool_Bili.SysInfo.screenHeight - res.height - self_1._bannerBottom;
  101. }
  102. });
  103. }
  104. else {
  105. Utils_1.utils.showLog("广告条创建失败!");
  106. }
  107. };
  108. AdAgentBili.prototype.ShowInterstitial = function () {
  109. Utils_1.utils.showLog("哔哩平台暂时无插屏广告");
  110. };
  111. AdAgentBili.prototype.HideBanner = function (location) {
  112. if (location === void 0) { location = YZ_Constant_1.BannerLocation.Home; }
  113. if (PlatUtils_1.default.IsBili) {
  114. if (this._bannerAd) {
  115. this._bannerAd.hide();
  116. }
  117. }
  118. };
  119. AdAgentBili.prototype.ShowVideo = function (callback) {
  120. if (PlatUtils_1.default.IsBili) {
  121. this._videoCallback = callback;
  122. var self_2 = this;
  123. this.showRewardedVideoAd({
  124. onSuccess: function (res) {
  125. // 广告成功展示
  126. Utils_1.utils.showLog("bili视频展示成功!");
  127. },
  128. onFail: function (e) {
  129. // 广告失败了
  130. Utils_1.utils.showLog("bili视频广告展示失败!");
  131. Utils_1.utils.Tool_Bili.share(function (res, msg) {
  132. if (res) {
  133. self_2._videoCallback && self_2._videoCallback(true, "");
  134. self_2._videoCallback = null;
  135. }
  136. else {
  137. self_2._videoCallback && self_2._videoCallback(false, "分享成功才可以获取奖励哦!");
  138. self_2._videoCallback = null;
  139. }
  140. });
  141. },
  142. onClose: function (e) {
  143. // 用户关闭了广告
  144. if (e.isEnded) {
  145. // 用户看完了
  146. Utils_1.utils.showLog("bili视频广告观看完毕,发送奖励");
  147. self_2._videoCallback && self_2._videoCallback(true, "");
  148. }
  149. else {
  150. // 用户没看完
  151. self_2._videoCallback && self_2._videoCallback(false, "观看完视频才能获得奖励!");
  152. self_2._videoCallback = null;
  153. }
  154. }
  155. });
  156. }
  157. };
  158. AdAgentBili.prototype.showRewardedVideoAd = function (_a) {
  159. var onSuccess = _a.onSuccess, onFail = _a.onFail, onClose = _a.onClose;
  160. // 创建激励视频对象实例:
  161. var ad = this.bl.createRewardedVideoAd();
  162. function adLoadHandler(e) {
  163. // !!!!!!
  164. // 重要!此处必须先 off 本次事件回调。
  165. // !!!!!!
  166. ad.offLoad(adLoadHandler);
  167. // 加载成功
  168. ad.show()
  169. .catch(function (e) {
  170. // 激励视频展示失败
  171. if (typeof onFail === 'function') {
  172. onFail(e);
  173. }
  174. return Promise.reject(e);
  175. })
  176. .then(function (res) {
  177. // 成功展示激励视频
  178. if (typeof onSuccess === 'function') {
  179. onSuccess(res);
  180. }
  181. });
  182. }
  183. function adErrorHandler(e) {
  184. // 激励广告出错
  185. if (typeof onFail === 'function') {
  186. onFail(e);
  187. }
  188. ad.offLoad(adLoadHandler);
  189. ad.offClose(adCloseHandler);
  190. ad.offError(adErrorHandler);
  191. ad.destroy();
  192. }
  193. function adCloseHandler(e) {
  194. // 用户点击“关闭广告”
  195. if (typeof onClose === 'function') {
  196. onClose(e);
  197. }
  198. ad.offLoad(adLoadHandler);
  199. ad.offClose(adCloseHandler);
  200. ad.offError(adErrorHandler);
  201. ad.destroy();
  202. }
  203. // 监听加载成功事件
  204. ad.offLoad(adLoadHandler);
  205. ad.onLoad(adLoadHandler);
  206. // 监听错误事件
  207. ad.offError(adErrorHandler);
  208. ad.onError(adErrorHandler);
  209. // 监听用户关闭事件
  210. ad.offClose(adCloseHandler);
  211. ad.onClose(adCloseHandler);
  212. // 加载
  213. ad.load();
  214. return ad;
  215. };
  216. AdAgentBili = __decorate([
  217. ccclass
  218. ], AdAgentBili);
  219. return AdAgentBili;
  220. }(AdAgent_1.default));
  221. exports.default = AdAgentBili;
  222. cc._RF.pop();