8d81df2d-4e4f-490a-994f-8b4100cb6ffe.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. "use strict";
  2. cc._RF.push(module, '8d81d8tTk9JCplPi0EAy2/+', 'AdAgentCocosplay');
  3. // common-plugin/Scripts/AdAgentCocosplay.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 Utils_1 = require("./Utils");
  27. var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
  28. /**
  29. * uc广告组件
  30. */
  31. var AdAgentCocosplay = /** @class */ (function (_super) {
  32. __extends(AdAgentCocosplay, _super);
  33. function AdAgentCocosplay() {
  34. var _this = _super !== null && _super.apply(this, arguments) || this;
  35. _this.banner = null;
  36. _this.interstitialAd = {};
  37. _this.videoAd = {};
  38. _this.bannerLoaded = false;
  39. _this.interstitialAdLoaded = false;
  40. _this.videoAdLoaded = false;
  41. _this._videoCallback = null;
  42. _this._isVideoLoaded = false;
  43. _this._videoAd = null;
  44. _this._bannerAd = null;
  45. //@ts-ignore
  46. _this.uc = window.uc;
  47. //插屏显示次数
  48. _this._insertShowCount = 0;
  49. _this._insertLastShowTime = 0;
  50. return _this;
  51. }
  52. AdAgentCocosplay.prototype.Init = function () {
  53. var _this = this;
  54. Utils_1.utils.registerServerInitEvent(function () {
  55. _this.initBanner();
  56. _this.createInsertAd();
  57. _this.createVideoAd();
  58. }, this);
  59. };
  60. AdAgentCocosplay.prototype.initBanner = function () {
  61. var self = this;
  62. self.bannerLoaded = false;
  63. //@ts-ignore
  64. self.banner = AdSDK.createBannerAd("1", Utils_1.utils.config.cocosConfig.bannerId, 2);
  65. //注册onLoad函数,游戏调用创建banner时,AdSDK通知平台创建广告,当创建成功,会执行该回调函数通知游戏
  66. self.banner.onLoad(function () {
  67. self.bannerLoaded = true;
  68. console.log("banner 创建banner成功,可以调用展示");
  69. });
  70. //注册onError函数,游戏调用创建banner时,AdSDK通知平台创建广告,当创建失败,会执行该回调函数通知游戏
  71. self.banner.onError(function (param) {
  72. self.destroyBannerAd();
  73. console.log("banner 创建banner失败,错误码 = ", param.errorCode);
  74. });
  75. console.log('banner 游戏创建banner广告');
  76. };
  77. AdAgentCocosplay.prototype.ShowBanner = function () {
  78. var self = this;
  79. // self.createBannerAd()
  80. // self.bannerLoaded = false;
  81. if (self.bannerLoaded) { //Object.keys(banner).length &&
  82. self.banner.show().then(function () {
  83. console.log('banner 广告显示成功');
  84. }, function (err) {
  85. console.log('banner 广告显示失败');
  86. });
  87. console.log('banner 展示banner广告');
  88. }
  89. else {
  90. console.log('banner 未加载成功');
  91. }
  92. };
  93. AdAgentCocosplay.prototype.HideBanner = function () {
  94. this.banner.hide();
  95. console.log('banner 隐藏banner广告');
  96. };
  97. AdAgentCocosplay.prototype.destroyBannerAd = function () {
  98. this.bannerLoaded = false;
  99. this.banner.destroy();
  100. this.banner = null;
  101. console.log('banner 销毁banner广告');
  102. };
  103. Object.defineProperty(AdAgentCocosplay.prototype, "ServerConfig", {
  104. get: function () {
  105. return Utils_1.utils._tool_Cocosplay.ServerConfig;
  106. },
  107. enumerable: false,
  108. configurable: true
  109. });
  110. /**
  111. * 验证插屏是否能展示
  112. * 1、次数限制 默认每日8次
  113. * 2、时间限制 默认60秒
  114. */
  115. AdAgentCocosplay.prototype.checkInsertAdShow = function () {
  116. var maxShowCount = this.ServerConfig.intersititial_max_show_count;
  117. maxShowCount = 0;
  118. var intervalTime = this.ServerConfig.intersititial_interval_time;
  119. var curTime = new Date().getTime();
  120. var interval = (curTime - this._insertLastShowTime) / 1000;
  121. Utils_1.utils.showLog("cocos服务器插屏最大显示次数为:" + maxShowCount + ",间隔显示时间为:" + intervalTime + "秒!");
  122. Utils_1.utils.showLog("cocos插屏当前广告显示次数:" + this._insertShowCount + "次,间隔时间:" + interval + "秒!");
  123. if (maxShowCount > 0 && this._insertShowCount >= maxShowCount) {
  124. Utils_1.utils.showLog("cocos插屏广告显示的次数达到" + maxShowCount + "次。插屏不显示");
  125. return false;
  126. }
  127. if (intervalTime > 0 && interval < intervalTime) {
  128. Utils_1.utils.showLog("cocos插屏广告显示的间隔少于" + intervalTime + "秒。插屏不显示");
  129. return false;
  130. }
  131. return true;
  132. };
  133. // 创建插屏广告 展示一次调用一次创建
  134. // 参数
  135. // adId: string 广告序号ID 游戏自定义
  136. // interstitialAdId: string 插屏广告ID 需后台申请
  137. // style: int 广告类型 1 全屏 2 半屏
  138. AdAgentCocosplay.prototype.ShowInterstitial = function () {
  139. var self = this;
  140. if (!self.checkInsertAdShow())
  141. return;
  142. if (self.interstitialAdLoaded) {
  143. self.interstitialAd.show().then(function () {
  144. self._insertLastShowTime = new Date().getTime();
  145. self.destroyInterstitialAd();
  146. self.createInsertAd();
  147. }, function (err) {
  148. console.log("interstitialAd 广告显示失败");
  149. });
  150. console.log("interstitialAd 游戏展示插屏广告");
  151. }
  152. };
  153. AdAgentCocosplay.prototype.createInsertAd = function () {
  154. var self = this;
  155. Utils_1.utils.showLog("插屏id为:" + Utils_1.utils.config.cocosConfig.insertId);
  156. //@ts-ignore
  157. this.interstitialAd = AdSDK.createInterstitialAd("1", Utils_1.utils.config.cocosConfig.insertId, 2);
  158. this.interstitialAd.onLoad(function () {
  159. self.interstitialAdLoaded = true;
  160. console.log("interstitialAd 创建插屏成功,可以调用展示");
  161. });
  162. self.interstitialAd.onError(function (param) {
  163. console.log("interstitialAd 创建插屏失败,错误码 = ", param.errorCode);
  164. self.destroyInterstitialAd();
  165. });
  166. };
  167. AdAgentCocosplay.prototype.hideInterstitialAd = function () {
  168. this.interstitialAd.hide();
  169. console.log("interstitialAd 游戏隐藏插屏广告");
  170. };
  171. AdAgentCocosplay.prototype.destroyInterstitialAd = function () {
  172. this.interstitialAdLoaded = false;
  173. this.interstitialAd.destroy();
  174. console.log("interstitialAd 游戏销毁插屏广告");
  175. };
  176. // 创建激励视频广告 展示一次创建一次
  177. // 参数
  178. // adId: string 广告序号ID 游戏自定义
  179. // videoAdId: string 视频广告ID 需后台申请
  180. // screenOrientation: int 广告类型 1 横屏 2 竖屏
  181. AdAgentCocosplay.prototype.ShowVideo = function (callback) {
  182. var self = this;
  183. self._videoCallback = callback;
  184. if (self.videoAdLoaded) {
  185. self.videoAd.show().then(function () {
  186. if (self._videoCallback) {
  187. self._videoCallback(true, "视频播放成功");
  188. self._videoCallback = null;
  189. }
  190. }, function (err) {
  191. if (self._videoCallback) {
  192. self._videoCallback(false, "暂无视频广告");
  193. self._videoCallback = null;
  194. }
  195. });
  196. }
  197. else {
  198. if (self._videoCallback) {
  199. self._videoCallback(false, "暂无视频广告");
  200. self._videoCallback = null;
  201. }
  202. self.createVideoAd();
  203. }
  204. };
  205. AdAgentCocosplay.prototype.createVideoAd = function () {
  206. var self = this;
  207. console.log("视频id为:" + Utils_1.utils.config.cocosConfig.videoId);
  208. //@ts-ignore
  209. self.videoAd = AdSDK.createRewardedVideoAd("1", Utils_1.utils.config.cocosConfig.videoId, 2);
  210. self.videoAd.onLoad(function () {
  211. self.videoAdLoaded = true;
  212. console.log("rewardedvideoAd 创建视频广告成功,可以调用展示");
  213. });
  214. self.videoAd.onError(function (param) {
  215. console.log("rewardedvideoAd 创建视频广告失败,错误码 = ", param.errorCode);
  216. self.destroyRewardedVideoAd(); //加载广告失败,销毁
  217. if (self._videoCallback) {
  218. self._videoCallback(false, "暂无视频广告");
  219. self._videoCallback = null;
  220. }
  221. });
  222. self.videoAd.onClose(function () {
  223. if (self._videoCallback) {
  224. self._videoCallback(false, "观看完视频才能获得奖励!");
  225. self._videoCallback = null;
  226. }
  227. self.destroyRewardedVideoAd(); //视频广告关闭,销毁
  228. self.createVideoAd();
  229. });
  230. };
  231. AdAgentCocosplay.prototype.destroyRewardedVideoAd = function () {
  232. this.videoAdLoaded = false;
  233. this.videoAd.destroy();
  234. console.log("rewardedvideoAd 游戏销毁视频广告");
  235. };
  236. AdAgentCocosplay = __decorate([
  237. ccclass
  238. ], AdAgentCocosplay);
  239. return AdAgentCocosplay;
  240. }(AdAgent_1.default));
  241. exports.default = AdAgentCocosplay;
  242. cc._RF.pop();