fdbd3267-8e53-447f-8b19-ba5b13c8636f.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. "use strict";
  2. cc._RF.push(module, 'fdbd3JnjlNEf4sZulsTyGNv', 'AdAgentXiaomi');
  3. // common-plugin/Scripts/AdAgentXiaomi.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 CompatibleTool_1 = require("./CompatibleTool");
  30. var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
  31. var AdAgentXiaoMi = /** @class */ (function (_super) {
  32. __extends(AdAgentXiaoMi, _super);
  33. function AdAgentXiaoMi() {
  34. var _this = _super !== null && _super.apply(this, arguments) || this;
  35. _this._bannerAd = null;
  36. _this._insertAd = null;
  37. _this._videoAd = null;
  38. _this._nativeBannerAd = [];
  39. _this._nativeInsertAd = [];
  40. _this._nativeSingleAd = [];
  41. _this.nativeBannerAd = null;
  42. _this._isInsertShow = false;
  43. // 当前位置id的索引
  44. _this._curPosIdIndexNativeBanner = 0;
  45. _this._curPosIdIndexNativeInser = 0;
  46. _this._curPosIdIndexSingleNative = 0;
  47. _this._nativeData = null;
  48. _this._nativeInsertData = null;
  49. _this._nativeBannerNode = null;
  50. _this._nativeInsertNode = null;
  51. _this._videoCallback = null;
  52. _this._videoLoaded = null;
  53. _this._isBannerShow = false;
  54. _this._isInsertAdShow = false;
  55. _this._isInsertAdLoaded = false;
  56. _this._isVideoLoaded = false;
  57. _this._isVideoShow = false;
  58. _this.lastLastShowVideoTime = 0; //最后一次显示视频时间
  59. _this.showNum = 0; // Banner广告展示次数
  60. _this.showInsertNum = 0; // 插屏广告展示次数
  61. _this._showBannerCallBack = null;
  62. _this._isNativeBannerShow = false; //原生广告展示,调用的隐藏
  63. _this._nativeIsClose = false;
  64. _this._showBannerTimerId = 0;
  65. _this._delayShowBannerId = 0;
  66. _this.canShowNativeBanner = true; //原生banner是否展示
  67. // nbclr:是否开启强制刷新
  68. //当前显示Banner的位置
  69. _this._curLocation = YZ_Constant_1.BannerLocation.None;
  70. _this._isTimeRefresh = false;
  71. //启动定时器的时间
  72. _this._startBannerTimerTask = 0;
  73. _this._showBannerCount = 0;
  74. _this._nativeBannerInfo = null;
  75. _this._curNativeBannerInfo = {};
  76. _this._nativeInsertAdShowCount = 0;
  77. _this._insertLastShowTime = 0;
  78. return _this;
  79. }
  80. Object.defineProperty(AdAgentXiaoMi.prototype, "ServerConfig", {
  81. get: function () {
  82. if (PlatUtils_1.default.IsXiaoMi) {
  83. return Utils_1.utils.Tool_XiaoMi.ServerConfig;
  84. }
  85. return null;
  86. },
  87. enumerable: false,
  88. configurable: true
  89. });
  90. AdAgentXiaoMi.prototype.Init = function () {
  91. var _this = this;
  92. if (PlatUtils_1.default.IsXiaoMi) {
  93. Utils_1.utils.registerServerInitEvent(function () {
  94. _this._initVideoAd();
  95. }, this);
  96. }
  97. };
  98. AdAgentXiaoMi.prototype._initVideoAd = function () {
  99. var _this = this;
  100. if (!this._videoAd) {
  101. if (!Utils_1.utils.config.xiaomiConfig.videoId) {
  102. Utils_1.utils.showLog("视频ID配置不正确!");
  103. if (this._videoCallback) {
  104. this._videoCallback(false, "暂无视频广告!");
  105. this._videoCallback = null;
  106. }
  107. return;
  108. }
  109. var posId = Utils_1.utils.config.xiaomiConfig.videoId.trim();
  110. Utils_1.utils.showLog("video广告ID:" + posId);
  111. //@ts-ignore
  112. this._videoAd = qg.createRewardedVideoAd({
  113. adUnitId: posId
  114. });
  115. if (this._videoAd) {
  116. this._videoAd.onLoad((function () {
  117. Utils_1.utils.showLog("激励视频加载成功!");
  118. _this._isVideoLoaded = true;
  119. }));
  120. this._videoAd.onError((function (err) {
  121. Utils_1.utils.showLog("激励视频异常!" + ("error: errorMsg: " + err.errMsg + ", erroCode: " + err.errCode));
  122. _this._isVideoLoaded = false;
  123. }));
  124. this._videoAd.onClose(function (res) {
  125. _this._isVideoShow = false;
  126. if (res && res.isEnded) {
  127. Utils_1.utils.showLog("正常播放结束,可以下发游戏奖励");
  128. if (_this._videoCallback) {
  129. _this._videoCallback(true, "");
  130. _this._videoCallback = null;
  131. }
  132. }
  133. else {
  134. Utils_1.utils.showLog("播放中途退出,不下发游戏奖励");
  135. if (_this._videoCallback) {
  136. _this._videoCallback(false, "观看完视频才能获得奖励!");
  137. _this._videoCallback = null;
  138. }
  139. }
  140. });
  141. // this._videoAd.load();
  142. }
  143. }
  144. };
  145. AdAgentXiaoMi.prototype.ShowBanner = function (location, args, isTimeRefresh) {
  146. var _this = this;
  147. if (location === void 0) { location = null; }
  148. if (args === void 0) { args = null; }
  149. if (isTimeRefresh === void 0) { isTimeRefresh = false; }
  150. if (PlatUtils_1.default.IsXiaoMi) {
  151. if (!this.ServerConfig) {
  152. Utils_1.utils.showLog("未请求到配置文件!");
  153. return;
  154. }
  155. if (this.ServerConfig && this.ServerConfig.isTest && this.ServerConfig.isTest == "true") {
  156. this.showNum++;
  157. if (this.showNum % 2 == 0) {
  158. Utils_1.utils.showLog("测试模式>> 顺序展示原生Banner广告!");
  159. this._createNativeBannerAd(this._showNativeBanner);
  160. }
  161. else {
  162. Utils_1.utils.showLog("测试模式>> 顺序展示默认Banner广告!");
  163. this._createMiniGameBannerAd(location);
  164. }
  165. return;
  166. }
  167. if (!this.checkBannerAdShow()) {
  168. this.HideBanner();
  169. return;
  170. }
  171. this._showBannerCount++;
  172. var oldLocation = this._curLocation;
  173. this._curLocation = location;
  174. this._isTimeRefresh = isTimeRefresh;
  175. //如果跳用位置切换之后,验证是不是通过定时器强制刷新数据,不是则先隐藏banner
  176. if (oldLocation != location) {
  177. this.HideBanner(location);
  178. }
  179. var curTime = new Date().getTime();
  180. var refresh_ad_time = (curTime - this._startBannerTimerTask) / 1000;
  181. var interval_1 = this.ServerConfig.refresh_ad_time;
  182. // console.log("isTimeRefresh=" + isTimeRefresh);
  183. if (args && args.isRefresh) {
  184. isTimeRefresh = true;
  185. }
  186. else if (interval_1 && interval_1 > 0) {
  187. if (refresh_ad_time > interval_1) {
  188. isTimeRefresh = true;
  189. }
  190. }
  191. // 判断当前位置是不是显示banner
  192. if (this.getNativeBannerInfo().is_show_banner == -1) {
  193. Utils_1.utils.showLog("当前位置配置为不展示banner!");
  194. this.HideBanner(location);
  195. return;
  196. }
  197. else {
  198. if (!isTimeRefresh) {
  199. if (this.ServerConfig.nbclr && this.ServerConfig.nbclr == "true" && this._curNativeBannerInfo.nativeBannerAd) {
  200. Utils_1.utils.showLog("服务器配置定时器刷新数据 ");
  201. Utils_1.utils.showLog("\u539F\u751Fbanner\u5EF6\u8FDF\u663E\u793A" + this.getNativeBannerInfo().delay_show_time + "\u79D2");
  202. clearTimeout(this._showBannerTimerId);
  203. clearTimeout(this._delayShowBannerId);
  204. //@ts-ignore
  205. this._delayShowBannerId = setTimeout(function () {
  206. _this._showNativeBanner(_this._curNativeBannerInfo.nativeBannerAd, _this._curNativeBannerInfo.data);
  207. }, this.getNativeBannerInfo().delay_show_time * 1000);
  208. Utils_1.utils.showLog("开启定时刷新 >>>>>>>>>" + (interval_1 - refresh_ad_time));
  209. //@ts-ignore
  210. this._showBannerTimerId = setTimeout(function () {
  211. Utils_1.utils.showLog("\u5B9A\u65F6" + interval_1 + "\u79D2\u8C03\u7528showbanner");
  212. _this.ShowBanner(_this._curLocation, {}, true);
  213. }, (interval_1 - refresh_ad_time) * 1000);
  214. return;
  215. }
  216. }
  217. }
  218. this.canShowNativeBanner = true;
  219. this._nativeIsClose = false;
  220. clearTimeout(this._showBannerTimerId);
  221. clearTimeout(this._delayShowBannerId);
  222. Utils_1.utils.showLog("清理定时器");
  223. if (this.ServerConfig) {
  224. Utils_1.utils.showLog("banner 优先展示 >>>>" + this.ServerConfig.banner_first_ad);
  225. if (interval_1 && interval_1 > 0) {
  226. Utils_1.utils.showLog("开启定时刷新 >>>>>>>>>" + interval_1);
  227. //@ts-ignore
  228. this._showBannerTimerId = setTimeout(function () {
  229. Utils_1.utils.showLog("\u5B9A\u65F6" + interval_1 + "\u79D2\u8C03\u7528showbanner");
  230. _this.ShowBanner(_this._curLocation, {}, true);
  231. }, interval_1 * 1000);
  232. }
  233. var closeCount = this.ServerConfig.banner_close_count ? this.ServerConfig.banner_close_count : 0;
  234. if (closeCount > 0 && Utils_1.utils.Tool_XiaoMi.bannerAdCloseCounts >= closeCount) {
  235. Utils_1.utils.showLog("banner \u5173\u95ED\u6B21\u6570\u8FBE\u5230" + closeCount + "\u6B21,banner\u4ECA\u65E5\u4E0D\u518D\u663E\u793A\uFF01");
  236. return;
  237. }
  238. if (this.ServerConfig.banner_first_ad) {
  239. if (this.ServerConfig.banner_first_ad.indexOf("native") > -1) {
  240. Utils_1.utils.showLog("优先展示原生Banner广告!" + isTimeRefresh + " <<<<");
  241. if (!this._isTimeRefresh) {
  242. Utils_1.utils.showLog("\u539F\u751Fbanner\u5EF6\u8FDF\u663E\u793A" + this.getNativeBannerInfo().delay_show_time + "\u79D2");
  243. //@ts-ignore
  244. this._delayShowBannerId = setTimeout(function () {
  245. _this._createNativeBannerAd(_this._showNativeBanner);
  246. }, this.getNativeBannerInfo().delay_show_time * 1000);
  247. }
  248. else {
  249. this._createNativeBannerAd(this._showNativeBanner);
  250. }
  251. }
  252. else {
  253. Utils_1.utils.showLog("优先展示小游戏Banner广告!");
  254. this._createMiniGameBannerAd(location);
  255. }
  256. }
  257. else {
  258. Utils_1.utils.showLog("配置数据中没有 banner_first_ad 字段, banner广告不显示!");
  259. }
  260. }
  261. else {
  262. Utils_1.utils.showLog("服务器配置数据未初始化!");
  263. }
  264. }
  265. };
  266. AdAgentXiaoMi.prototype.HideBanner = function (location) {
  267. if (location === void 0) { location = null; }
  268. if (PlatUtils_1.default.IsXiaoMi) {
  269. clearTimeout(this._showBannerTimerId);
  270. clearTimeout(this._delayShowBannerId);
  271. this._isBannerShow = false;
  272. this._nativeIsClose = true;
  273. if (this._bannerAd) {
  274. Utils_1.utils.showLog("隐藏小游戏Banner");
  275. this._bannerAd.hide();
  276. }
  277. this.canShowNativeBanner = false;
  278. if (this._nativeBannerNode) {
  279. Utils_1.utils.showLog("隐藏原生Banner");
  280. this._nativeBannerNode.active = false;
  281. }
  282. }
  283. };
  284. /**
  285. * 获取当前banner配置
  286. */
  287. AdAgentXiaoMi.prototype.getNativeBannerInfo = function () {
  288. if (this._nativeBannerInfo && this._nativeBannerInfo.location == this._curLocation) {
  289. return this._nativeBannerInfo;
  290. }
  291. return Utils_1.utils.config.xiaomiConfig.getNativeBannerInfo(this._curLocation);
  292. };
  293. AdAgentXiaoMi.prototype._createMiniGameBannerAd = function (location) {
  294. var _this = this;
  295. if (location === void 0) { location = null; }
  296. if (PlatUtils_1.default.IsXiaoMi) {
  297. this._isBannerShow = true;
  298. if (Utils_1.utils.config.xiaomiConfig
  299. && Utils_1.utils.config.xiaomiConfig.bannerId) {
  300. if (this._bannerAd) {
  301. this._bannerAd.destroy();
  302. }
  303. var left = (Utils_1.utils.Tool_XiaoMi.SysInfo.screenWidth - 385) * 0.5;
  304. if (cc.winSize.height < cc.winSize.width) {
  305. left = Utils_1.utils.Tool_XiaoMi.SysInfo.screenWidth * 0.5;
  306. }
  307. var bannerStyle = {
  308. left: left,
  309. top: Utils_1.utils.Tool_XiaoMi.SysInfo.screenHeight - 58,
  310. width: 385
  311. };
  312. if (this.getNativeBannerInfo()._alignType === "top") {
  313. bannerStyle.top = 0;
  314. }
  315. //@ts-ignore
  316. this._bannerAd = qg.createBannerAd({
  317. adUnitId: Utils_1.utils.config.xiaomiConfig.bannerId,
  318. style: bannerStyle
  319. });
  320. Utils_1.utils.showLog("createBannerAd:" + this._bannerAd);
  321. Utils_1.utils.showLog("注册小游戏banner回调!");
  322. this._bannerAd.onError(function (err) {
  323. Utils_1.utils.showLog("xiaomi 小游戏Banner广告出错: " + err.code + err.msg);
  324. var bannerInfo = _this.getNativeBannerInfo();
  325. if (bannerInfo.st_banner_show_back_up == -1) {
  326. Utils_1.utils.showLog("服务器配置不显示备用广告");
  327. return;
  328. }
  329. if (_this._isBannerShow) {
  330. _this._isBannerShow = false;
  331. if (_this.ServerConfig.banner_first_ad.indexOf("default") > -1) {
  332. _this._curPosIdIndexNativeBanner = 0;
  333. _this._createNativeBannerAd(_this._showNativeBanner.bind(_this));
  334. }
  335. }
  336. });
  337. this._bannerAd.onResize(function (res) {
  338. Utils_1.utils.showLog("xiaomi 小游戏Banner显示成功!");
  339. if (_this._nativeBannerNode) {
  340. _this._nativeBannerNode.active = false;
  341. }
  342. _this._showBannerCallBack && _this._showBannerCallBack();
  343. _this._showBannerCallBack = null;
  344. // utils.showLog("xiaomi 小游戏Banner onResize!" + JSON.stringify(res));
  345. // utils.showLog("xiaomi 小游戏Banner curStyle" + JSON.stringify(this._bannerAd.style));
  346. // this._bannerAd.style.width = utils.Tool_XiaoMi.SysInfo.screenWidth;
  347. _this._bannerAd.style.left = (Utils_1.utils.Tool_XiaoMi.SysInfo.screenWidth - res.width) * 0.5;
  348. if (_this.getNativeBannerInfo()._alignType == "top") {
  349. _this._bannerAd.style.top = 0;
  350. }
  351. else {
  352. _this._bannerAd.style.top = Utils_1.utils.Tool_XiaoMi.SysInfo.screenHeight - res.height;
  353. }
  354. // utils.showLog("xiaomi 小游戏Banner curStyle" + JSON.stringify(this._bannerAd.style));
  355. });
  356. this._bannerAd.onClose(function () {
  357. Utils_1.utils._bannerCloseTime = new Date().getTime();
  358. if (!_this._isNativeBannerShow) {
  359. Utils_1.utils.Tool_XiaoMi.countBannerCloseCount();
  360. Utils_1.utils.showLog("xiaomi 小游戏Banner 广告隐藏,当前隐藏次数>" + Utils_1.utils.Tool_XiaoMi.bannerAdCloseCounts);
  361. }
  362. else {
  363. _this._isNativeBannerShow = false;
  364. Utils_1.utils.showLog("xiaomi 小游戏Banner 广告隐藏,来自原生的显示,不增加隐藏次数,当前隐藏次数>" + Utils_1.utils.Tool_XiaoMi.bannerAdCloseCounts);
  365. }
  366. });
  367. if (this._bannerAd) {
  368. this._bannerAd.show();
  369. Utils_1.utils.showLog("xiaomi 小游戏Banner show");
  370. }
  371. else {
  372. Utils_1.utils.showLog("xiaomi 小游戏Banner广告创建失败!");
  373. if (this.ServerConfig.indexOf("default") > -1) {
  374. this._curPosIdIndexNativeBanner = 0;
  375. this._createNativeBannerAd(this._showNativeBanner.bind(this));
  376. }
  377. }
  378. }
  379. else {
  380. Utils_1.utils.showLog("xiaomi 小游戏Banner广告配置信息错误!");
  381. var bannerInfo = this.getNativeBannerInfo();
  382. if (bannerInfo.st_banner_show_back_up == -1) {
  383. Utils_1.utils.showLog("服务器配置不显示备用广告");
  384. return;
  385. }
  386. if (this.ServerConfig.banner_first_ad.indexOf("default") > -1) {
  387. this._curPosIdIndexNativeBanner = 0;
  388. this._createNativeBannerAd(this._showNativeBanner.bind(this));
  389. }
  390. }
  391. }
  392. };
  393. AdAgentXiaoMi.prototype._createNativeBannerAd = function (completeCallback) {
  394. var _this = this;
  395. if (PlatUtils_1.default.IsXiaoMi) {
  396. var callback_1 = completeCallback;
  397. Utils_1.utils.showLog("curPosIdIndexNativeBanner:" + this._curPosIdIndexNativeBanner);
  398. var nativeBannerAd_1 = this._nativeBannerAd[this._curPosIdIndexNativeBanner];
  399. if (!nativeBannerAd_1) {
  400. if (Utils_1.utils.config.xiaomiConfig.nativeBannerIds
  401. && Utils_1.utils.config.xiaomiConfig.nativeBannerIds[this._curPosIdIndexNativeBanner]) {
  402. Utils_1.utils.showLog("创建原生广告Banner。 posId:" + Utils_1.utils.config.xiaomiConfig.nativeBannerIds[this._curPosIdIndexNativeBanner]);
  403. //@ts-ignore
  404. nativeBannerAd_1 = qg.createNativeAd({
  405. adUnitId: Utils_1.utils.config.xiaomiConfig.nativeBannerIds[this._curPosIdIndexNativeBanner]
  406. });
  407. if (nativeBannerAd_1) {
  408. this._nativeBannerAd.push(nativeBannerAd_1);
  409. nativeBannerAd_1.onLoad(function (res) {
  410. if (_this._nativeIsClose) {
  411. Utils_1.utils.showLog("隐藏了BANNER,不做任何处理!");
  412. return;
  413. }
  414. if (res && res.adList && res.adList.length > 0) {
  415. _this._startBannerTimerTask = new Date().getTime();
  416. Utils_1.utils.showLog("原生Banner广告资源拉取成功!");
  417. Utils_1.utils.showLog(JSON.stringify(res));
  418. res = JSON.parse(JSON.stringify(res));
  419. var data = res.adList[0];
  420. //如果当前显示为结算banner,则不做数据验证
  421. if (_this._checkNativeDataValid(data) || _this.getNativeBannerInfo().show_st_banner == "true") {
  422. if (_this.canShowNativeBanner) {
  423. _this._curPosIdIndexNativeBanner = 0;
  424. _this._showNativeBanner(nativeBannerAd_1, data);
  425. _this._curNativeBannerInfo.nativeBannerAd = nativeBannerAd_1;
  426. _this._curNativeBannerInfo.data = data;
  427. // 删除当前广告
  428. if (_this._bannerAd) {
  429. _this._isNativeBannerShow = true;
  430. Utils_1.utils.showLog("隐藏小游戏Banner");
  431. _this._bannerAd.hide();
  432. }
  433. }
  434. else {
  435. Utils_1.utils.showLog("已经隐藏banner不可重复展示");
  436. }
  437. return;
  438. }
  439. }
  440. Utils_1.utils.showLog("原生Banner广告资源出错!");
  441. _this._curPosIdIndexNativeBanner++;
  442. if (_this._curPosIdIndexNativeBanner < Utils_1.utils.config.xiaomiConfig.nativeBannerIds.length) {
  443. _this._createNativeBannerAd(callback_1);
  444. }
  445. else {
  446. _this._curPosIdIndexNativeBanner = 0;
  447. // 原生广告遍历完毕
  448. Utils_1.utils.showLog("原生广告条遍历完毕,无法展示!");
  449. var bannerInfo = _this.getNativeBannerInfo();
  450. if (bannerInfo.st_banner_show_back_up == -1) {
  451. Utils_1.utils.showLog(">>>>>服务器配置不显示备用广告!");
  452. return;
  453. }
  454. if (_this.ServerConfig.banner_first_ad.indexOf("native") > -1) {
  455. Utils_1.utils.showLog("开始展示小游戏广告条!");
  456. _this._createMiniGameBannerAd();
  457. }
  458. }
  459. });
  460. nativeBannerAd_1.onError(function (err) {
  461. if (_this._nativeIsClose) {
  462. Utils_1.utils.showLog("隐藏了BANNER,不做任何处理!");
  463. return;
  464. }
  465. Utils_1.utils.showLog("原生Banner广告资源拉取失败!" + err.code + err.msg);
  466. _this._curPosIdIndexNativeBanner++;
  467. if (_this._curPosIdIndexNativeBanner < Utils_1.utils.config.xiaomiConfig.nativeBannerIds.length) {
  468. _this._createNativeBannerAd(callback_1);
  469. }
  470. else {
  471. _this._curPosIdIndexNativeBanner = 0;
  472. // 原生广告遍历完毕
  473. Utils_1.utils.showLog("原生广告条遍历完毕,无法展示!");
  474. var bannerInfo = _this.getNativeBannerInfo();
  475. if (bannerInfo.st_banner_show_back_up == -1) {
  476. Utils_1.utils.showLog(">>>>>服务器配置不显示备用广告!");
  477. return;
  478. }
  479. if (_this.ServerConfig.banner_first_ad.indexOf("native") > -1) {
  480. Utils_1.utils.showLog("开始展示小游戏广告条!");
  481. _this._createMiniGameBannerAd();
  482. }
  483. else {
  484. //显示自定义banner
  485. if (Utils_1.utils.isShowRecommondGamesBanner()) {
  486. clearTimeout(_this._showBannerTimerId);
  487. Utils_1.utils.showRecommendGamesBanner();
  488. Utils_1.utils.showLog("\u5C0F\u6E38\u620F\u3001\u539F\u751F\u5E7F\u544A\u6761\u90FD\u65E0\u6CD5\u5C55\u793A\uFF0C\u5C55\u793A\u81EA\u5B9A\u4E49banner");
  489. return;
  490. }
  491. }
  492. }
  493. });
  494. }
  495. }
  496. }
  497. if (nativeBannerAd_1) {
  498. Utils_1.utils.showLog("nativeBannerAd reLoad>>");
  499. nativeBannerAd_1.load();
  500. }
  501. else {
  502. this._curPosIdIndexNativeBanner++;
  503. if (Utils_1.utils.config.xiaomiConfig.nativeBannerIds && this._curPosIdIndexNativeBanner < Utils_1.utils.config.xiaomiConfig.nativeBannerIds.length) {
  504. this._createNativeBannerAd(callback_1);
  505. }
  506. else {
  507. this._curPosIdIndexNativeBanner = 0;
  508. // 原生广告遍历完毕
  509. Utils_1.utils.showLog("原生广告条遍历完毕,无法展示!");
  510. var bannerInfo = this.getNativeBannerInfo();
  511. if (bannerInfo.st_banner_show_back_up == -1) {
  512. Utils_1.utils.showLog(">>>>>服务器配置不显示备用广告!");
  513. return;
  514. }
  515. if (this.ServerConfig.banner_first_ad.indexOf("native") > -1) {
  516. Utils_1.utils.showLog("开始展示小游戏广告条!");
  517. this._createMiniGameBannerAd();
  518. }
  519. else {
  520. //显示自定义banner
  521. if (Utils_1.utils.isShowRecommondGamesBanner()) {
  522. clearTimeout(this._showBannerTimerId);
  523. Utils_1.utils.showRecommendGamesBanner();
  524. Utils_1.utils.showLog("\u5C0F\u6E38\u620F\u3001\u539F\u751F\u5E7F\u544A\u6761\u90FD\u65E0\u6CD5\u5C55\u793A\uFF0C\u5C55\u793A\u81EA\u5B9A\u4E49banner");
  525. return;
  526. }
  527. }
  528. }
  529. }
  530. }
  531. };
  532. /**
  533. * 显示视频广告
  534. * @param callback 视频回调
  535. */
  536. AdAgentXiaoMi.prototype.ShowVideo = function (callback) {
  537. if (PlatUtils_1.default.IsXiaoMi) {
  538. this._videoCallback = callback;
  539. if (!this._videoAd) {
  540. if (this._videoCallback) {
  541. this._videoCallback(false, "暂无视频广告!");
  542. this._videoCallback = null;
  543. }
  544. this._initVideoAd();
  545. }
  546. else {
  547. // if (this._isVideoLoaded) {
  548. this._videoAd.show();
  549. // } else {
  550. // if (this._videoCallback) {
  551. // this._videoCallback(false, "暂无视频广告!");
  552. // this._videoCallback = null;
  553. // }
  554. // this._videoAd.load();
  555. // return;
  556. // }
  557. }
  558. }
  559. };
  560. /**
  561. * 显示插屏
  562. * @param location
  563. */
  564. AdAgentXiaoMi.prototype.ShowInterstitial = function (location) {
  565. if (location === void 0) { location = null; }
  566. if (PlatUtils_1.default.IsXiaoMi) {
  567. if (!this.checkInsertAdShow()) {
  568. return;
  569. }
  570. var delayTime = 0;
  571. if (this.ServerConfig
  572. && this.ServerConfig.intersititia_delay_show_time) {
  573. delayTime = this.ServerConfig.intersititia_delay_show_time;
  574. }
  575. Utils_1.utils.showLog("\u63D2\u5C4F\u5E7F\u544A\u5EF6\u65F6\u5C55\u793A\uFF01 delayTime:" + delayTime + "\u79D2");
  576. if (this.ServerConfig && this.ServerConfig.isTest && this.ServerConfig.isTest == "true") {
  577. this.showInsertNum++;
  578. // utils.showLog(this.showInsertNum % 2 == 0, "<<this.showInsertNum % 2 == 0");
  579. //@ts-ignore
  580. if (this.showInsertNum % 2 == 0) {
  581. Utils_1.utils.showLog("测试模式>> 顺序展示原生插屏广告!");
  582. this.nativeInserAdDelayCall();
  583. }
  584. else {
  585. //@ts-ignore
  586. this._createMiniGameInsertAd();
  587. }
  588. return;
  589. }
  590. if (this.ServerConfig) {
  591. if (location && location == YZ_Constant_1.BannerLocation.Pause) {
  592. //@ts-ignore
  593. if (this.ServerConfig.intersititial_first_ad == "native") {
  594. Utils_1.utils.showLog("暂停界面不延时展示!");
  595. Utils_1.utils.showLog("优先展示原生插屏广告!");
  596. this.nativeInserAdDelayCall();
  597. }
  598. else {
  599. Utils_1.utils.showLog("优先展示小游戏插屏广告!");
  600. this._createMiniGameInsertAd();
  601. }
  602. }
  603. else {
  604. //@ts-ignore
  605. if (this.ServerConfig.intersititial_first_ad == "native") {
  606. Utils_1.utils.showLog("优先展示原生插屏广告!");
  607. Utils_1.utils.delayCall(this.nativeInserAdDelayCall.bind(this), delayTime);
  608. }
  609. else {
  610. Utils_1.utils.showLog("优先展示小游戏插屏广告!");
  611. Utils_1.utils.delayCall(this._createMiniGameInsertAd.bind(this), delayTime);
  612. }
  613. }
  614. }
  615. else {
  616. Utils_1.utils.showLog("服务器配置数据未初始化!");
  617. }
  618. }
  619. };
  620. AdAgentXiaoMi.prototype._createMiniGameInsertAd = function () {
  621. var _this = this;
  622. this._isInsertShow = true;
  623. //@ts-ignore
  624. this._insertAd = qg.createInterstitialAd({
  625. adUnitId: Utils_1.utils.config.xiaomiConfig.insertId.trim()
  626. });
  627. if (this._insertAd) {
  628. this._insertAd.onLoad(function () {
  629. Utils_1.utils.showLog("插屏广告加载成功");
  630. if (_this._isInsertShow) {
  631. _this._insertAd.show().then(function () {
  632. Utils_1.utils.showLog("插屏广告展示成功!");
  633. _this._insertLastShowTime = new Date().getTime();
  634. }).catch(function (err) {
  635. Utils_1.utils.showLog("\u63D2\u5C4F\u5E7F\u544A\u5C55\u793A\u5931\u8D25!, err=" + JSON.stringify(err));
  636. });
  637. }
  638. });
  639. this._insertAd.onError(function (err) {
  640. Utils_1.utils.showLog("\u63D2\u5C4F\u5E7F\u544A\u76D1\u542C\u5F02\u5E38!, err=" + JSON.stringify(err));
  641. if (_this._isInsertShow) {
  642. if (_this.ServerConfig.intersititial_first_ad == "default") {
  643. _this._isInsertShow = false;
  644. Utils_1.utils.showLog("开始显示原生插屏广告!");
  645. _this._curPosIdIndexNativeInser = 0;
  646. Utils_1.utils.delayCall(_this.nativeInserAdDelayCall.bind(_this), _this.ServerConfig.intersititia_delay_show_time || 0);
  647. }
  648. }
  649. });
  650. }
  651. else {
  652. Utils_1.utils.showLog("插屏广告未初始化!");
  653. }
  654. };
  655. AdAgentXiaoMi.prototype.nativeInserAdDelayCall = function () {
  656. Utils_1.utils.showLog("原生插屏广告: 当前点击次数=" + Utils_1.utils.Tool_XiaoMi.NativeInsertAdClickTimes + "; 点击次数限制=" + this.ServerConfig.intersititia_click_count + "; 展示次数间隔=" + this.ServerConfig.intersititial_interval_time + "; 展示累计=" + this._nativeInsertAdShowCount);
  657. if (Utils_1.utils.Tool_XiaoMi.NativeInsertAdClickTimes >= (this.ServerConfig.intersititia_click_count || 0)) {
  658. // 每日点击次数到达上限,限制展示次数
  659. if (this._nativeInsertAdShowCount >= (this.ServerConfig.intersititia_show_interval || 0)) {
  660. // 可以展示
  661. this._createNativeInsertAd(this._showNativeInsert);
  662. }
  663. else {
  664. // 不能展示
  665. this._nativeInsertAdShowCount++;
  666. }
  667. }
  668. else {
  669. this._createNativeInsertAd(this._showNativeInsert);
  670. }
  671. };
  672. AdAgentXiaoMi.prototype._createNativeInsertAd = function (completeCallback) {
  673. var _this = this;
  674. if (PlatUtils_1.default.IsXiaoMi) {
  675. var callback_2 = completeCallback;
  676. Utils_1.utils.showLog("curPosIdIndexNativeInsert:" + this._curPosIdIndexNativeInser);
  677. if (Utils_1.utils.config.xiaomiConfig.nativeInsertIds
  678. && Utils_1.utils.config.xiaomiConfig.nativeInsertIds[this._curPosIdIndexNativeInser]) {
  679. Utils_1.utils.showLog("创建原生插屏广告。 posId:" + Utils_1.utils.config.xiaomiConfig.nativeInsertIds[this._curPosIdIndexNativeInser]);
  680. //@ts-ignore
  681. var nativeInsertAd_1 = qg.createNativeAd({
  682. adUnitId: Utils_1.utils.config.xiaomiConfig.nativeInsertIds[this._curPosIdIndexNativeInser]
  683. });
  684. if (nativeInsertAd_1) {
  685. // this._nativeInsertAd.push(nativeInsertAd);
  686. nativeInsertAd_1.onLoad(function (res) {
  687. if (res && res.adList && res.adList.length > 0) {
  688. Utils_1.utils.showLog("原生插屏广告资源拉取成功!");
  689. Utils_1.utils.showLog(JSON.stringify(res));
  690. res = JSON.parse(JSON.stringify(res));
  691. var data = res.adList[0];
  692. if (_this._checkNativeInsertDataValid(data)) {
  693. _this._curPosIdIndexNativeInser = 0;
  694. _this._showNativeInsert(nativeInsertAd_1, data);
  695. return;
  696. }
  697. else {
  698. Utils_1.utils.showLog("原生插屏广告资源不合法!");
  699. }
  700. }
  701. Utils_1.utils.showLog("原生插屏广告资源出错!");
  702. _this._curPosIdIndexNativeInser++;
  703. if (_this._curPosIdIndexNativeInser < Utils_1.utils.config.xiaomiConfig.nativeInsertIds.length) {
  704. _this._createNativeInsertAd(callback_2);
  705. }
  706. else {
  707. _this._curPosIdIndexNativeInser = 0;
  708. // 原生广告遍历完毕
  709. Utils_1.utils.showLog("原生插屏广告遍历完毕,无法展示!");
  710. if (_this.ServerConfig.intersititial_first_ad == "native") {
  711. Utils_1.utils.showLog("开始展示小游戏插屏广告!");
  712. _this._createMiniGameInsertAd();
  713. }
  714. }
  715. });
  716. nativeInsertAd_1.onError(function (err) {
  717. Utils_1.utils.showLog("原生插屏广告创建失败!" + JSON.stringify(err));
  718. });
  719. var adLoad = nativeInsertAd_1.load();
  720. adLoad && adLoad.then(function (res) {
  721. console.log("res", JSON.stringify(res));
  722. }).catch(function (err) {
  723. Utils_1.utils.showLog("原生插屏广告资源拉取失败!" + JSON.stringify(err));
  724. _this._curPosIdIndexNativeInser++;
  725. if (_this._curPosIdIndexNativeInser < Utils_1.utils.config.xiaomiConfig.nativeInsertIds.length) {
  726. _this._createNativeInsertAd(callback_2);
  727. }
  728. else {
  729. _this._curPosIdIndexNativeInser = 0;
  730. // 原生广告遍历完毕
  731. Utils_1.utils.showLog("原生插屏广告遍历完毕,无法展示!");
  732. if (_this.ServerConfig.intersititial_first_ad == "native") {
  733. Utils_1.utils.showLog("开始展示小游戏插屏广告!");
  734. _this._createMiniGameInsertAd();
  735. }
  736. }
  737. });
  738. }
  739. else {
  740. this._curPosIdIndexNativeInser++;
  741. if (Utils_1.utils.config.xiaomiConfig.nativeInsertIds && this._curPosIdIndexNativeInser < Utils_1.utils.config.xiaomiConfig.nativeInsertIds.length) {
  742. this._createNativeInsertAd(callback_2);
  743. }
  744. else {
  745. this._curPosIdIndexNativeInser = 0;
  746. // 原生广告遍历完毕
  747. Utils_1.utils.showLog("原生插屏广告遍历完毕,无法展示!");
  748. if (this.ServerConfig.intersititial_first_ad == "native") {
  749. Utils_1.utils.showLog("开始展示小游戏插屏广告!");
  750. this._createMiniGameInsertAd();
  751. }
  752. }
  753. }
  754. }
  755. }
  756. };
  757. /**
  758. * 显示原生插屏组件
  759. */
  760. AdAgentXiaoMi.prototype._showNativeInsert = function (nativeInsertAd, data) {
  761. if (PlatUtils_1.default.IsXiaoMi) {
  762. if (data) {
  763. Utils_1.utils.showLog("显示原生插屏");
  764. if ((!cc.isValid(this._nativeInsertNode)) || !this._nativeInsertNode && Utils_1.utils.config.otherconfig.nativeInsert) {
  765. Utils_1.utils.showLog("创建原生插屏广告位");
  766. this._nativeInsertNode = cc.instantiate(Utils_1.utils.config.otherconfig.nativeInsert);
  767. this._nativeInsertNode.position = CompatibleTool_1.default.position(cc.winSize.width / 2, cc.winSize.height / 2);
  768. cc.director.getScene().addChild(this._nativeInsertNode, 9999);
  769. }
  770. if (this._nativeInsertNode) {
  771. var nativeInsert = this._nativeInsertNode.getComponent("YZ_NativeInsert");
  772. if (nativeInsert) {
  773. this._nativeInsertAdShowCount = 0;
  774. this._insertLastShowTime = new Date().getTime();
  775. nativeInsert.init(nativeInsertAd, data);
  776. }
  777. else {
  778. Utils_1.utils.showLog("NativeInsert组件不存在!");
  779. }
  780. }
  781. else {
  782. Utils_1.utils.showLog("原生广告插屏位没有创建!");
  783. }
  784. if (!this.ServerConfig.show_insert_hide_banner || this.ServerConfig.show_insert_hide_banner != "false") {
  785. Utils_1.utils.showLog("原生广告插屏展示后隐藏Banner!");
  786. this.HideBanner();
  787. }
  788. }
  789. }
  790. };
  791. /**
  792. * 显示原生banner组件
  793. */
  794. AdAgentXiaoMi.prototype._showNativeBanner = function (nativeBannerAd, data) {
  795. if (PlatUtils_1.default.IsXiaoMi) {
  796. if (data) {
  797. if (cc.isValid(this._nativeBannerNode) && this._nativeBannerNode) {
  798. this._nativeBannerNode.destroy();
  799. }
  800. // if (!cc.isValid(this._nativeBannerNode) || (!this._nativeBannerNode && utils.config.otherconfig.nativeBanner)) {
  801. this._nativeBannerNode = cc.instantiate(Utils_1.utils.config.otherconfig.nativeBanner);
  802. this._nativeBannerNode.position = CompatibleTool_1.default.position(cc.winSize.width / 2, this._nativeBannerNode.height * this._nativeBannerNode.scaleY / 2);
  803. cc.director.getScene().addChild(this._nativeBannerNode, 1000);
  804. // }
  805. if (this._nativeBannerNode) {
  806. this._nativeBannerNode.active = true;
  807. var nativeBanner = this._nativeBannerNode.getComponent("YZ_NativeBanner");
  808. if (nativeBanner) {
  809. nativeBanner.init(nativeBannerAd, data, this.getNativeBannerInfo());
  810. }
  811. else {
  812. Utils_1.utils.showLog("NativeBanner组件不存在!");
  813. }
  814. this._showBannerCallBack && this._showBannerCallBack();
  815. this._showBannerCallBack = null;
  816. }
  817. else {
  818. Utils_1.utils.showLog("原生广告banner位没有创建!");
  819. }
  820. }
  821. }
  822. };
  823. /**
  824. * 验证插屏是否能展示
  825. * 2、时间限制 默认30秒
  826. */
  827. AdAgentXiaoMi.prototype.checkInsertAdShow = function () {
  828. var intervalTime = this.ServerConfig.intersititial_interval_time ? this.ServerConfig.intersititial_interval_time : 30;
  829. var curTime = new Date().getTime();
  830. var interval = (curTime - this._insertLastShowTime) / 1000;
  831. Utils_1.utils.showLog("xiaomi服务器插屏间隔显示时间为:" + intervalTime + "秒!");
  832. Utils_1.utils.showLog("xiaomi插屏当前广告间隔时间:" + interval + "秒!");
  833. if (intervalTime > 0 && interval < intervalTime) {
  834. Utils_1.utils.showLog("xiaomi插屏广告显示的间隔少于" + intervalTime + "秒。插屏不显示");
  835. return false;
  836. }
  837. return true;
  838. };
  839. /**
  840. * 验证Banner是否能展示
  841. * 2、时间限制 默认30秒
  842. */
  843. AdAgentXiaoMi.prototype.checkBannerAdShow = function () {
  844. var intervalTime = this.ServerConfig.hide_banner_interval_show_time ? this.ServerConfig.hide_banner_interval_show_time : 0;
  845. var curTime = new Date().getTime();
  846. var interval = (curTime - Utils_1.utils._bannerCloseTime) / 1000;
  847. Utils_1.utils.showLog("xiaomi服务器Banner关闭后间隔显示时间为:" + intervalTime + "秒!");
  848. Utils_1.utils.showLog("xiaomi-Banner关闭后当前广告间隔时间:" + interval + "秒!");
  849. if (intervalTime > 0 && interval < intervalTime) {
  850. Utils_1.utils.showLog("xiaomi-Banner关闭后显示的间隔少于" + intervalTime + "秒。Banner不显示");
  851. return false;
  852. }
  853. return true;
  854. };
  855. AdAgentXiaoMi.prototype._checkNativeInsertDataValid = function (data) {
  856. if (!data) {
  857. return false;
  858. }
  859. return data.title && ((data.iconUrlList && data.iconUrlList.length > 0) || (data.imgUrlList && data.imgUrlList.length > 0));
  860. };
  861. AdAgentXiaoMi.prototype._checkNativeDataValid = function (data) {
  862. if (!data) {
  863. return false;
  864. }
  865. return data.imgUrlList && data.imgUrlList.length;
  866. };
  867. AdAgentXiaoMi = __decorate([
  868. ccclass
  869. ], AdAgentXiaoMi);
  870. return AdAgentXiaoMi;
  871. }(AdAgent_1.default));
  872. exports.default = AdAgentXiaoMi;
  873. cc._RF.pop();