AdAgentVIVO.ts 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  1. import AdAgent from "./AdAgent";
  2. import { BannerLocation } from "./YZ_Constant";
  3. import PlatUtils from "./PlatUtils";
  4. import { utils } from "./Utils";
  5. import NativeTryGamesWidget from "./NativeTryGamesWidget";
  6. import YZ_NativeBanner from "./YZ_NativeBanner";
  7. import { NativeBannerInfo } from "./CommonConfig";
  8. import CompatibleTool from "./CompatibleTool";
  9. import YZ_NativeItem from "./YZ_NativeItem";
  10. import YZ_NativeAdObject from "./YZ_NativeAdObject";
  11. const { ccclass, property } = cc._decorator;
  12. @ccclass
  13. export default class AdAgentVIVO extends AdAgent {
  14. _bannerAd: any = null;
  15. _insertAd: any = null;
  16. _videoAd: any = null;
  17. _nativeBannerAd: any[] = [];
  18. _nativeInsertAd: any[] = [];
  19. _nativeSingleAd: any[] = [];
  20. _isBannerShow: boolean = false;
  21. _isInsertShow: boolean = false;
  22. // 当前位置id的索引
  23. _curPosIdIndexNativeBanner: number = 0;
  24. _curPosIdIndexNativeInser: number = 0;
  25. _curPosIdIndexSingleNative: number = 0;
  26. _nativeData: any = null;
  27. _nativeInsertData: any = null;
  28. _nativeBannerNode: cc.Node = null;
  29. _nativeInsertNode: cc.Node = null;
  30. _videoCallback: Function = null;
  31. _videoLoaded: boolean = null;
  32. _isInsertAdShow: boolean = false;
  33. _isInsertAdLoaded: boolean = false;
  34. _isVideoLoaded: boolean = false;
  35. _isVideoShow: boolean = false;
  36. lastLastShowVideoTime: number = 0; //最后一次显示视频时间
  37. showNum: number = 0; // Banner广告展示次数
  38. showInsertNum: number = 0; // 插屏广告展示次数
  39. _curNativeItem: YZ_NativeItem = null;
  40. _nativeAdObject: YZ_NativeAdObject = null;
  41. _nativeAd: any = null;
  42. public get SysInfo() {
  43. return utils.Tool_Vivo.SysInfo;
  44. }
  45. public get ServerConfig() {
  46. if (PlatUtils.IsVIVO) {
  47. return utils.Tool_Vivo.ServerConfig;
  48. }
  49. return null;
  50. }
  51. _nativeBannerInfo: NativeBannerInfo = null;
  52. /**
  53. * 获取当前banner配置
  54. */
  55. getNativeBannerInfo() {
  56. if (this._nativeBannerInfo && this._nativeBannerInfo.location == this._curLocation) {
  57. return this._nativeBannerInfo;
  58. }
  59. return utils.config.vivoconfig.getNativeBannerInfo(this._curLocation);
  60. }
  61. public Init() {
  62. if (PlatUtils.IsVIVO) {
  63. if (utils.Tool_Vivo.isOverMiniVersion("1084")) {
  64. //@ts-ignore
  65. qg.isSupportNativeAd = true;
  66. }
  67. utils.registerServerInitEvent(() => {
  68. if (utils.config.vivoconfig.showAd) {
  69. this._initVideoAd();
  70. } else {
  71. utils.showLog("广告开关关闭状态,所有广告不显示!要显示广告,请打开 CommonUtils 组件上VIVIO 配置下的广告开关!");
  72. }
  73. }, this);
  74. }
  75. }
  76. _initVideoAd() {
  77. if (!this._videoAd) {
  78. if (!utils.Tool_Vivo.isOverMiniVersion("1041")) {
  79. utils.showLog("当前版本不支持视频广告!");
  80. if (this._videoCallback) {
  81. this._videoCallback(false, "暂无视频广告!");
  82. this._videoCallback = null;
  83. }
  84. return;
  85. }
  86. if (!utils.config.vivoconfig.videoId) {
  87. utils.showLog("视频ID配置不正确!");
  88. if (this._videoCallback) {
  89. this._videoCallback(false, "暂无视频广告!");
  90. this._videoCallback = null;
  91. }
  92. return;
  93. }
  94. let posId: string = utils.config.vivoconfig.videoId.trim();
  95. utils.showLog("video广告ID:" + posId);
  96. //@ts-ignore
  97. this._videoAd = qg.createRewardedVideoAd({
  98. posId: posId
  99. });
  100. if (this._videoAd) {
  101. this._videoAd.onLoad((() => {
  102. utils.showLog("激励视频加载成功!");
  103. this._isVideoLoaded = true;
  104. if (this._isVideoShow) {
  105. var adshow = this._videoAd.show();
  106. // 调用then和catch之前需要对show的结果做下判空处理,防止出错(如果没有判空,在平台版本为1052以及以下的手机上将会出现错误)
  107. adshow && adshow.then(() => {
  108. this._isVideoLoaded = false;
  109. }).catch(err => {
  110. if (this._videoCallback) {
  111. this._videoCallback(false, "暂无视频广告!");
  112. this._videoCallback = null;
  113. }
  114. utils.showLog("视频播放失败!")
  115. });
  116. }
  117. }));
  118. this._videoAd.onError((err => {
  119. utils.showLog("激励视频异常!" + err.errCode);
  120. this._isVideoLoaded = false;
  121. if (this._videoCallback) {
  122. this._videoCallback(false, "暂无视频广告!");
  123. this._videoCallback = null;
  124. }
  125. }));
  126. this._videoAd.onClose(res => {
  127. // setTimeout(() => {
  128. // utils.showLog(`延时调用加载视频!`);
  129. this._videoAd.load();
  130. // }, 500);
  131. this._isVideoShow = false;
  132. if (res && res.isEnded) {
  133. utils.showLog("正常播放结束,可以下发游戏奖励");
  134. if (this._videoCallback) {
  135. this._videoCallback(true, "");
  136. this._videoCallback = null;
  137. }
  138. } else {
  139. utils.showLog("播放中途退出,不下发游戏奖励");
  140. if (this._videoCallback) {
  141. this._videoCallback(false, "观看完视频才能获得奖励!");
  142. this._videoCallback = null;
  143. }
  144. }
  145. });
  146. }
  147. }
  148. }
  149. //当前显示Banner的位置
  150. _curLocation: BannerLocation = BannerLocation.None;
  151. ShowBanner(location: BannerLocation = BannerLocation.Home, args: any = null, isTimeRefresh: boolean = false) {
  152. if (!this.ServerConfig) {
  153. utils.showLog("未请求到配置文件!");
  154. return;
  155. }
  156. let oldLocation = this._curLocation;
  157. this._curLocation = location;
  158. //如果跳用位置切换之后,验证是不是通过定时器强制刷新数据,不是则先隐藏banner
  159. if (oldLocation != location) {
  160. this.HideBanner(location);
  161. }
  162. let curTime: number = new Date().getTime();
  163. let refresh_ad_time: number = (curTime - this._startBannerTimerTask) / 1000;
  164. let interval = this.ServerConfig.refresh_ad_time;
  165. if (args && args.isRefresh) {
  166. isTimeRefresh = true;
  167. } else if (interval && interval > 0) {
  168. if (refresh_ad_time > interval) {
  169. isTimeRefresh = true;
  170. }
  171. }
  172. // 判断当前位置是不是显示banner
  173. if (this.getNativeBannerInfo().is_show_banner == -1) {
  174. utils.showLog("当前位置配置为不展示banner!");
  175. this.HideBanner(location);
  176. return;
  177. } else if (this.getNativeBannerInfo().is_show_rec > -1) {
  178. utils.showLog("当前位置显示互推banner");
  179. if (utils.Tool_Vivo.canShowRecommend()) {
  180. this.HideBanner(location);
  181. utils.Tool_Vivo.showRecBanner();
  182. return;
  183. }
  184. utils.showLog("当前平台不支持互推banner");
  185. } else {
  186. if (!isTimeRefresh) {
  187. if (this.ServerConfig.nbclr && this.ServerConfig.nbclr == "true" && this._curNativeBannerInfo.nativeBannerAd) {
  188. utils.showLog("服务器配置定时器刷新数据 ");
  189. this._showNativeBanner(this._curNativeBannerInfo.nativeBannerAd, this._curNativeBannerInfo.data);
  190. utils.showLog("开启定时刷新 >>>>>>>>>" + (interval - refresh_ad_time));
  191. clearTimeout(this._showBannerTimerId);
  192. clearTimeout(this._delayShowBannerId);
  193. //@ts-ignore
  194. this._showBannerTimerId = setTimeout(() => {
  195. utils.showLog(`定时${interval}秒调用showbanner`);
  196. this.ShowOldBanner(this._curLocation, {});
  197. }, (interval - refresh_ad_time) * 1000);
  198. return;
  199. }
  200. }
  201. }
  202. if (utils._tool_Vivo.isOverMiniVersion("1059")) {
  203. this.showNewBanner(location, args);
  204. } else {
  205. this.ShowOldBanner(location, args);
  206. }
  207. }
  208. _lastShowTime: number = 0;//上一次调用showBanner的时间
  209. _bannerTimeoutShow: number = 0;
  210. showNewBanner(location: BannerLocation = BannerLocation.Home, args: any = null) {
  211. let curTime = new Date().getTime();
  212. let cap = this.ServerConfig ? this.ServerConfig.cap_show_banner_time : undefined;
  213. cap = cap ? cap : 15;
  214. if (((curTime - this._lastShowTime) / 1000) > cap) {
  215. this._lastShowTime = curTime;
  216. this.ShowOldBanner(location, args);
  217. } else {
  218. this._lastShowTime = curTime;
  219. clearTimeout(this._bannerTimeoutShow);
  220. this._bannerTimeoutShow = setTimeout(() => {
  221. utils.showLog("达到" + cap + "间隔,显示banner");
  222. this.ShowOldBanner(this._curLocation, args);
  223. }, cap * 1000);
  224. utils.showLog("1059以上展示banner必须间隔" + cap + "秒")
  225. }
  226. }
  227. _nativeIsClose: boolean = false;//原生banner是否已经被关闭
  228. _showBannerTimerId: number = 0;
  229. _delayShowBannerId: number = 0;
  230. _startBannerTimerTask: number = 0;
  231. public ShowOldBanner(location: BannerLocation = BannerLocation.Home, args: any = null) {
  232. if (PlatUtils.IsVIVO) {
  233. if (!utils.config.vivoconfig.showAd) {
  234. return;
  235. }
  236. clearTimeout(this._showBannerTimerId);
  237. clearTimeout(this._delayShowBannerId);
  238. this._nativeIsClose = false;
  239. if (this.ServerConfig) {
  240. let interval = this.ServerConfig.refresh_ad_time;
  241. let bannerInfo = this.getNativeBannerInfo();
  242. if (interval && interval > 0) {
  243. //@ts-ignore
  244. this._showBannerTimerId = setTimeout(() => {
  245. utils.showLog(`定时${interval}秒调用showbanner`);
  246. this.ShowBanner(location, {}, true);
  247. }, interval * 1000);
  248. }
  249. if (this.ServerConfig && this.ServerConfig.isTest && this.ServerConfig.isTest == "true") {
  250. this.showNum++;
  251. //@ts-ignore
  252. if (this.showNum % 2 != 0 && qg.isSupportNativeAd) {
  253. utils.showLog("测试模式>> 顺序展示原生Banner广告!");
  254. this._createNativeBannerAd(this._showNativeBanner);
  255. } else {
  256. //@ts-ignore
  257. utils.showLog("测试模式>>" + (qg.isSupportNativeAd ? "" : "平台不支持原生广告>>" + "顺序展示小游戏Banner广告!"));
  258. this._createMiniGameBannerAd(location);
  259. }
  260. return;
  261. }
  262. if (this.ServerConfig.banner_first_ad) {
  263. //@ts-ignore
  264. if (this.ServerConfig.banner_first_ad == "native" && qg.isSupportNativeAd) {
  265. utils.showLog("优先展示原生Banner广告!");
  266. if (this.ServerConfig.intersititial_first_ad == "native") {
  267. let timeOut = 1;
  268. if (this.ServerConfig.native_banner_delay_time) {
  269. timeOut = this.ServerConfig.native_banner_delay_time;
  270. }
  271. utils.showLog(`因为插屏也是优先展示原生,所以banner延迟显示${timeOut}秒`);
  272. //@ts-ignore
  273. this._delayShowBannerId = setTimeout(() => {
  274. this._createNativeBannerAd(this._showNativeBanner);
  275. }, timeOut * 1000);
  276. } else {
  277. this._createNativeBannerAd(this._showNativeBanner);
  278. }
  279. } else {
  280. //@ts-ignore
  281. utils.showLog("优先展示小游戏Banner广告,引擎" + (qg.isSupportNativeAd ? "支持" : "不支持") + "原生广告");
  282. this._createMiniGameBannerAd(location);
  283. }
  284. } else {
  285. utils.showLog("配置数据中没有 banner_first_ad 字段, banner广告不显示!");
  286. }
  287. } else {
  288. utils.showLog("服务器配置数据未初始化!");
  289. }
  290. }
  291. }
  292. public HideBanner(location: BannerLocation = BannerLocation.Home) {
  293. if (PlatUtils.IsVIVO) {
  294. clearTimeout(this._showBannerTimerId);
  295. clearTimeout(this._delayShowBannerId);
  296. clearTimeout(this._bannerTimeoutShow);
  297. this._nativeIsClose = true;
  298. if (this._bannerAd) {
  299. var adhide = this._bannerAd.hide();
  300. adhide && adhide.then(() => {
  301. console.log("banner广告隐藏成功");
  302. }).catch(err => {
  303. console.log("banner广告隐藏失败", JSON.stringify(err));
  304. var addestroy = this._bannerAd.destroy();
  305. addestroy && addestroy.then(() => {
  306. console.log("banner广告销毁成功");
  307. }).catch(err => {
  308. console.log("banner广告销毁失败", JSON.stringify(err));
  309. });
  310. });
  311. }
  312. if (this._nativeBannerNode) {
  313. utils.showLog("隐藏原生Banner");
  314. this._nativeBannerNode.active = false;
  315. }
  316. //隐藏互推banner
  317. utils.Tool_Vivo && utils.Tool_Vivo.hideRecBanner();
  318. }
  319. }
  320. _tryIndex = 0;
  321. tryGameInfo: any;
  322. public showNativeTryGameWidget(params: any = null) {
  323. if (PlatUtils.IsVIVO) {
  324. utils.showLog("创建原生抖动广告。 posId:" + utils.config.vivoconfig.nativeTryGameIds[0], " utils.tryGameDate", utils.tryGameDate, "needchange", utils.nativeNeedChange);
  325. if (utils.nativeNeedChange || !utils.tryGameDate) {
  326. if (utils.config.vivoconfig.nativeTryGameIds) {
  327. //@ts-ignore
  328. let tryGameAd = qg.createNativeAd({
  329. posId: utils.config.vivoconfig.nativeTryGameIds[0]
  330. });
  331. utils.showLog("tryGameAd:" + tryGameAd)
  332. if (tryGameAd) {
  333. tryGameAd.onLoad((res) => {
  334. utils.showLog("res", res)
  335. utils.nativeNeedChange = false;
  336. this.tryGameInfo = res.adList;
  337. if (res && res.adList && res.adList.length > 0) {
  338. utils.showLog("原生抖动试完广告资源拉取成功!");
  339. utils.showLog(JSON.stringify(res));
  340. res = JSON.parse(JSON.stringify(res));
  341. let adId = res.adList[0].adId
  342. utils.showLog("adId:" + adId);
  343. let canAdd = true;
  344. utils.tryGameDate.forEach((element) => {
  345. if (adId == element.date[0].adId) {
  346. canAdd = false;
  347. }
  348. })
  349. if (canAdd) {
  350. utils.tryGameDate.push({ "tryGameAd": tryGameAd, "date": this.tryGameInfo });
  351. if (utils._nativeTryGameNode) {
  352. utils._nativeTryGameNode.getComponent(NativeTryGamesWidget).init();
  353. }
  354. }
  355. }
  356. });
  357. tryGameAd.onError((err) => {
  358. console.log("原生广告加载异常", JSON.stringify(err));
  359. })
  360. tryGameAd.load();
  361. }
  362. } else {
  363. utils.showLog("原生抖动id不存在")
  364. }
  365. }
  366. this.createNativeTryGameWidget(params);
  367. }
  368. }
  369. public ShowInterstitial(location: BannerLocation = null) {
  370. if (PlatUtils.IsVIVO) {
  371. if (!utils.config.vivoconfig.showAd) {
  372. return;
  373. }
  374. if (this.ServerConfig && this.ServerConfig.isTest && this.ServerConfig.isTest == "true") {
  375. this.showInsertNum++;
  376. // utils.showLog(this.showInsertNum % 2 == 0, "<<this.showInsertNum % 2 == 0");
  377. //@ts-ignore
  378. if (this.showInsertNum % 2 == 0 && qg.isSupportNativeAd) {
  379. utils.showLog("测试模式>> 顺序展示原生插屏广告!");
  380. this.nativeInserAdDelayCall();
  381. } else {
  382. //@ts-ignore
  383. utils.showLog("测试模式>>" + (qg.isSupportNativeAd ? "" : "平台不支持原生广告>>") + "顺序展示小游戏插屏广告!");
  384. this._createMiniGameInsertAd();
  385. }
  386. return;
  387. }
  388. if (this.ServerConfig) {
  389. if (location && location == BannerLocation.Pause) {
  390. //@ts-ignore
  391. if (this.ServerConfig.intersititial_first_ad == "native" && qg.isSupportNativeAd) {
  392. utils.showLog("暂停界面不延时展示!");
  393. utils.showLog("优先展示原生插屏广告!");
  394. this.nativeInserAdDelayCall();
  395. } else {
  396. utils.showLog("优先展示小游戏插屏广告!");
  397. this._createMiniGameInsertAd();
  398. }
  399. } else {
  400. //@ts-ignore
  401. if (this.ServerConfig.intersititial_first_ad == "native" && qg.isSupportNativeAd) {
  402. utils.showLog("优先展示原生插屏广告!");
  403. utils.delayCall(this.nativeInserAdDelayCall.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  404. } else {
  405. utils.showLog("优先展示小游戏插屏广告!");
  406. this._createMiniGameInsertAd();
  407. }
  408. }
  409. } else {
  410. utils.showLog("服务器配置数据未初始化!");
  411. }
  412. }
  413. }
  414. _nativeInsertAdShowCount: number = 0;
  415. public nativeInserAdDelayCall() {
  416. utils.showLog("原生插屏广告: 当前点击次数=" + utils.Tool_Vivo.NativeInsertAdClickTimes + "; 点击次数限制=" + this.ServerConfig.intersititia_click_count + "; 展示次数间隔=" + this.ServerConfig.intersititial_interval_time + "; 展示累计=" + this._nativeInsertAdShowCount);
  417. if (utils.Tool_Vivo.NativeInsertAdClickTimes >= (this.ServerConfig.intersititia_click_count || 0)) {
  418. // 每日点击次数到达上限,限制展示次数
  419. if (this._nativeInsertAdShowCount >= (this.ServerConfig.intersititia_show_interval || 0)) {
  420. // 可以展示
  421. this._createNativeInsertAd(this._showNativeInsert);
  422. } else {
  423. // 不能展示
  424. this._nativeInsertAdShowCount++;
  425. }
  426. } else {
  427. this._createNativeInsertAd(this._showNativeInsert);
  428. }
  429. }
  430. public ShowVideo(callback: Function) {
  431. if (PlatUtils.IsVIVO) {
  432. this._videoCallback = callback;
  433. if (!utils.config.vivoconfig.showAd) {
  434. if (this._videoCallback) {
  435. this._videoCallback(false, "暂无视频广告!");
  436. this._videoCallback = null;
  437. }
  438. return;
  439. }
  440. this._isVideoShow = true;
  441. let curTime: number = new Date().getTime();
  442. let interval: number = (curTime - this.lastLastShowVideoTime) / 1000;
  443. let interval_time = 0;
  444. if (utils.ServerConfig && utils.ServerConfig.video_interval_time) {
  445. interval_time = utils.ServerConfig.video_interval_time;
  446. }
  447. if (interval > interval_time) {
  448. this.lastLastShowVideoTime = new Date().getTime();
  449. if (!this._videoAd) {
  450. this._initVideoAd();
  451. } else {
  452. if (this._isVideoLoaded) {
  453. var adshow = this._videoAd.show();
  454. // 调用then和catch之前需要对show的结果做下判空处理,防止出错(如果没有判空,在平台版本为1052以及以下的手机上将会出现错误)
  455. adshow && adshow.then(() => {
  456. utils.showLog("视频显示成功!");
  457. this._isVideoLoaded = false;
  458. }).catch(err => {
  459. utils.showLog("激励视频广告显示失败" + JSON.stringify(err));
  460. if (this._videoCallback) {
  461. this._videoCallback(false, "暂无视频广告!");
  462. this._videoCallback = null;
  463. }
  464. });
  465. } else {
  466. this._isVideoShow = false;
  467. if (this._videoCallback) {
  468. this._videoCallback(false, "暂无视频广告!");
  469. this._videoCallback = null;
  470. }
  471. this._videoAd.load();
  472. return;
  473. }
  474. }
  475. } else {
  476. utils.showLog("视频广告请求间隔小于60秒,直接返回false");
  477. if (this._videoCallback) {
  478. this._videoCallback(false, "暂无视频广告!");
  479. this._videoCallback = null;
  480. }
  481. }
  482. }
  483. }
  484. _createMiniGameInsertAd() {
  485. if (PlatUtils.IsVIVO) {
  486. this._isInsertShow = true;
  487. if (utils.config.vivoconfig
  488. && utils.config.vivoconfig.insertId) {
  489. // if (!this._insertAd) {
  490. //@ts-ignore
  491. this._insertAd = qg.createInterstitialAd({
  492. posId: utils.config.vivoconfig.insertId
  493. });
  494. if (this._insertAd) {
  495. utils.showLog("注册小游戏插屏广告回调!");
  496. this._insertAd.onError(((err) => {
  497. utils.showLog("vivo 小游戏插屏广告出错:" + err.errCode + err.errMsg);
  498. if (this._isInsertShow) {
  499. if (this.ServerConfig.intersititial_first_ad == "default") {
  500. this._isInsertShow = false;
  501. utils.showLog("开始显示原生插屏广告!");
  502. this._curPosIdIndexNativeInser = 0;
  503. utils.delayCall(this.nativeInserAdDelayCall.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  504. }
  505. }
  506. }).bind(this));
  507. // this._insertAd.onLoad((() => {
  508. if (this._isInsertShow) {
  509. // this._isInsertShow = false;
  510. var adshow = this._insertAd.show();
  511. // 调用then和catch之前需要对show的结果做下判空处理,防止出错(如果没有判空,在平台版本为1052以及以下的手机上将会出现错误)
  512. adshow && adshow.then(() => {
  513. utils.showLog("vivo 小游戏插屏广告显示成功!");
  514. this._isInsertShow = false;
  515. }).catch(err => {
  516. switch (err.code) {
  517. case 30003:
  518. utils.showLog("新用户7天内不能曝光插屏,请将手机时间调整为7天后,退出游戏重新进入")
  519. break;
  520. case 30009:
  521. utils.showLog("10秒内调用广告次数超过1次,10秒后再调用")
  522. // setTimeout(() => {
  523. // show()
  524. // }, 10000);
  525. break;
  526. case 30002:
  527. utils.showLog("加载广告失败,重新加载广告")
  528. break;
  529. default:
  530. // 参考 https://minigame.vivo.com.cn/documents/#/lesson/open-ability/ad?id=广告错误码信息 对错误码做分类处理
  531. utils.showLog("插屏广告展示失败")
  532. break;
  533. }
  534. });
  535. }
  536. // }).bind(this));
  537. }
  538. // } else {
  539. // this._insertAd.load();
  540. // }
  541. if (!this._insertAd) {
  542. utils.showLog("vivo 小游戏插屏广告创建失败!");
  543. if (this.ServerConfig.intersititial_first_ad == "default") {
  544. utils.showLog("开始显示原生插屏广告!");
  545. this._curPosIdIndexNativeInser = 0;
  546. utils.delayCall(this.nativeInserAdDelayCall.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  547. }
  548. }
  549. } else {
  550. utils.showLog("vivo 小游戏插屏广告配置信息错误!");
  551. if (this.ServerConfig.intersititial_first_ad == "default") {
  552. utils.showLog("开始显示原生插屏广告!");
  553. this._curPosIdIndexNativeInser = 0;
  554. utils.delayCall(this.nativeInserAdDelayCall.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  555. }
  556. }
  557. }
  558. }
  559. _createMiniGameBannerAd(location: BannerLocation = null) {
  560. if (PlatUtils.IsVIVO) {
  561. this._isBannerShow = true;
  562. if (utils.config.vivoconfig
  563. && utils.config.vivoconfig.bannerId) {
  564. let oldBannerAd = this._bannerAd;
  565. if (oldBannerAd) {
  566. var addestroy = oldBannerAd.destroy();
  567. addestroy && addestroy.then(() => {
  568. console.log("banner广告销毁成功");
  569. }).catch(err => {
  570. console.log("banner广告销毁失败", JSON.stringify(err));
  571. });
  572. }
  573. // if (!this._bannerAd) {
  574. //@ts-ignore
  575. this._bannerAd = qg.createBannerAd({
  576. posId: utils.config.vivoconfig.bannerId,
  577. style: {}
  578. });
  579. if (this._bannerAd) {
  580. utils.showLog("注册小游戏banner回调!");
  581. this._bannerAd.onError(((err) => {
  582. utils.showLog("VIVO 广告条加载失败! code : " + err.errCode + "; msg : " + err.errMsg);
  583. let bannerInfo = this.getNativeBannerInfo();
  584. if (bannerInfo.st_banner_show_back_up == -1) {
  585. utils.showLog("服务器配置不显示备用广告");
  586. return;
  587. }
  588. if (this._isBannerShow) {
  589. this._isBannerShow = false;
  590. if (this.ServerConfig.banner_first_ad == "default") {
  591. this._curPosIdIndexNativeBanner = 0;
  592. this._createNativeBannerAd(this._showNativeBanner.bind(this));
  593. }
  594. }
  595. }).bind(this));
  596. } else {
  597. if (this.ServerConfig.banner_first_ad == "default") {
  598. this._curPosIdIndexNativeBanner = 0;
  599. this._createNativeBannerAd(this._showNativeBanner.bind(this));
  600. }
  601. }
  602. // }
  603. if (this._bannerAd) {
  604. let adshow = this._bannerAd.show();
  605. adshow && adshow.then(() => {
  606. utils.showLog("vivo小游戏banner广告展示成功");
  607. if (this._nativeBannerNode) {
  608. this._nativeBannerNode.active = false;
  609. }
  610. }).catch((err) => {
  611. switch (err.code) {
  612. case 30003:
  613. utils.showLog("新用户7天内不能曝光Banner,请将手机时间调整为7天后,退出游戏重新进入")
  614. break;
  615. case 30009:
  616. utils.showLog("10秒内调用广告次数超过1次,10秒后再调用")
  617. // setTimeout(() => {
  618. // show()
  619. // }, 10000);
  620. break;
  621. case 30002:
  622. utils.showLog("加载广告失败,重新加载广告")
  623. break;
  624. default:
  625. // 参考 https://minigame.vivo.com.cn/documents/#/lesson/open-ability/ad?id=广告错误码信息 对错误码做分类处理
  626. utils.showLog("banner广告展示失败")
  627. break;
  628. }
  629. });
  630. } else {
  631. utils.showLog("vivo 小游戏Banner广告创建失败!");
  632. if (this.ServerConfig.banner_first_ad == "default") {
  633. this._curPosIdIndexNativeBanner = 0;
  634. this._createNativeBannerAd(this._showNativeBanner.bind(this));
  635. }
  636. }
  637. } else {
  638. utils.showLog("vivo 小游戏Banner广告配置信息错误!");
  639. let bannerInfo = this.getNativeBannerInfo();
  640. if (bannerInfo.st_banner_show_back_up == -1) {
  641. utils.showLog("服务器配置不显示备用广告");
  642. return;
  643. }
  644. if (this.ServerConfig.banner_first_ad == "default") {
  645. this._curPosIdIndexNativeBanner = 0;
  646. this._createNativeBannerAd(this._showNativeBanner.bind(this));
  647. }
  648. }
  649. }
  650. }
  651. _curNativeBannerInfo: any = {};
  652. _createNativeBannerAd(completeCallback: Function) {
  653. if (PlatUtils.IsVIVO) {
  654. //@ts-ignore
  655. if (!qg.isSupportNativeAd) {
  656. utils.showLog("当前引擎不支持原生Banner广告!");
  657. return;
  658. }
  659. let callback = completeCallback;
  660. utils.showLog("curPosIdIndexNativeBanner:" + this._curPosIdIndexNativeBanner);
  661. if (utils.config.vivoconfig.nativeBannerIds
  662. && utils.config.vivoconfig.nativeBannerIds[this._curPosIdIndexNativeBanner]) {
  663. utils.showLog("创建原生广告Banner。 posId:" + utils.config.vivoconfig.nativeBannerIds[this._curPosIdIndexNativeBanner]);
  664. //@ts-ignore
  665. let nativeBannerAd = qg.createNativeAd({
  666. posId: utils.config.vivoconfig.nativeBannerIds[this._curPosIdIndexNativeBanner]
  667. });
  668. if (nativeBannerAd) {
  669. nativeBannerAd.onLoad((res) => {
  670. if (this._nativeIsClose) {
  671. utils.showLog("隐藏了BANNER,不做任何处理!");
  672. return;
  673. }
  674. if (res && res.adList && res.adList.length > 0) {
  675. this._startBannerTimerTask = new Date().getTime();
  676. utils.showLog("原生Banner广告资源拉取成功!");
  677. utils.showLog(JSON.stringify(res));
  678. res = JSON.parse(JSON.stringify(res));
  679. let data = res.adList[0];
  680. if (this._checkNativeDataValid(data) || this.getNativeBannerInfo().show_st_banner == "true") {
  681. this._curPosIdIndexNativeBanner = 0;
  682. this._showNativeBanner(nativeBannerAd, data);
  683. this._curNativeBannerInfo.nativeBannerAd = nativeBannerAd;
  684. this._curNativeBannerInfo.data = data;
  685. // 删除当前广告
  686. if (this._bannerAd) {
  687. var adhide = this._bannerAd.hide();
  688. adhide && adhide.then(() => {
  689. console.log("banner广告隐藏成功");
  690. }).catch(err => {
  691. console.log("banner广告隐藏失败", JSON.stringify(err));
  692. var addestroy = this._bannerAd.destroy();
  693. addestroy && addestroy.then(() => {
  694. console.log("banner广告销毁成功");
  695. }).catch(err => {
  696. console.log("banner广告销毁失败", JSON.stringify(err));
  697. });
  698. });
  699. }
  700. return;
  701. }
  702. }
  703. utils.showLog("原生Banner广告资源出错!");
  704. this._curPosIdIndexNativeBanner++;
  705. if (this._curPosIdIndexNativeBanner < utils.config.vivoconfig.nativeBannerIds.length) {
  706. this._createNativeBannerAd(callback);
  707. } else {
  708. this._curPosIdIndexNativeBanner = 0;
  709. // 原生广告遍历完毕
  710. utils.showLog("原生广告条遍历完毕,无法展示!");
  711. let bannerInfo = this.getNativeBannerInfo();
  712. if (bannerInfo.st_banner_show_back_up == -1) {
  713. utils.showLog(">>>>>服务器配置不显示备用广告!");
  714. return;
  715. }
  716. if (this.ServerConfig.banner_first_ad == "native") {
  717. utils.showLog("开始展示小游戏广告条!")
  718. this._createMiniGameBannerAd();
  719. }
  720. }
  721. });
  722. let adLoad = nativeBannerAd.load();
  723. adLoad && adLoad.then((res) => {
  724. console.log("res", JSON.stringify(res));
  725. }).catch(err => {
  726. utils.showLog("原生Banner广告资源拉取失败!" + JSON.stringify(err));
  727. if (this._nativeIsClose) {
  728. utils.showLog("隐藏了BANNER,不做任何处理!");
  729. return;
  730. }
  731. this._curPosIdIndexNativeBanner++;
  732. if (this._curPosIdIndexNativeBanner < utils.config.vivoconfig.nativeBannerIds.length) {
  733. this._createNativeBannerAd(callback);
  734. } else {
  735. this._curPosIdIndexNativeBanner = 0;
  736. // 原生广告遍历完毕
  737. utils.showLog("原生广告条遍历完毕,无法展示!");
  738. let bannerInfo = this.getNativeBannerInfo();
  739. if (bannerInfo.st_banner_show_back_up == -1) {
  740. utils.showLog(">>>>>服务器配置不显示备用广告!");
  741. return;
  742. }
  743. if (this.ServerConfig.banner_first_ad == "native") {
  744. utils.showLog("开始展示小游戏广告条!")
  745. this._createMiniGameBannerAd();
  746. }
  747. }
  748. })
  749. } else {
  750. this._curPosIdIndexNativeBanner++;
  751. if (utils.config.vivoconfig.nativeBannerIds && this._curPosIdIndexNativeBanner < utils.config.vivoconfig.nativeBannerIds.length) {
  752. this._createNativeBannerAd(callback);
  753. } else {
  754. this._curPosIdIndexNativeBanner = 0;
  755. // 原生广告遍历完毕
  756. utils.showLog("原生广告条遍历完毕,无法展示!");
  757. let bannerInfo = this.getNativeBannerInfo();
  758. if (bannerInfo.st_banner_show_back_up == -1) {
  759. utils.showLog(">>>>>服务器配置不显示备用广告!");
  760. return;
  761. }
  762. if (this.ServerConfig.banner_first_ad == "native") {
  763. utils.showLog("开始展示小游戏广告条!")
  764. this._createMiniGameBannerAd();
  765. }
  766. }
  767. }
  768. }
  769. }
  770. }
  771. _createNativeInsertAd(completeCallback: Function) {
  772. if (PlatUtils.IsVIVO) {
  773. //@ts-ignore
  774. if (!qg.isSupportNativeAd) {
  775. utils.showLog("当前引擎不支持原生插屏广告!");
  776. return;
  777. }
  778. let callback = completeCallback;
  779. utils.showLog("curPosIdIndexNativeInsert:" + this._curPosIdIndexNativeInser);
  780. if (utils.config.vivoconfig.nativeInsertIds
  781. && utils.config.vivoconfig.nativeInsertIds[this._curPosIdIndexNativeInser]) {
  782. utils.showLog("创建原生插屏广告。 posId:" + utils.config.vivoconfig.nativeInsertIds[this._curPosIdIndexNativeInser]);
  783. //@ts-ignore
  784. let nativeInsertAd = qg.createNativeAd({
  785. posId: utils.config.vivoconfig.nativeInsertIds[this._curPosIdIndexNativeInser]
  786. });
  787. if (nativeInsertAd) {
  788. // this._nativeInsertAd.push(nativeInsertAd);
  789. nativeInsertAd.onLoad((res) => {
  790. if (res && res.adList && res.adList.length > 0) {
  791. utils.showLog("原生插屏广告资源拉取成功!");
  792. utils.showLog(JSON.stringify(res));
  793. res = JSON.parse(JSON.stringify(res));
  794. let data = res.adList[0];
  795. if (this._checkNativeInsertDataValid(data)) {
  796. this._curPosIdIndexNativeInser = 0;
  797. this._showNativeInsert(nativeInsertAd, data);
  798. return;
  799. } else {
  800. utils.showLog("原生插屏广告资源不合法!");
  801. }
  802. }
  803. utils.showLog("原生插屏广告资源出错!");
  804. this._curPosIdIndexNativeInser++;
  805. if (this._curPosIdIndexNativeInser < utils.config.vivoconfig.nativeInsertIds.length) {
  806. this._createNativeInsertAd(callback);
  807. } else {
  808. this._curPosIdIndexNativeInser = 0;
  809. // 原生广告遍历完毕
  810. utils.showLog("原生插屏广告遍历完毕,无法展示!");
  811. if (this.ServerConfig.intersititial_first_ad == "native") {
  812. utils.showLog("开始展示小游戏插屏广告!");
  813. this._createMiniGameInsertAd();
  814. }
  815. }
  816. });
  817. nativeInsertAd.onError((err) => {
  818. utils.showLog("原生插屏广告创建失败!" + JSON.stringify(err));
  819. });
  820. let adLoad = nativeInsertAd.load();
  821. adLoad && adLoad.then((res) => {
  822. console.log("res", JSON.stringify(res));
  823. }).catch(err => {
  824. utils.showLog("原生插屏广告资源拉取失败!" + JSON.stringify(err));
  825. this._curPosIdIndexNativeInser++;
  826. if (this._curPosIdIndexNativeInser < utils.config.vivoconfig.nativeInsertIds.length) {
  827. this._createNativeInsertAd(callback);
  828. } else {
  829. this._curPosIdIndexNativeInser = 0;
  830. // 原生广告遍历完毕
  831. utils.showLog("原生插屏广告遍历完毕,无法展示!");
  832. if (this.ServerConfig.intersititial_first_ad == "native") {
  833. utils.showLog("开始展示小游戏插屏广告!");
  834. this._createMiniGameInsertAd();
  835. }
  836. }
  837. })
  838. } else {
  839. this._curPosIdIndexNativeInser++;
  840. if (utils.config.vivoconfig.nativeInsertIds && this._curPosIdIndexNativeInser < utils.config.vivoconfig.nativeInsertIds.length) {
  841. this._createNativeInsertAd(callback);
  842. } else {
  843. this._curPosIdIndexNativeInser = 0;
  844. // 原生广告遍历完毕
  845. utils.showLog("原生插屏广告遍历完毕,无法展示!");
  846. if (this.ServerConfig.intersititial_first_ad == "native") {
  847. utils.showLog("开始展示小游戏插屏广告!");
  848. this._createMiniGameInsertAd();
  849. }
  850. }
  851. }
  852. }
  853. }
  854. }
  855. /**
  856. * 显示原生banner组件
  857. */
  858. _showNativeBanner(nativeBannerAd: any, data: any) {
  859. if (PlatUtils.IsVIVO) {
  860. if (data) {
  861. utils.showLog("显示原生banner");
  862. if (cc.isValid(this._nativeBannerNode) && this._nativeBannerNode) {
  863. this._nativeBannerNode.destroy();
  864. }
  865. // if (!cc.isValid(this._nativeBannerNode) || (!this._nativeBannerNode && utils.config.otherconfig.nativeBanner)) {
  866. utils.showLog("创建原生广告banner位");
  867. this._nativeBannerNode = cc.instantiate(utils.config.otherconfig.nativeBanner);
  868. this._nativeBannerNode.position = CompatibleTool.position(cc.winSize.width / 2, this._nativeBannerNode.height * this._nativeBannerNode.scaleY / 2);
  869. cc.director.getScene().addChild(this._nativeBannerNode, 1000);
  870. // }
  871. if (this._nativeBannerNode) {
  872. this._nativeBannerNode.active = true;
  873. let nativeBanner: YZ_NativeBanner = this._nativeBannerNode.getComponent("YZ_NativeBanner");
  874. if (nativeBanner) {
  875. nativeBanner.init(nativeBannerAd, data, this.getNativeBannerInfo());
  876. } else {
  877. utils.showLog("NativeBanner组件不存在!");
  878. }
  879. } else {
  880. utils.showLog("原生广告banner位没有创建!");
  881. }
  882. }
  883. }
  884. }
  885. /**
  886. * 显示原生插屏组件
  887. */
  888. _showNativeInsert(nativeInsertAd: any, data: any) {
  889. if (PlatUtils.IsVIVO) {
  890. if (data) {
  891. utils.showLog("显示原生插屏");
  892. if ((!cc.isValid(this._nativeInsertNode)) || !this._nativeInsertNode && utils.config.otherconfig.nativeInsert) {
  893. utils.showLog("创建原生插屏广告位");
  894. this._nativeInsertNode = cc.instantiate(utils.config.otherconfig.nativeInsert);
  895. this._nativeInsertNode.position = CompatibleTool.position(cc.winSize.width / 2, cc.winSize.height / 2);
  896. cc.director.getScene().addChild(this._nativeInsertNode, 9999);
  897. }
  898. if (this._nativeInsertNode) {
  899. let nativeInsert = this._nativeInsertNode.getComponent("YZ_NativeInsert");
  900. if (nativeInsert) {
  901. this._nativeInsertAdShowCount = 0;
  902. nativeInsert.init(nativeInsertAd, data);
  903. } else {
  904. utils.showLog("NativeInsert组件不存在!");
  905. }
  906. } else {
  907. utils.showLog("原生广告插屏位没有创建!");
  908. }
  909. if (!this.ServerConfig.show_insert_hide_banner || this.ServerConfig.show_insert_hide_banner != "false") {
  910. utils.showLog("原生广告插屏展示后隐藏Banner!");
  911. this.HideBanner();
  912. }
  913. }
  914. }
  915. }
  916. public ShowCloseBtnBanner(location: BannerLocation = BannerLocation.Home, args: any) {
  917. utils.showLog("ShowCloseBtnBanner >>>>>>>>>.");
  918. let isMoveBtn = 0;
  919. let btn = args.closeBtn;
  920. let winHeight = cc.winSize.height;
  921. if (this.ServerConfig) {
  922. if (this.ServerConfig.is_move_btn) {
  923. isMoveBtn = this.ServerConfig.is_move_btn;
  924. }
  925. utils.showLog(isMoveBtn == 0 ? "显示banner,且按钮在上面" : `显示Banner,按钮居底部且${isMoveBtn}毫秒后移动`);
  926. // if (!isMoveBtn || isMoveBtn == 0) {
  927. // btn.y = -(winHeight / 2) + btn.height;
  928. // this.ShowBanner(location, args);
  929. // } else {
  930. setTimeout(() => {
  931. // if (!this._bannerIsHide) {
  932. utils.showLog("延迟调用关闭按钮的Banner >>>>");
  933. this.ShowBanner(location, args);
  934. // } else {
  935. // utils.showLog("当前Banner为隐藏状态,关闭按钮的Banner不显示 >>>>");
  936. // }
  937. var adY = 240;
  938. utils.showLog('utils - adY:' + adY);
  939. if (adY > 0 && btn) {
  940. btn.y = -(winHeight / 2 - adY) + btn.height;
  941. utils.showLog("btnClose.y" + btn.y);
  942. }
  943. }, isMoveBtn);
  944. // }
  945. }
  946. }
  947. // {"adList":[{"adId":0,"title":"快手","desc":"手机总是这么卡,如何解决","icon":"http://imgwsdl.vivo.com.cn/appstore/developer/icon/20190918/201909181113001599041.png",
  948. // "imgUrlList":["http://ads-marketing-vivofs.vivo.com.cn/NtBrJ9dueygDLoz8/material/201908/78707646401346ca9fc8398ff79850d220190809.jpg"],
  949. // "logoUrl":"","creativeType":0,"interactionType":2},
  950. // {"adId":1,"title":"快手","desc":"手机总是这么卡,如何解决","icon":"http://imgwsdl.vivo.com.cn/appstore/developer/icon/20190918/201909181113001599041.png",
  951. // "imgUrlList":["http://ads-marketing-vivofs.vivo.com.cn/NtBrJ9dueygDLoz8/material/201908/78707646401346ca9fc8398ff79850d220190809.jpg"],"logoUrl":"","creativeType":0,"interactionType":2},
  952. // {"adId":2,"title":"快手","desc":"手机总是这么卡,如何解决","icon":"http://imgwsdl.vivo.com.cn/appstore/developer/icon/20190918/201909181113001599041.png","imgUrlList":["http://ads-marketing-vivofs.vivo.com.cn/NtBrJ9dueygDLoz8/material/201908/78707646401346ca9fc8398ff79850d220190809.jpg
  953. _checkNativeDataValid(data: any) {
  954. if (!data) {
  955. return false;
  956. }
  957. return (data.icon || (data.imgUrlList && data.imgUrlList.length > 0));
  958. }
  959. _checkNativeInsertDataValid(data: any) {
  960. if (!data) {
  961. return false;
  962. }
  963. return (data.icon || (data.imgUrlList && data.imgUrlList.length > 0));
  964. }
  965. _checkNativeSignDataValid(data: any) {
  966. if (!data) {
  967. return false;
  968. }
  969. return (data.imgUrlList && data.imgUrlList.length > 0);
  970. }
  971. _nativeTryGameNode: cc.Node = null;
  972. /**
  973. * 显示浮窗广告挂件
  974. * @param params
  975. * ```
  976. * {
  977. * group:string
  978. * left:number
  979. * bottom:number
  980. * scale:number
  981. * parent:cc.Node
  982. * }
  983. * ```
  984. * @returns 生成的组件
  985. */
  986. public createNativeTryGameWidget(params: any = null) {
  987. if (this._nativeTryGameNode && cc.isValid(this._nativeTryGameNode)) {
  988. this._nativeTryGameNode.destroy();
  989. }
  990. let node = cc.instantiate(utils.config.otherconfig.nativeTryGameWidget);
  991. if (node) {
  992. this._nativeTryGameNode = node;
  993. this._nativeTryGameNode.zIndex = 9999;
  994. }
  995. node = this._nativeTryGameNode;
  996. let widget: cc.Widget = node.getComponent(cc.Widget);
  997. if (params) {
  998. if (params.group) {
  999. node.group = params.group;
  1000. }
  1001. if (params.scale != null) {
  1002. node.scale = params.scale;
  1003. }
  1004. if (params.top != null) {
  1005. widget.isAlignTop = true;
  1006. widget.isAlignBottom = false;
  1007. widget.top = params.top;
  1008. } else if (params.bottom != null) {
  1009. widget.isAlignTop = false;
  1010. widget.isAlignBottom = true;
  1011. widget.bottom = params.bottom;
  1012. }
  1013. if (params.left != null) {
  1014. widget.isAlignLeft = true;
  1015. widget.isAlignRight = false;
  1016. widget.left = params.left;
  1017. } else if (params.right != null) {
  1018. widget.isAlignLeft = false;
  1019. widget.isAlignRight = true;
  1020. widget.right = params.right;
  1021. }
  1022. if (params.parent != null) {
  1023. node.parent = params.parent;
  1024. }
  1025. }
  1026. widget.updateAlignment();
  1027. node.active = true;
  1028. if (this._nativeTryGameNode) {
  1029. this._nativeTryGameNode.getComponent(NativeTryGamesWidget).init();
  1030. }
  1031. }
  1032. /**
  1033. * 隐藏浮动试玩挂件
  1034. */
  1035. public hideNativeTryGameWidget() {
  1036. if (this._nativeTryGameNode && cc.isValid(this._nativeTryGameNode)) {
  1037. this._nativeTryGameNode.destroy();
  1038. }
  1039. }
  1040. signleNativeAd: cc.Node = null;
  1041. /**
  1042. * 创建结算页面推广组件
  1043. */
  1044. public ShowSingleNativeAd(params?: any) {
  1045. if (utils.config.otherconfig.singleNativeAd) {
  1046. if (this.signleNativeAd && cc.isValid(this.signleNativeAd)) {
  1047. this.signleNativeAd.destroy();
  1048. }
  1049. this.signleNativeAd = cc.instantiate(utils.config.otherconfig.singleNativeAd);
  1050. let nativeItem: YZ_NativeItem = this.signleNativeAd.getComponent("YZ_NativeItem");
  1051. nativeItem.showType = 2;
  1052. nativeItem.params = params;
  1053. this._curNativeItem = nativeItem;
  1054. if (params && params.parent) {
  1055. params.parent.addChild(this.signleNativeAd, cc.macro.MAX_ZINDEX);
  1056. }
  1057. this.createNativeAd();
  1058. utils.showLog("单个原生广告创建成功!");
  1059. return this.signleNativeAd;
  1060. } else {
  1061. utils.showLog("未找到预制体 singleNativeAd, 请查看CommonUtils组件上是否赋值!");
  1062. return null;
  1063. }
  1064. }
  1065. _singleAdCreateTime: number = 0;
  1066. /**
  1067. * 创建单个原生广告
  1068. * @param completeCallback
  1069. */
  1070. createNativeAd(params: any = null, nativeItem?: YZ_NativeItem) {
  1071. utils.showLog("_createNativeAd >>>>>");
  1072. if (PlatUtils.IsVIVO) {
  1073. if (nativeItem) {
  1074. this._curNativeItem = nativeItem;
  1075. }
  1076. let refreshTime = this.ServerConfig.st_native_ad_refresh_time ? this.ServerConfig.st_native_ad_refresh_time : 0;
  1077. if (this._nativeAdObject && this._nativeAdObject.data && !this._nativeAdObject.is_reportClick && (new Date().getTime() - this._singleAdCreateTime) / 1000 < refreshTime) {
  1078. utils.showLog(`当前原生广告的展示时间${(new Date().getTime() - this._singleAdCreateTime) / 1000}秒,未达刷新时间${refreshTime}秒限制,直接使用上一次数据!`);
  1079. this._showNativeAd();
  1080. return;
  1081. }
  1082. utils.showLog("_curPosIdIndexSingleNative:" + this._curPosIdIndexSingleNative + " #this._nativeSingleAd.length" + this._nativeSingleAd.length);
  1083. let nativeSingleAd = this._nativeSingleAd[this._curPosIdIndexSingleNative];
  1084. if (!nativeSingleAd) {
  1085. if (utils.config.vivoconfig.nativeSingleAdIds
  1086. && utils.config.vivoconfig.nativeSingleAdIds[this._curPosIdIndexSingleNative]) {
  1087. utils.showLog("创建原生广告。 posId:" + utils.config.vivoconfig.nativeSingleAdIds[this._curPosIdIndexSingleNative]);
  1088. //@ts-ignore
  1089. nativeSingleAd = qg.createNativeAd({
  1090. posId: utils.config.vivoconfig.nativeSingleAdIds[this._curPosIdIndexSingleNative]
  1091. });
  1092. if (nativeSingleAd) {
  1093. this._nativeSingleAd.push(nativeSingleAd);
  1094. nativeSingleAd.onLoad((res) => {
  1095. if (res && res.adList && res.adList.length > 0) {
  1096. utils.showLog("原生广告资源拉取成功!");
  1097. utils.showLog(JSON.stringify(res));
  1098. res = JSON.parse(JSON.stringify(res));
  1099. let data = res.adList[0];
  1100. if (this._checkNativeSignDataValid(data)) {
  1101. this._singleAdCreateTime = new Date().getTime();
  1102. this._curPosIdIndexSingleNative = 0;
  1103. this._nativeData = data;
  1104. this._nativeAd = nativeSingleAd;
  1105. // utils.showLog("callback >>>", callback);
  1106. // callback(this.getNativeAdData());
  1107. this._showNativeAd();
  1108. return;
  1109. } else {
  1110. utils.showLog("原生广告资源不合法!");
  1111. }
  1112. }
  1113. utils.showLog("原生广告资源出错!");
  1114. this._curPosIdIndexSingleNative++;
  1115. if (this._curPosIdIndexSingleNative < utils.config.vivoconfig.nativeSingleAdIds.length) {
  1116. this.createNativeAd();
  1117. } else {
  1118. this._curPosIdIndexSingleNative = 0;
  1119. // 原生广告遍历完毕
  1120. utils.showLog("单个原生广告ID遍历完毕,无法展示!");
  1121. }
  1122. });
  1123. nativeSingleAd.onError((err) => {
  1124. utils.showLog("原生单个广告资源拉取失败!" + err.code + err.msg);
  1125. this._curPosIdIndexSingleNative++;
  1126. if (this._curPosIdIndexSingleNative < utils.config.vivoconfig.nativeSingleAdIds.length) {
  1127. this.createNativeAd();
  1128. } else {
  1129. this._curPosIdIndexSingleNative = 0;
  1130. // 原生广告遍历完毕
  1131. utils.showLog("单个原生广告ID遍历完毕,无法展示!");
  1132. }
  1133. });
  1134. }
  1135. }
  1136. }
  1137. if (nativeSingleAd) {
  1138. utils.showLog("nativeSingleAd reload");
  1139. nativeSingleAd.load();
  1140. } else {
  1141. this._curPosIdIndexSingleNative++;
  1142. if (utils.config.vivoconfig.nativeSingleAdIds && this._curPosIdIndexSingleNative < utils.config.vivoconfig.nativeSingleAdIds.length) {
  1143. this.createNativeAd();
  1144. } else {
  1145. this._curPosIdIndexSingleNative = 0;
  1146. // 原生广告遍历完毕
  1147. utils.showLog("单个原生广告ID 遍历完毕,无法展示!");
  1148. }
  1149. }
  1150. }
  1151. }
  1152. _showNativeAd() {
  1153. if (this._curNativeItem && cc.isValid(this._curNativeItem.node)) {
  1154. this._curNativeItem.init(this.getNativeAdData());
  1155. }
  1156. }
  1157. /**
  1158. * 获取原生广告数据
  1159. * @param args
  1160. */
  1161. getNativeAdData() {
  1162. if (!this._nativeAdObject) {
  1163. this._nativeAdObject = new YZ_NativeAdObject();
  1164. }
  1165. this._nativeAdObject._nativeObj = this._nativeAd;
  1166. if (this._nativeAdObject.data) {
  1167. if (this._nativeAdObject.data.adId != this._nativeData.adId) {
  1168. this._nativeAdObject.is_reportClick = false;
  1169. this._nativeAdObject.is_reportShow = false;
  1170. }
  1171. }
  1172. this._nativeAdObject.data = this._nativeData;
  1173. // setTimeout(() => {
  1174. // utils.showLog("延迟两秒重新请求原生广告数据");
  1175. // this._createNativeAd(args);
  1176. // }, 2000);
  1177. if (this._nativeData && this._nativeAd) {
  1178. // this._nativeData = null;
  1179. utils.showLog("获取原生数据 >>>", this._nativeData, "...", this._nativeAdObject)
  1180. return this._nativeAdObject;
  1181. }
  1182. return null;
  1183. }
  1184. _customAd: any = null;
  1185. _refreshCustomAdTimerTask = null;
  1186. _isHide: boolean = false;
  1187. _lastLocation: string = "";
  1188. /**
  1189. * 显示模版广告
  1190. * @param parmas
  1191. */
  1192. public showCustomAd(parme?: any) {
  1193. if (utils.config.vivoconfig.customAdInfos.length <= 0) {
  1194. utils.showLog("服务器配置未配置模版广告!");
  1195. return;
  1196. }
  1197. //@ts-ignore
  1198. if (qg.createCustomAd) {
  1199. clearTimeout(this._refreshCustomAdTimerTask);
  1200. let customAdInfo = utils.config.vivoconfig.getCustomAdInfoInfo(parme.location);
  1201. utils.showLog("当前位置 obj >>>", customAdInfo.customAdObj);
  1202. if (customAdInfo.is_show_ad != "true") {
  1203. utils.showLog("当前位置服务器配置为不显示模版广告!");
  1204. return;
  1205. }
  1206. if (this._lastLocation != parme.location) {
  1207. this.hideCustomAd();
  1208. }
  1209. if (customAdInfo.customAdObj != null) {
  1210. this._customAd = customAdInfo.customAdObj;
  1211. if (!customAdInfo.customAdObj.isShow()) {
  1212. utils.showLog("当前位置上次展示过原生模版,当前重新显示出来!");
  1213. customAdInfo.customAdObj.show().then(() => {
  1214. if (customAdInfo.hide_banner == "true") {
  1215. this.HideBanner();
  1216. }
  1217. utils.showLog("重新展示成功!");
  1218. }).catch((err) => {
  1219. customAdInfo.customAdObj.destroy();
  1220. customAdInfo.customAdObj = null;
  1221. utils.showLog('重新展示原生模板广告失败', JSON.stringify(err));
  1222. this.showCustomAd(parme);
  1223. });
  1224. } else {
  1225. utils.showLog("当前位置的模版正在显示中,不执行加载!");
  1226. }
  1227. if (customAdInfo.refresh_time > 0) {
  1228. this._refreshCustomAdTimerTask = setTimeout(() => {
  1229. utils.showLog(`定时${customAdInfo.refresh_time}秒刷新原生模版`);
  1230. this.showCustomAd(parme)
  1231. }, customAdInfo.refresh_time * 1000);
  1232. }
  1233. return;
  1234. }
  1235. this._lastLocation = parme.location;
  1236. this._isHide = false;
  1237. let style: any = {};
  1238. if (customAdInfo.top > 0) {
  1239. style.top = customAdInfo.top / cc.winSize.height * this.SysInfo.screenHeight;
  1240. } else if (customAdInfo.bottom > 0) {
  1241. style.top = this.SysInfo.screenHeight - customAdInfo.height - customAdInfo.bottom / cc.winSize.height * this.SysInfo.screenHeight;
  1242. }
  1243. if (customAdInfo.left > 0) {
  1244. style.left = customAdInfo.left / cc.winSize.width * this.SysInfo.screenWidth;
  1245. } else if (customAdInfo.right > 0) {
  1246. style.left = this.SysInfo.screenWidth - customAdInfo.width - customAdInfo.right / cc.winSize.width * this.SysInfo.screenWidth;
  1247. }
  1248. utils.showLog("custom ad style>>" + JSON.stringify(style));
  1249. //@ts-ignore
  1250. var _customAd = qg.createCustomAd({
  1251. posId: customAdInfo.id,
  1252. style: style
  1253. });
  1254. _customAd.onError(err => {
  1255. try {
  1256. utils.showLog("原生模板广告加载失败", JSON.stringify(err));
  1257. } catch (erro) {
  1258. console.log("模版异常:#errMsg=" + erro);
  1259. }
  1260. });
  1261. _customAd.onClose(() => {
  1262. utils.showLog("原生模板广告 onClose");
  1263. _customAd.destroy();
  1264. _customAd = null;
  1265. customAdInfo.customAdObj = null;
  1266. })
  1267. _customAd.onLoad(() => {
  1268. _customAd.offLoad();
  1269. utils.showLog("原生模板广告加载成功!");
  1270. this._customAd = _customAd;
  1271. customAdInfo.customAdObj && customAdInfo.customAdObj.destroy();
  1272. customAdInfo.customAdObj = _customAd;
  1273. if (!this._isHide) {
  1274. _customAd.show().then(() => {
  1275. if (customAdInfo.hide_banner == "true") {
  1276. this.HideBanner();
  1277. }
  1278. utils.showLog('原生模板广告展示完成!');
  1279. }).catch((err) => {
  1280. utils.showLog('原生模板广告展示失败', JSON.stringify(err));
  1281. })
  1282. } else {
  1283. utils.showLog('原生模板广告调用了隐藏,当前广告不显示!');
  1284. }
  1285. });
  1286. if (customAdInfo.refresh_time > 0) {
  1287. this._refreshCustomAdTimerTask = setTimeout(() => {
  1288. utils.showLog(`定时${customAdInfo.refresh_time}秒刷新原生模版`);
  1289. this.showCustomAd(parme)
  1290. }, customAdInfo.refresh_time * 1000);
  1291. }
  1292. } else {
  1293. utils.showLog("当前平台不支持原生模版广告!");
  1294. }
  1295. }
  1296. /**
  1297. * 显示模版广告
  1298. * @param parmas
  1299. */
  1300. public showCustomAdV2(parme?: any) {
  1301. if (utils.config.vivoconfig.customAdInfos.length <= 0) {
  1302. utils.showLog("服务器配置未配置模版广告!");
  1303. return;
  1304. }
  1305. //@ts-ignore
  1306. if (qg.createCustomAd) {
  1307. clearTimeout(this._refreshCustomAdTimerTask);
  1308. let customAdInfo = utils.config.vivoconfig.getCustomAdInfoInfo(parme.location);
  1309. utils.showLog("当前位置的广告对象 >>>", customAdInfo.customAdObj);
  1310. if (customAdInfo.is_show_ad != "true") {
  1311. utils.showLog("当前位置服务器配置为不显示模版广告!");
  1312. return;
  1313. }
  1314. var isRefresh = true;
  1315. if (this._lastLocation != parme.location) {
  1316. this.hideCustomAd();
  1317. } else {
  1318. if (customAdInfo.customAdObj != null) {
  1319. if (!customAdInfo.customAdObj.isShow()) {
  1320. utils.showLog("当前位置上次展示过原生模版,当前重新显示出来!");
  1321. isRefresh = false;
  1322. customAdInfo.customAdObj.show().then(() => {
  1323. if (customAdInfo.hide_banner == "true") {
  1324. this.HideBanner();
  1325. }
  1326. utils.showLog("重新展示成功!");
  1327. }).catch((err) => {
  1328. isRefresh = true;
  1329. customAdInfo.customAdObj.destroy();
  1330. customAdInfo.customAdObj = null;
  1331. utils.showLog('重新展示原生模板广告失败', JSON.stringify(err));
  1332. // this.showCustomAd(parme);
  1333. });
  1334. } else {
  1335. utils.showLog("当前位置的模版正在显示中,不执行加载!");
  1336. }
  1337. // if (customAdInfo.refresh_time > 0) {
  1338. // this._refreshCustomAdTimerTask = setTimeout(() => {
  1339. // utils.showLog(`定时${customAdInfo.refresh_time}秒刷新原生模版`);
  1340. // this.showCustomAd(parme)
  1341. // }, customAdInfo.refresh_time * 1000);
  1342. // }
  1343. // return;
  1344. }
  1345. }
  1346. this._lastLocation = parme.location;
  1347. this._isHide = false;
  1348. let style: any = {};
  1349. if (customAdInfo.top > 0) {
  1350. style.top = customAdInfo.top / cc.winSize.height * this.SysInfo.screenHeight;
  1351. } else if (customAdInfo.bottom > 0) {
  1352. style.top = this.SysInfo.screenHeight - customAdInfo.height - customAdInfo.bottom / cc.winSize.height * this.SysInfo.screenHeight;
  1353. }
  1354. if (customAdInfo.left > 0) {
  1355. style.left = customAdInfo.left / cc.winSize.width * this.SysInfo.screenWidth;
  1356. } else if (customAdInfo.right > 0) {
  1357. style.left = this.SysInfo.screenWidth - customAdInfo.width - customAdInfo.right / cc.winSize.width * this.SysInfo.screenWidth;
  1358. }
  1359. utils.showLog("custom ad style>>" + JSON.stringify(style));
  1360. //@ts-ignore
  1361. var _customAd = qg.createCustomAd({
  1362. posId: customAdInfo.id,
  1363. style: style
  1364. });
  1365. _customAd.onError(err => {
  1366. try {
  1367. utils.showLog("原生模板广告加载失败", JSON.stringify(err));
  1368. } catch (erro) {
  1369. console.log("模版异常:#errMsg=" + erro);
  1370. }
  1371. });
  1372. _customAd.onClose(() => {
  1373. utils.showLog("原生模板广告 onClose");
  1374. _customAd.destroy();
  1375. _customAd = null;
  1376. customAdInfo.customAdObj = null;
  1377. })
  1378. _customAd.onLoad(() => {
  1379. utils.showLog("原生模板广告加载成功!");
  1380. this._customAd = _customAd;
  1381. customAdInfo.customAdObj && customAdInfo.customAdObj.destroy();
  1382. customAdInfo.customAdObj = _customAd;
  1383. if (!this._isHide && isRefresh) {
  1384. _customAd.show().then(() => {
  1385. if (customAdInfo.hide_banner == "true") {
  1386. this.HideBanner();
  1387. }
  1388. utils.showLog('原生模板广告展示完成!');
  1389. }).catch((err) => {
  1390. utils.showLog('原生模板广告展示失败', JSON.stringify(err));
  1391. })
  1392. } else {
  1393. utils.showLog('原生模板广告调用了隐藏或者当前不刷新广告,当前广告不显示!');
  1394. }
  1395. });
  1396. if (customAdInfo.refresh_time > 0) {
  1397. this._refreshCustomAdTimerTask = setTimeout(() => {
  1398. utils.showLog(`定时${customAdInfo.refresh_time}秒刷新原生模版`);
  1399. this.showCustomAd(parme)
  1400. }, customAdInfo.refresh_time * 1000);
  1401. }
  1402. } else {
  1403. utils.showLog("当前平台不支持原生模版广告!");
  1404. }
  1405. }
  1406. /**
  1407. * 隐藏模版广告
  1408. */
  1409. public hideCustomAd() {
  1410. this._isHide = true;
  1411. clearTimeout(this._refreshCustomAdTimerTask);
  1412. if (this._customAd) {
  1413. this._customAd.hide();
  1414. }
  1415. }
  1416. }