AdAgentXiaomi.ts 39 KB

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