AdAgentHuaWei.ts 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008
  1. import AdAgent from "./AdAgent";
  2. import { BannerLocation } from "./YZ_Constant";
  3. import PlatUtils from "./PlatUtils";
  4. import { utils } from "./Utils";
  5. import RewardInsert from "./RewardInsert";
  6. import YZ_StatementRecommentAd from "./YZ_StatementRecommentAd";
  7. import YZ_NativeAdObject from "./YZ_NativeAdObject";
  8. import YZ_NativeItem from "./YZ_NativeItem";
  9. import NativeTryGamesWidget from "./NativeTryGamesWidget";
  10. import YZ_NativeBanner from "./YZ_NativeBanner";
  11. import { BannerIdInfo, NativeBannerInfo } from "./CommonConfig";
  12. import CompatibleTool from "./CompatibleTool";
  13. const { ccclass, property } = cc._decorator;
  14. @ccclass
  15. export default class AdAgentHuaWei extends AdAgent {
  16. //@ts-ignore
  17. huawei: any = window.qg;
  18. _bannerAd: any = null;
  19. _insertAd: any = null;
  20. _videoAd: any = null;
  21. _nativeBannerAd: any[] = [];
  22. _nativeInsertAd: any[] = [];
  23. _nativeSingleAd: any[] = [];
  24. _nativeAd: any = null;
  25. // 广告组件是否初始化成功
  26. _isAdInit: boolean = false;
  27. _isBannerShow: boolean = false;
  28. _isInsertShow: boolean = false;
  29. // 当前位置id的索引
  30. _curPosIdIndexNativeBanner: number = 0;
  31. _curPosIdIndexNativeInser: number = 0;
  32. _curPosIdIndexSingleNative: number = 0;
  33. _nativeData: any = null;
  34. _nativeInsertData: any = null;
  35. _nativeBannerNode: cc.Node = null;
  36. _nativeInsertNode: cc.Node = null;
  37. _videoCallback: Function = null;
  38. _videoLoaded: boolean = null;
  39. _miniBannerHeight: number = 0;
  40. _showBannerCallBack: Function = null;
  41. _nativeAdObject: YZ_NativeAdObject = null;
  42. //插屏显示次数
  43. _insertShowCount: number = 0;
  44. _insertLastShowTime: number = 0;
  45. _curNativeItem: YZ_NativeItem = null;
  46. _isNativeBannerShow: boolean = false; //原生广告展示,调用的隐藏
  47. lastLastShowVideoTime: number = 0; //最后一次显示视频时间
  48. _bannerHideCount: number = 0;
  49. canShowNativeBanner = true; //原生banner是否展示
  50. public get ServerConfig() {
  51. if (utils.Tool_Huawei && utils.Tool_Huawei.ServerConfig) return utils.Tool_Huawei.ServerConfig;
  52. return {};
  53. }
  54. _nativeBannerInfo: NativeBannerInfo = null;
  55. /**
  56. * 获取当前banner配置
  57. */
  58. getNativeBannerInfo() {
  59. if (this._nativeBannerInfo && this._nativeBannerInfo.location == this._curLocation) {
  60. return this._nativeBannerInfo;
  61. }
  62. return utils.config.huaweiConfig.getNativeBannerInfo(this._curLocation);
  63. }
  64. public Init() {
  65. if (PlatUtils.IsHuaWei) {
  66. cc.director.on("IsDataInit", (() => {
  67. this._isDataInit = true;
  68. cc.director.off("IsDataInit");
  69. }), this)
  70. utils.registerServerInitEvent(() => {
  71. utils.showLog("HuaWei 广告代理组件初始化!");
  72. this._initVideoAd();
  73. // this._initInsertAd();
  74. // this._initLocalDate();
  75. }, this)
  76. }
  77. }
  78. bannerType: number = 1;
  79. /**
  80. * banner样式:1代表原生banner,2代表默认banner
  81. */
  82. banner_style_mod: Array<number> = [1, 2];
  83. intersititialType: number = 1;
  84. /**
  85. * 插屏样式:1代表原生插屏,2代表默认插屏,3代表原生icon
  86. */
  87. intersititial_style_mod: Array<number> = [1, 2, 3];
  88. /**
  89. * banner展示次数
  90. */
  91. bannerTimes: number = 0;
  92. /**
  93. * 插屏展示次数
  94. */
  95. intersititialTimes: number = 0;
  96. _initLocalDate() {
  97. if (!this.ServerConfig.banner_style_mod) {
  98. this.ServerConfig.banner_style_mod = [1, 2];
  99. }
  100. if (!this.ServerConfig.intersititial_style_mod) {
  101. this.ServerConfig.intersititial_style_mod = [1, 2, 3];
  102. }
  103. if (!this.ServerConfig.icon_jump_native) {
  104. this.ServerConfig.icon_jump_native = 1;
  105. }
  106. if (!this.ServerConfig.banner_style_level) {
  107. this.ServerConfig.banner_style_level = 2;
  108. }
  109. if (!this.ServerConfig.intersititial_style_level) {
  110. this.ServerConfig.intersititial_style_level = 2;
  111. }
  112. this.ServerConfig.intersititia_delay_show_time = 0;
  113. console.log("插屏延时展示,延时时间:" + this.ServerConfig.intersititia_delay_show_time);
  114. console.log("banner && inter", this.ServerConfig.banner_style_mod, this.ServerConfig.intersititial_style_mod)
  115. };
  116. _nativeIsClose: boolean = false;
  117. _showBannerTimerId: number = 0;
  118. _delayShowBannerId: number = 0;
  119. // nbclr:是否开启强制刷新
  120. //当前显示Banner的位置
  121. _curLocation: BannerLocation = BannerLocation.None;
  122. //启动定时器的时间
  123. _startBannerTimerTask: number = 0;
  124. public ShowBanner(location: BannerLocation = null, args: any = null, isTimeRefresh: boolean = false) {
  125. // return
  126. if (PlatUtils.IsHuaWei) {
  127. if (!this.ServerConfig) {
  128. utils.showLog("未请求到配置文件!");
  129. return;
  130. }
  131. let oldLocation = this._curLocation;
  132. this._curLocation = location;
  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. if (args && args.isRefresh) {
  141. isTimeRefresh = true;
  142. } else if (interval && interval > 0) {
  143. if (refresh_ad_time > interval) {
  144. isTimeRefresh = true;
  145. }
  146. }
  147. // 判断当前位置是不是显示banner
  148. if (this.getNativeBannerInfo().is_show_banner == -1) {
  149. utils.showLog("当前位置配置为不展示banner!");
  150. this.HideBanner(location);
  151. return;
  152. } else {
  153. if (!isTimeRefresh) {
  154. if (this.ServerConfig.nbclr && this.ServerConfig.nbclr == "true" && this._curNativeBannerInfo.nativeBannerAd) {
  155. utils.showLog("服务器配置定时器刷新数据 ");
  156. this._showNativeBanner(this._curNativeBannerInfo.nativeBannerAd, this._curNativeBannerInfo.data);
  157. utils.showLog("开启定时刷新 >>>>>>>>>" + (interval - refresh_ad_time));
  158. clearTimeout(this._showBannerTimerId);
  159. clearTimeout(this._delayShowBannerId);
  160. //@ts-ignore
  161. this._showBannerTimerId = setTimeout(() => {
  162. utils.showLog(`定时${interval}秒调用showbanner`);
  163. this.ShowBanner(this._curLocation, {}, true);
  164. }, (interval - refresh_ad_time) * 1000);
  165. return;
  166. }
  167. }
  168. }
  169. this.canShowNativeBanner = true;
  170. this._nativeIsClose = false;
  171. clearTimeout(this._showBannerTimerId);
  172. clearTimeout(this._delayShowBannerId);
  173. utils.showLog("清理定时器");
  174. if (this.ServerConfig) {
  175. utils.showLog("banner 优先展示 >>>>" + this.ServerConfig.banner_first_ad);
  176. if (interval && interval > 0) {
  177. utils.showLog("开启定时刷新 >>>>>>>>>" + interval);
  178. //@ts-ignore
  179. this._showBannerTimerId = setTimeout(() => {
  180. utils.showLog(`定时${interval}秒调用showbanner`);
  181. this.ShowBanner(this._curLocation, {}, true);
  182. }, interval * 1000);
  183. }
  184. if (this.ServerConfig.banner_style_level) {
  185. if (this.ServerConfig.banner_style_mod) {
  186. if (this.ServerConfig.banner_style_mod[this.bannerType - 1] == 1) {
  187. utils.showLog("banner_style_mod 为 1,展示原生banner");
  188. this._createNativeBannerAd(this._showNativeBanner);
  189. }
  190. else if (this.ServerConfig.banner_style_mod[this.bannerType - 1] == 2) {
  191. utils.showLog("banner_style_mod 为 2,展示默认banner");
  192. this._createMiniGameBannerAd(location);
  193. }
  194. else {
  195. utils.showLog("banner_style_mod 配置出错!")
  196. }
  197. }
  198. else {
  199. utils.showLog("服务器没有配置 banner_style_mod 字段!!")
  200. this.ServerConfig.banner_style_level = 0
  201. this.ShowBanner(location);
  202. }
  203. this.bannerTimes++;
  204. if (this.bannerTimes % this.ServerConfig.banner_style_level == 0) {
  205. this.bannerType++;
  206. this.bannerType = this.bannerType > 2 ? 1 : this.bannerType;
  207. }
  208. }
  209. else if (this.ServerConfig.banner_first_ad) {
  210. if (this.ServerConfig.banner_first_ad.indexOf("native") > -1) {
  211. utils.showLog("优先展示原生Banner广告!");
  212. if (this.ServerConfig.intersititial_first_ad == "native") {
  213. let timeOut = 0;
  214. if (this.ServerConfig.native_banner_delay_time) {
  215. timeOut = this.ServerConfig.native_banner_delay_time;
  216. }
  217. utils.showLog(`因为插屏也是优先展示原生,所以banner延迟显示${timeOut}秒`);
  218. //@ts-ignore
  219. this._delayShowBannerId = setTimeout(() => {
  220. this._createNativeBannerAd(this._showNativeBanner);
  221. }, timeOut * 1000);
  222. } else {
  223. this._createNativeBannerAd(this._showNativeBanner);
  224. }
  225. } else {
  226. utils.showLog("优先展示小游戏Banner广告!");
  227. this._createMiniGameBannerAd(location);
  228. }
  229. } else {
  230. this._createNativeBannerAd(this._showNativeBanner);
  231. }
  232. } else {
  233. utils.showLog("服务器配置数据未初始化!");
  234. }
  235. }
  236. }
  237. public HideBanner(location: BannerLocation = null) {
  238. if (PlatUtils.IsHuaWei) {
  239. clearTimeout(this._showBannerTimerId);
  240. clearTimeout(this._delayShowBannerId);
  241. if (this._bannerAd) {
  242. utils.showLog("隐藏小游戏Banner");
  243. this._bannerAd.hide();
  244. }
  245. if (this._nativeBannerNode) {
  246. utils.showLog("隐藏原生Banner");
  247. this._nativeBannerNode.active = false;
  248. }
  249. }
  250. }
  251. tryGameAdArr: any[] = [];
  252. public showNativeTryGameWidget() {
  253. if (PlatUtils.IsHuaWei) {
  254. utils.showLog("显示原生icon nativeNeedChange=", utils.nativeNeedChange, " utils.tryGameDate", utils.tryGameDate, "utils.config.huaweiConfig.nativeTryGameIds", utils.config.huaweiConfig.nativeTryGameIds)
  255. if (utils.nativeNeedChange || !utils.tryGameDate) {
  256. if (utils.config.huaweiConfig.nativeTryGameIds) {
  257. let len = utils.config.huaweiConfig.nativeTryGameIds.length;
  258. utils.showLog("len:" + len);
  259. for (let i = 0; i < len; i++) {
  260. if (!this.tryGameAdArr[i]) {
  261. let posId = utils.config.huaweiConfig.nativeTryGameIds[i];
  262. utils.showLog("创建原生icon。 posId:" + posId);
  263. //@ts-ignore
  264. let tryGameAd = qg.createNativeAd({
  265. adUnitId: posId,
  266. success: (code) => {
  267. console.log("loadNativeAd loadNativeAd : success");
  268. },
  269. fail: (data, code) => {
  270. console.log("loadNativeAd loadNativeAd fail: " + data + "," + code);
  271. },
  272. complete: () => {
  273. console.log("loadNativeAd loadNativeAd : complete");
  274. }
  275. });
  276. if (tryGameAd) {
  277. tryGameAd.onLoad((res) => {
  278. utils.nativeNeedChange = false
  279. console.log("原生icon加载成功");
  280. if (res && res.adList && res.adList.length > 0) {
  281. res = JSON.parse(JSON.stringify(res));
  282. let adId = res.adList[0].adId
  283. utils.showLog("adId:" + adId);
  284. let canAdd = true;
  285. utils.tryGameDate.forEach((element) => {
  286. if (adId == element.date[0].adId) {
  287. canAdd = false;
  288. }
  289. })
  290. if (canAdd) {
  291. utils.tryGameDate.push({ "tryGameAd": tryGameAd, "date": res.adList });
  292. if (utils._nativeTryGameNode) {
  293. utils._nativeTryGameNode.getComponent(NativeTryGamesWidget).init();
  294. }
  295. else {
  296. this.createNativeTryGameWidget();
  297. }
  298. }
  299. utils.showLog("原生icon广告资源拉取成功,是否可以添加广告" + canAdd);
  300. utils.showLog(JSON.stringify(res));
  301. }
  302. });
  303. tryGameAd.onError((err) => {
  304. utils.showLog("原生icon广告资源拉取失败!" + JSON.stringify(err));
  305. utils.nativeNeedChange = true;
  306. if (this.ServerConfig.intersititial_style_level) {
  307. if (this.ServerConfig.intersititial_style_mod[this.intersititialType - 1] == 3) {
  308. utils.showLog("开始展示原生插屏!");
  309. utils.delayCall(this.nativeInserAdDelayCall.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  310. }
  311. }
  312. tryGameAd.destroy();
  313. });
  314. tryGameAd.load();
  315. this.tryGameAdArr[i] = tryGameAd;
  316. }
  317. } else {
  318. this.tryGameAdArr[i].load();
  319. }
  320. }
  321. }
  322. }
  323. this.createNativeTryGameWidget();
  324. }
  325. }
  326. _nativeTryGameNode: cc.Node = null;
  327. /**
  328. * 显示浮窗广告挂件
  329. * @param params
  330. * ```
  331. * {
  332. * group:string
  333. * left:number
  334. * bottom:number
  335. * scale:number
  336. * parent:cc.Node
  337. * }
  338. * ```
  339. * @returns 生成的组件
  340. */
  341. public createNativeTryGameWidget(params: any = null) {
  342. if (this._nativeTryGameNode && cc.isValid(this._nativeTryGameNode)) {
  343. this._nativeTryGameNode.destroy();
  344. }
  345. let node = cc.instantiate(utils.config.otherconfig.nativeTryGameWidget);
  346. if (node) {
  347. this._nativeTryGameNode = node;
  348. this._nativeTryGameNode.zIndex = 9999;
  349. }
  350. node = this._nativeTryGameNode;
  351. let widget: cc.Widget = node.getComponent(cc.Widget);
  352. if (params) {
  353. if (params.group) {
  354. node.group = params.group;
  355. }
  356. if (params.scale != null) {
  357. node.scale = params.scale;
  358. }
  359. if (params.top != null) {
  360. widget.isAlignTop = true;
  361. widget.isAlignBottom = false;
  362. widget.top = params.top;
  363. } else if (params.bottom != null) {
  364. widget.isAlignTop = false;
  365. widget.isAlignBottom = true;
  366. widget.bottom = params.bottom;
  367. }
  368. if (params.left != null) {
  369. widget.isAlignLeft = true;
  370. widget.isAlignRight = false;
  371. widget.left = params.left;
  372. } else if (params.right != null) {
  373. widget.isAlignLeft = false;
  374. widget.isAlignRight = true;
  375. widget.right = params.right;
  376. }
  377. if (params.parent != null) {
  378. node.parent = params.parent;
  379. }
  380. widget.updateAlignment();
  381. }
  382. else {
  383. node.parent = cc.director.getScene();
  384. if (utils.otherConfig && utils.otherConfig.group) {
  385. node.group = utils.otherConfig.group;
  386. }
  387. widget.enabled = false;
  388. node.setPosition(cc.v2(cc.winSize.width / 2, cc.winSize.height / 2));
  389. }
  390. node.active = true;
  391. if (this._nativeTryGameNode) {
  392. this._nativeTryGameNode.getComponent(NativeTryGamesWidget).init();
  393. }
  394. }
  395. /**
  396. * 隐藏浮动试玩挂件
  397. */
  398. public hideNativeTryGameWidget() {
  399. if (this._nativeTryGameNode && cc.isValid(this._nativeTryGameNode)) {
  400. this._nativeTryGameNode.destroy();
  401. }
  402. }
  403. _isDataInit: boolean = false;
  404. _canShowNativeSplashView(callBack) {
  405. if (!this._isDataInit) {
  406. utils.showLog("广告数据未初始化完成,设置回调>>>>>");
  407. cc.director.off("IsDataInit");
  408. let call = callBack;
  409. cc.director.on("IsDataInit", (() => {
  410. utils.showLog("回调成功,展示开屏广告>>>>>");
  411. this._isDataInit = true;
  412. this.showNativeSplashView(call);
  413. cc.director.off("IsDataInit");
  414. }), this)
  415. return false;
  416. }
  417. return true;
  418. }
  419. public showNativeSplashView(callBack: Function = null) {
  420. if (!this._canShowNativeSplashView(callBack)) return;
  421. if (PlatUtils.IsHuaWei) {
  422. // utils.config.huaweiConfig.nativeSplashId = "testu7m3hc4gvm";
  423. utils.showLog("显示开屏广告,广告ID ", utils.config.huaweiConfig.nativeSplashId)
  424. if (utils.config.huaweiConfig.nativeSplashId) {
  425. let posId = utils.config.huaweiConfig.nativeSplashId;
  426. utils.showLog("创建原生开屏广告。 posId:" + posId);
  427. //@ts-ignore
  428. let splashView = qg.createNativeAd({
  429. adUnitId: posId,
  430. success: (code) => {
  431. console.log("loadNativeAd loadNativeAd : success");
  432. },
  433. fail: (data, code) => {
  434. console.log("loadNativeAd loadNativeAd fail: " + data + "," + code);
  435. },
  436. complete: () => {
  437. console.log("loadNativeAd loadNativeAd : complete");
  438. }
  439. });
  440. if (splashView) {
  441. splashView.onLoad((res) => {
  442. if (res && res.adList && res.adList.length > 0) {
  443. res = JSON.parse(JSON.stringify(res));
  444. let adId = res.adList[0].adId
  445. utils.showLog("adId:" + adId);
  446. utils.showLog("原生开屏广告资源拉取成功:" + JSON.stringify(res));
  447. let data = res.adList[0];
  448. this._creatNativeSplashView(splashView, data);
  449. if (callBack) {
  450. cc.director.on("SplashViewOff", (() => {
  451. callBack();
  452. cc.director.off("SplashViewOff");
  453. }), this);
  454. }
  455. }
  456. });
  457. splashView.onError((err) => {
  458. if (callBack) {
  459. callBack();
  460. }
  461. utils.showLog("原生开屏广告资源拉取失败!" + JSON.stringify(err));
  462. splashView.destroy();
  463. });
  464. splashView.load();
  465. }
  466. else {
  467. if (callBack) {
  468. callBack();
  469. }
  470. utils.showLog("原生开屏广告创建失败!");
  471. }
  472. }
  473. else {
  474. if (callBack) {
  475. callBack();
  476. }
  477. utils.showLog("原生开屏广告ID配置有误!!");
  478. }
  479. }
  480. }
  481. _nativeSplashNode: cc.Node = null;
  482. /**
  483. * 显示原生开屏组件
  484. */
  485. _creatNativeSplashView(splashView: any, data: any) {
  486. if (PlatUtils.IsHuaWei) {
  487. if (data) {
  488. if (!cc.isValid(this._nativeSplashNode) && utils.config.otherconfig.nativeSplashView) {
  489. utils.showLog("创建原生插屏广告位");
  490. this._nativeSplashNode = cc.instantiate(utils.config.otherconfig.nativeSplashView);
  491. this._nativeSplashNode.position = CompatibleTool.position(cc.winSize.width / 2, cc.winSize.height / 2);
  492. cc.director.getScene().addChild(this._nativeSplashNode, 999999);
  493. }
  494. if (this._nativeSplashNode) {
  495. let nativeSplash = this._nativeSplashNode.getComponent("YZ_NativeSplashView");
  496. if (nativeSplash) {
  497. nativeSplash.init(splashView, data);
  498. // utils.HuaweiTool.countInserShowCount();
  499. // utils.showLog("原生插屏显示成功,当前显示次数=" + utils.HuaweiTool.insertAdShowCounts);
  500. } else {
  501. cc.director.emit("SplashViewOff");
  502. utils.showLog("NativeSplashView组件不存在!");
  503. }
  504. } else {
  505. cc.director.emit("SplashViewOff");
  506. utils.showLog("原生开屏广告没有创建成功!");
  507. }
  508. }
  509. }
  510. }
  511. insertTimeID: number = 0;
  512. public ShowInterstitial(location: BannerLocation = null) {
  513. // utils.delayCall(this.nativeInserAdDelayCall.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  514. if (PlatUtils.IsHuaWei) {
  515. if (this.ServerConfig) {
  516. if (!this.checkInsertAdShow()) return;
  517. if (location && location == BannerLocation.Pause) {
  518. if (this.ServerConfig.intersititial_first_ad == "native") {
  519. utils.showLog("暂停界面不延时展示!");
  520. utils.showLog("优先展示原生插屏广告!");
  521. this.nativeInserAdDelayCall();
  522. } else {
  523. utils.showLog("优先展示小游戏插屏广告!");
  524. this._createMiniGameInsertAd();
  525. }
  526. } else {
  527. if (this.ServerConfig.intersititial_style_level) {
  528. if (this.ServerConfig.intersititial_style_mod) {
  529. if (this.ServerConfig.intersititial_style_mod[this.intersititialType - 1] == 1) {
  530. utils.showLog("intersititial_style_mod 为 1,展示原生插屏");
  531. utils.delayCall(this.nativeInserAdDelayCall.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  532. }
  533. else if (this.ServerConfig.intersititial_style_mod[this.intersititialType - 1] == 2) {
  534. utils.showLog("intersititial_style_mod 为 2,展示默认插屏");
  535. this._createMiniGameInsertAd();
  536. }
  537. else if (this.ServerConfig.intersititial_style_mod[this.intersititialType - 1] == 3) {
  538. utils.showLog("intersititial_style_mod 为 2,展示原生icon");
  539. this.showNativeTryGameWidget();
  540. }
  541. else {
  542. utils.showLog("banner_style_mod 配置出错!")
  543. }
  544. }
  545. else {
  546. utils.showLog("服务器没有配置 banner_style_mod 字段!!")
  547. this.ServerConfig.intersititial_style_level = 0
  548. this.ShowInterstitial(location);
  549. }
  550. setTimeout(() => {
  551. this.intersititialTimes++;
  552. if (this.intersititialTimes % this.ServerConfig.intersititial_style_level == 0) {
  553. this.intersititialType++;
  554. this.intersititialType = this.intersititialType > 3 ? 1 : this.intersititialType;
  555. }
  556. }, 3000);
  557. }
  558. else if (this.ServerConfig.intersititial_first_ad == "native") {
  559. utils.showLog("优先展示原生插屏广告!");
  560. utils.delayCall(this.nativeInserAdDelayCall.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  561. } else {
  562. utils.showLog("优先展示小游戏插屏广告!");
  563. this._createMiniGameInsertAd();
  564. }
  565. }
  566. } else {
  567. utils.showLog("服务器配置数据未初始化!");
  568. }
  569. }
  570. }
  571. _nativeInsertAdShowCount: number = 0;
  572. public nativeInserAdDelayCall() {
  573. // utils.showLog("原生插屏广告: 当前点击次数=" + utils.HuaweiTool.NativeInsertAdClickTimes + "; 点击次数限制=" + this.ServerConfig.intersititia_click_count + "; 展示次数间隔=" + this.ServerConfig.intersititia_show_interval + "; 展示累计=" + this._nativeInsertAdShowCount);
  574. // if (utils.HuaweiTool.NativeInsertAdClickTimes >= (this.ServerConfig.intersititia_click_count || 0)) {
  575. // // 每日点击次数到达上限,限制展示次数
  576. // if (this._nativeInsertAdShowCount >= (this.ServerConfig.intersititia_show_interval || 0)) {
  577. // 可以展示
  578. this._createNativeInsertAd(this._showNativeInsert);
  579. // } else {
  580. // // 不能展示
  581. // this._nativeInsertAdShowCount++;
  582. // }
  583. // } else {
  584. // this._createNativeInsertAd(this._showNativeInsert);
  585. // }
  586. }
  587. public ShowVideo(callback: Function) {
  588. if (PlatUtils.IsHuaWei) {
  589. this._videoCallback = callback;
  590. let curTime: number = new Date().getTime();
  591. let interval: number = (curTime - this.lastLastShowVideoTime) / 1000;
  592. if (interval < 4) {
  593. utils.showLog("视频广告请求间隔小于4秒,直接返回false");
  594. if (this._videoCallback) {
  595. this._videoCallback(false, "暂无视频广告!");
  596. this._videoCallback = null;
  597. }
  598. return;
  599. }
  600. if (this.ServerConfig) {
  601. if (!this._videoAd) {
  602. this._initVideoAd();
  603. }
  604. if (this._videoAd && this._videoLoaded) {
  605. this._videoAd.show();
  606. return;
  607. }
  608. }
  609. utils.showLog("暂无视频广告!");
  610. this._videoAd.load();
  611. if (this._videoCallback) {
  612. this._videoCallback(false, "暂无视频广告!");
  613. this._videoCallback = null;
  614. }
  615. }
  616. }
  617. _initVideoAd() {
  618. if (!this._videoAd) {
  619. if (utils.config.huaweiConfig.videoId) {
  620. utils.showLog("视频广告初始化:ID=" + utils.config.huaweiConfig.videoId);
  621. this._videoAd = this.huawei.createRewardedVideoAd({
  622. adUnitId: utils.config.huaweiConfig.videoId,
  623. success: (code) => {
  624. console.log("ad demo : loadAndShowVideoAd createRewardedVideoAd: success");
  625. },
  626. fail: (data, code) => {
  627. console.log("ad demo : loadAndShowVideoAd createRewardedVideoAd fail: " + data + "," + code);
  628. },
  629. complete: () => {
  630. console.log("ad demo : loadAndShowVideoAd createRewardedVideoAd complete");
  631. }
  632. });
  633. let self = this;
  634. if (this._videoAd) {
  635. utils.showLog("初始化注册视频回调!");
  636. this._videoAd.onLoad(function () {
  637. utils.showLog("激励视频加载成功");
  638. self._videoLoaded = true;
  639. })
  640. this._videoAd.onError((err) => {
  641. utils.showLog("激励视频出错: " + err.code + err.msg);
  642. self._videoLoaded = false;
  643. if (self._videoCallback) {
  644. self._videoCallback(false, "暂无视频广告!");
  645. self._videoCallback = null;
  646. }
  647. });
  648. this._videoAd.onClose((res) => {
  649. setTimeout(() => {
  650. self._videoAd.load();
  651. utils.showLog("延迟3秒重新加载视频广告");
  652. }, 3000);
  653. if (res.isEnded) {
  654. this.lastLastShowVideoTime = new Date().getTime();
  655. utils.showLog('激励视频广告完成,发放奖励');
  656. if (self._videoCallback) {
  657. self._videoCallback(true, "");
  658. self._videoCallback = null;
  659. }
  660. } else {
  661. utils.showLog('激励视频广告取消关闭,不发放奖励');
  662. if (self._videoCallback) {
  663. self._videoCallback(false, "观看完视频才能获得奖励!");
  664. self._videoCallback = null;
  665. }
  666. }
  667. });
  668. this._videoAd.load();
  669. }
  670. } else {
  671. utils.showLog("视频广告Id配置错误!");
  672. }
  673. }
  674. }
  675. /**
  676. * 验证插屏是否能展示
  677. * 1、次数限制 默认每日8次
  678. * 2、时间限制 默认60秒
  679. */
  680. private checkInsertAdShow(): boolean {
  681. let maxShowCount = this.ServerConfig.intersititial_max_show_count;
  682. let intervalTime = this.ServerConfig.intersititial_interval_time;
  683. let curTime: number = new Date().getTime();
  684. let interval: number = (curTime - this._insertLastShowTime) / 1000;
  685. utils.showLog("Huawei服务器插屏最大显示次数为:" + maxShowCount + ",间隔显示时间为:" + intervalTime + "秒!");
  686. // utils.showLog("Huawei插屏当前广告显示次数:" + utils.Tool_Huawei.insertAdShowCounts + "次,间隔时间:" + interval + "秒!");
  687. // if (maxShowCount > 0 && utils.HuaweiTool.insertAdShowCounts >= maxShowCount) {
  688. // utils.showLog("Huawei插屏广告显示的次数达到" + maxShowCount + "次。插屏不显示");
  689. // return false;
  690. // }
  691. if (intervalTime > 0 && interval < intervalTime) {
  692. utils.showLog("Huawei插屏广告显示的间隔少于" + intervalTime + "秒。插屏不显示");
  693. return false;
  694. }
  695. return true;
  696. }
  697. /**
  698. * 验证是否第一次创建插屏
  699. */
  700. miniInserAdIsCreate: boolean = false;
  701. /**
  702. * 创建小程序插屏广告
  703. * @param isUnique 是否唯一展示 不轮询显示原生
  704. */
  705. // _createMiniGameInsertAd(isUnique: boolean = false) {
  706. // if (PlatUtils.IsHuaWei) {
  707. // if (isUnique) {
  708. // if (!this.checkInsertAdShow()) return;
  709. // }
  710. // this._isInsertShow = true;
  711. // if (utils.config.huaweiConfig
  712. // && utils.config.huaweiConfig.insertId) {
  713. // //@ts-ignore
  714. // let _insertAd = qg.createInterstitialAd({
  715. // adUnitId: utils.config.huaweiConfig.insertId
  716. // });
  717. // utils.showLog("小游戏插屏ID:", utils.config.huaweiConfig.insertId);
  718. // if (_insertAd) {
  719. // utils.showLog("注册小游戏插屏广告回调!");
  720. // _insertAd.onError(((err) => {
  721. // utils.showLog("华为 小游戏插屏广告出错:" + JSON.stringify(err));
  722. // if (this._isInsertShow && !isUnique) {
  723. // if (this.ServerConfig.intersititial_style_level) {
  724. // if (this.ServerConfig.intersititial_style_mod[this.intersititialType - 1] == 2) {
  725. // utils.showLog("开始展示原生插屏!");
  726. // utils.delayCall(this.nativeInserAdDelayCall.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  727. // }
  728. // else if (this.ServerConfig.intersititial_style_mod[this.intersititialType - 1] == 1) {
  729. // utils.showLog("开始展示原生icon!");
  730. // this.showNativeTryGameWidget();
  731. // }
  732. // }
  733. // else if (this.ServerConfig.intersititial_first_ad == "default") {
  734. // this._isInsertShow = false;
  735. // utils.showLog("开始显示原生插屏广告!");
  736. // this._curPosIdIndexNativeInser = 0;
  737. // utils.delayCall(this.nativeInserAdDelayCall.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  738. // }
  739. // else {
  740. // utils.showLog("开始展示原生icon!");
  741. // this.showNativeTryGameWidget();
  742. // }
  743. // }
  744. // _insertAd.offError();
  745. // }).bind(this));
  746. // _insertAd.onLoad((() => {
  747. // utils.showLog("Huawei 小程序插屏广告加载成功!");
  748. // if (this._isInsertShow) {
  749. // this._isInsertShow = false;
  750. // _insertAd.show();
  751. // //onShow
  752. // this._isInsertShow = false;
  753. // // utils.Tool_Huawei.countInserShowCount();
  754. // this._insertLastShowTime = new Date().getTime();
  755. // let closeType = this.ServerConfig.intersititial_open_close_banner;
  756. // if (closeType && closeType > 0) {
  757. // utils.showLog("Huawei 配置当前插屏显示成功后" + (closeType == 1 ? "销毁" : "隐藏") + "banner!");
  758. // this.HideBanner();
  759. // }
  760. // }
  761. // _insertAd.offLoad();
  762. // }).bind(this));
  763. // // _insertAd.onClose((() => {
  764. // // utils.showLog("关闭插屏广告,展示banner")
  765. // // this.ShowBanner();
  766. // // }).bind(this));
  767. // _insertAd.load();
  768. // }
  769. // if (!_insertAd) {
  770. // utils.showLog("Huawei小游戏插屏广告创建失败!");
  771. // if (this.ServerConfig.intersititial_first_ad == "default" && !isUnique) {
  772. // utils.showLog("开始显示原生插屏广告!");
  773. // this._curPosIdIndexNativeInser = 0;
  774. // utils.delayCall(this.nativeInserAdDelayCall.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  775. // }
  776. // }
  777. // } else {
  778. // utils.showLog("Huawei 小游戏插屏广告配置信息错误!");
  779. // if (this.ServerConfig.intersititial_first_ad == "default" && !isUnique) {
  780. // utils.showLog("开始显示原生插屏广告!");
  781. // this._curPosIdIndexNativeInser = 0;
  782. // utils.delayCall(this.nativeInserAdDelayCall.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  783. // }
  784. // }
  785. // }
  786. // }
  787. _isInsertLoad: boolean = false;
  788. _canInsertShow: boolean = false;
  789. _initInsertAd() {
  790. if (!this._insertAd) {
  791. if (utils.config.huaweiConfig
  792. && utils.config.huaweiConfig.insertId) {
  793. //@ts-ignore
  794. this._insertAd = qg.createInterstitialAd({
  795. adUnitId: utils.config.huaweiConfig.insertId
  796. });
  797. utils.showLog("小游戏插屏ID:", utils.config.huaweiConfig.insertId);
  798. if (this._insertAd) {
  799. utils.showLog("注册小游戏插屏广告回调!");
  800. this._insertAd.onError(((err) => {
  801. utils.showLog("华为 小游戏插屏广告出错:" + JSON.stringify(err));
  802. if (this._canInsertShow) {
  803. if (this.ServerConfig.intersititial_style_level) {
  804. if (this.ServerConfig.intersititial_style_mod[this.intersititialType - 1] == 2) {
  805. utils.showLog("开始展示原生插屏!");
  806. utils.delayCall(this.nativeInserAdDelayCall.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  807. }
  808. else if (this.ServerConfig.intersititial_style_mod[this.intersititialType - 1] == 1) {
  809. utils.showLog("开始展示原生icon!");
  810. this.showNativeTryGameWidget();
  811. }
  812. }
  813. else if (this.ServerConfig.intersititial_first_ad == "default") {
  814. this._isInsertShow = false;
  815. utils.showLog("开始显示原生插屏广告!");
  816. this._curPosIdIndexNativeInser = 0;
  817. utils.delayCall(this.nativeInserAdDelayCall.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  818. }
  819. else {
  820. utils.showLog("开始展示原生icon!");
  821. this.showNativeTryGameWidget();
  822. }
  823. this._canInsertShow = false;
  824. }
  825. this._insertAd.destroy();
  826. this._insertAd = null;
  827. }).bind(this));
  828. this._insertAd.onLoad((() => {
  829. utils.showLog("Huawei 小程序插屏广告加载成功! #_canInsertShow=" + this._canInsertShow);
  830. this._isInsertLoad = true;
  831. // utils.Tool_Huawei.countInserShowCount();
  832. if (this._canInsertShow) {
  833. this._insertLastShowTime = new Date().getTime();
  834. let closeType = this.ServerConfig.intersititial_open_close_banner;
  835. if (closeType && closeType > 0) {
  836. utils.showLog("Huawei 配置当前插屏显示成功后" + (closeType == 1 ? "销毁" : "隐藏") + "banner!");
  837. this.HideBanner();
  838. }
  839. this._insertAd.show();
  840. }
  841. }).bind(this));
  842. this._insertAd.onClose((() => {
  843. utils.showLog("huawei 小程序插屏广告关闭")
  844. this._isInsertLoad = false;
  845. this._canInsertShow = false;
  846. this.ShowBanner();
  847. // setTimeout(() => {
  848. // this._insertAd.load();
  849. // }, 3000);
  850. }).bind(this));
  851. }
  852. if (!this._insertAd) {
  853. utils.showLog("Huawei小游戏插屏广告创建失败!");
  854. if (this._canInsertShow) {
  855. if (this.ServerConfig.intersititial_style_level) {
  856. if (this.ServerConfig.intersititial_style_mod[this.intersititialType - 1] == 2) {
  857. utils.showLog("开始展示原生插屏!");
  858. utils.delayCall(this.nativeInserAdDelayCall.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  859. }
  860. else if (this.ServerConfig.intersititial_style_mod[this.intersititialType - 1] == 1) {
  861. utils.showLog("开始展示原生icon!");
  862. this.showNativeTryGameWidget();
  863. }
  864. }
  865. else if (this.ServerConfig.intersititial_first_ad == "default") {
  866. this._isInsertShow = false;
  867. utils.showLog("开始显示原生插屏广告!");
  868. this._curPosIdIndexNativeInser = 0;
  869. utils.delayCall(this.nativeInserAdDelayCall.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  870. }
  871. else {
  872. utils.showLog("开始展示原生icon!");
  873. this.showNativeTryGameWidget();
  874. }
  875. this._canInsertShow = false;
  876. }
  877. }
  878. } else {
  879. utils.showLog("Huawei 小游戏插屏广告配置信息错误!");
  880. }
  881. }
  882. }
  883. _createMiniGameInsertAd() {
  884. if (PlatUtils.IsHuaWei) {
  885. utils.showLog("[展示小游戏插屏广告]");
  886. if (this.ServerConfig) {
  887. this._canInsertShow = true;
  888. if (!this._insertAd) {
  889. this._initInsertAd();
  890. }
  891. if (this._insertAd) {
  892. this._insertAd.load();
  893. return;
  894. }
  895. }
  896. }
  897. }
  898. _createMiniGameBannerAd(location: BannerLocation = null) {
  899. this._isBannerShow = true;
  900. if (!utils.config.huaweiConfig.bannerId) {
  901. utils.showLog("BannerID 未正确配置!");
  902. return;
  903. }
  904. if (!this._bannerAd) {
  905. //@ts-ignore
  906. let top = utils.Tool_Huawei.SysInfo.safeArea.height - 57;
  907. utils.showLog("banner =====#top=" + utils.Tool_Huawei.SysInfo.safeArea.height + " #top=" + top);
  908. this._bannerAd = this.huawei.createBannerAd({
  909. adUnitId: utils.config.huaweiConfig.bannerId,
  910. style: {
  911. top: top,
  912. left: 0,
  913. height: 57,
  914. width: 360
  915. }
  916. });
  917. utils.showLog("注册小游戏banner回调!" + this._bannerAd + " id=" + utils.config.huaweiConfig.bannerId);
  918. if (this._bannerAd) {
  919. this._bannerAd.onError((err) => {
  920. utils.showLog("huawei小游戏Banner,显示异常:" + JSON.stringify(err))
  921. if (this.ServerConfig.banner_style_level) {
  922. if (this.ServerConfig.banner_style_mod[this.bannerType - 1] == 2) {
  923. utils.showLog("开始展示默认banner");
  924. this._createNativeBannerAd(this._showNativeBanner);
  925. }
  926. }
  927. })
  928. this._bannerAd.onLoad(() => {
  929. utils.showLog("huawei小游戏Banner,加载成功")
  930. if (this._nativeBannerNode) {
  931. this._nativeBannerNode.active = false;
  932. }
  933. })
  934. // this._bannerAd.onShow((() => {
  935. // utils.showLog("huawei 小游戏Banner显示成功!");
  936. // }).bind(this));
  937. // this._bannerAd.onHide(function () {
  938. // utils.showLog("huawei 小游戏Banner 广告隐藏");
  939. // })
  940. }
  941. }
  942. if (this._bannerAd) {
  943. this._bannerAd.show();
  944. } else {
  945. utils.showLog("huawei 小游戏Banner广告创建失败!");
  946. if (this.ServerConfig.banner_style_level) {
  947. if (this.ServerConfig.banner_style_mod[this.bannerType - 1] == 2) {
  948. utils.showLog("开始展示默认banner");
  949. this._createNativeBannerAd(this._showNativeBanner);
  950. }
  951. }
  952. }
  953. }
  954. _curNativeBannerInfo: any = {};
  955. _createNativeBannerAd(completeCallback: Function) {
  956. if (PlatUtils.IsHuaWei) {
  957. let callback = completeCallback;
  958. utils.showLog("curPosIdIndexNativeBanner:" + this._curPosIdIndexNativeBanner + " #id=" + utils.config.huaweiConfig.nativeBannerIds[this._curPosIdIndexNativeBanner]);
  959. let nativeBannerAd = this._nativeBannerAd[this._curPosIdIndexNativeBanner];
  960. if (!nativeBannerAd) {
  961. if (utils.config.huaweiConfig.nativeBannerIds
  962. && utils.config.huaweiConfig.nativeBannerIds[this._curPosIdIndexNativeBanner]) {
  963. utils.showLog("创建原生广告Banner。 posId:" + utils.config.huaweiConfig.nativeBannerIds[this._curPosIdIndexNativeBanner]);
  964. //@ts-ignore
  965. // nativeBannerAd = qg.createNativeAd({
  966. // posId: utils.config.huaweiConfig.nativeBannerIds[this._curPosIdIndexNativeBanner]
  967. // });
  968. nativeBannerAd = this.huawei.createNativeAd({
  969. adUnitId: utils.config.huaweiConfig.nativeBannerIds[this._curPosIdIndexNativeBanner],
  970. success: (code) => {
  971. console.log("loadNativeAd loadNativeAd : success");
  972. },
  973. fail: (data, code) => {
  974. console.log("loadNativeAd loadNativeAd fail: " + data + "," + code);
  975. },
  976. complete: () => {
  977. console.log("loadNativeAd loadNativeAd : complete");
  978. }
  979. });
  980. if (nativeBannerAd) {
  981. this._nativeBannerAd.push(nativeBannerAd);
  982. nativeBannerAd.onLoad((res) => {
  983. if (this._nativeIsClose) {
  984. utils.showLog("隐藏了BANNER,不做任何处理!");
  985. return;
  986. }
  987. if (res && res.adList && res.adList.length > 0) {
  988. this._startBannerTimerTask = new Date().getTime();
  989. utils.showLog("原生Banner广告资源拉取成功!");
  990. utils.showLog(JSON.stringify(res));
  991. res = JSON.parse(JSON.stringify(res));
  992. let data = res.adList[0];
  993. //如果当前显示为结算banner,则不做数据验证
  994. if (this._checkNativeDataValid(data) || this.getNativeBannerInfo().show_st_banner == "true") {
  995. utils.showLog("当前原生Banner数据:" + data);
  996. if (this.canShowNativeBanner) {
  997. this._curPosIdIndexNativeBanner = 0;
  998. this._showNativeBanner(nativeBannerAd, data);
  999. this._curNativeBannerInfo.nativeBannerAd = nativeBannerAd;
  1000. this._curNativeBannerInfo.data = data;
  1001. } else {
  1002. cc.warn("已经隐藏banner不可重复展示")
  1003. }
  1004. return;
  1005. }
  1006. }
  1007. utils.showLog("原生Banner广告资源出错!");
  1008. this._curPosIdIndexNativeBanner++;
  1009. if (this._curPosIdIndexNativeBanner < utils.config.huaweiConfig.nativeBannerIds.length) {
  1010. this._createNativeBannerAd(callback);
  1011. } else {
  1012. this._curPosIdIndexNativeBanner = 0;
  1013. // 原生广告遍历完毕
  1014. utils.showLog("原生广告条遍历完毕,无法展示!");
  1015. let bannerInfo = this.getNativeBannerInfo();
  1016. if (bannerInfo.st_banner_show_back_up == -1) {
  1017. utils.showLog(">>>>>服务器配置不显示备用广告!");
  1018. return;
  1019. }
  1020. if (this.ServerConfig.banner_first_ad.indexOf("native") > -1) {
  1021. utils.showLog("开始展示小游戏广告条!")
  1022. this._createMiniGameBannerAd();
  1023. } else {
  1024. //显示自定义banner
  1025. if (utils.isShowRecommondGamesBanner()) {
  1026. clearTimeout(this._showBannerTimerId);
  1027. utils.showRecommendGamesBanner();
  1028. utils.showLog(`小游戏、原生广告条都无法展示,展示自定义banner`);
  1029. return;
  1030. }
  1031. }
  1032. }
  1033. });
  1034. nativeBannerAd.onError((err) => {
  1035. if (this._nativeIsClose) {
  1036. utils.showLog("隐藏了BANNER,不做任何处理!");
  1037. return;
  1038. }
  1039. utils.showLog("原生Banner广告资源拉取失败!" + JSON.stringify(err));
  1040. this._curPosIdIndexNativeBanner++;
  1041. if (this._curPosIdIndexNativeBanner < utils.config.huaweiConfig.nativeBannerIds.length) {
  1042. this._createNativeBannerAd(callback);
  1043. } else {
  1044. this._curPosIdIndexNativeBanner = 0;
  1045. // 原生广告遍历完毕
  1046. utils.showLog("原生广告条遍历完毕,无法展示!");
  1047. let bannerInfo = this.getNativeBannerInfo();
  1048. if (bannerInfo.st_banner_show_back_up == -1) {
  1049. utils.showLog(">>>>>服务器配置不显示备用广告!");
  1050. return;
  1051. }
  1052. if (this.ServerConfig.banner_style_level) {
  1053. if (this.ServerConfig.banner_style_mod[this.bannerType - 1] == 1) {
  1054. utils.showLog("开始展示默认banner");
  1055. this._createMiniGameBannerAd();
  1056. }
  1057. }
  1058. else if (this.ServerConfig.banner_first_ad.indexOf("native") > -1) {
  1059. utils.showLog("开始展示小游戏广告条!")
  1060. this._createMiniGameBannerAd();
  1061. } else {
  1062. //显示自定义banner
  1063. if (utils.isShowRecommondGamesBanner()) {
  1064. clearTimeout(this._showBannerTimerId);
  1065. utils.showRecommendGamesBanner();
  1066. utils.showLog(`小游戏、原生广告条都无法展示,展示自定义banner`);
  1067. return;
  1068. }
  1069. }
  1070. }
  1071. });
  1072. }
  1073. }
  1074. }
  1075. if (nativeBannerAd) {
  1076. utils.showLog("nativeBannerAd reLoad>>");
  1077. nativeBannerAd.load();
  1078. } else {
  1079. this._curPosIdIndexNativeBanner++;
  1080. if (utils.config.huaweiConfig.nativeBannerIds && this._curPosIdIndexNativeBanner < utils.config.huaweiConfig.nativeBannerIds.length) {
  1081. this._createNativeBannerAd(callback);
  1082. } else {
  1083. this._curPosIdIndexNativeBanner = 0;
  1084. // 原生广告遍历完毕
  1085. utils.showLog("原生广告条遍历完毕,无法展示!");
  1086. let bannerInfo = this.getNativeBannerInfo();
  1087. if (bannerInfo.st_banner_show_back_up == -1) {
  1088. utils.showLog(">>>>>服务器配置不显示备用广告!");
  1089. return;
  1090. }
  1091. if (this.ServerConfig.banner_style_level) {
  1092. if (this.ServerConfig.banner_style_mod[this.bannerType - 1] == 1) {
  1093. utils.showLog("开始展示默认banner");
  1094. this._createMiniGameBannerAd();
  1095. }
  1096. }
  1097. else if (this.ServerConfig.banner_first_ad.indexOf("native") > -1) {
  1098. utils.showLog("开始展示小游戏广告条!")
  1099. this._createMiniGameBannerAd();
  1100. } else {
  1101. //显示自定义banner
  1102. if (utils.isShowRecommondGamesBanner()) {
  1103. clearTimeout(this._showBannerTimerId);
  1104. utils.showRecommendGamesBanner();
  1105. utils.showLog(`小游戏、原生广告条都无法展示,展示自定义banner`);
  1106. return;
  1107. }
  1108. }
  1109. }
  1110. }
  1111. }
  1112. }
  1113. _createNativeInsertAd(completeCallback: Function) {
  1114. if (PlatUtils.IsHuaWei) {
  1115. let callback = completeCallback;
  1116. utils.showLog("curPosIdIndexNativeInsert:" + this._curPosIdIndexNativeInser);
  1117. let nativeInsertAd = this._nativeInsertAd[this._curPosIdIndexNativeInser];
  1118. if (!nativeInsertAd) {
  1119. if (utils.config.huaweiConfig.nativeInsertIds
  1120. && utils.config.huaweiConfig.nativeInsertIds[this._curPosIdIndexNativeInser]) {
  1121. utils.showLog("创建原生插屏广告。 posId:" + utils.config.huaweiConfig.nativeInsertIds[this._curPosIdIndexNativeInser]);
  1122. //@ts-ignore
  1123. nativeInsertAd = qg.createNativeAd({
  1124. adUnitId: utils.config.huaweiConfig.nativeInsertIds[this._curPosIdIndexNativeInser]
  1125. });
  1126. if (nativeInsertAd) {
  1127. this._nativeInsertAd.push(nativeInsertAd);
  1128. nativeInsertAd.onLoad((res) => {
  1129. if (res && res.adList && res.adList.length > 0) {
  1130. utils.showLog("原生插屏广告资源拉取成功!");
  1131. utils.showLog(JSON.stringify(res));
  1132. res = JSON.parse(JSON.stringify(res));
  1133. let data = res.adList[0];
  1134. if (this._checkNativeInsertDataValid(data)) {
  1135. this._curPosIdIndexNativeInser = 0;
  1136. this._showNativeInsert(nativeInsertAd, data);
  1137. return;
  1138. } else {
  1139. utils.showLog("原生插屏广告资源不合法!");
  1140. }
  1141. }
  1142. utils.showLog("原生插屏广告资源出错!");
  1143. this._curPosIdIndexNativeInser++;
  1144. if (this._curPosIdIndexNativeInser < utils.config.huaweiConfig.nativeInsertIds.length) {
  1145. this._createNativeInsertAd(callback);
  1146. } else {
  1147. this._curPosIdIndexNativeInser = 0;
  1148. // 原生广告遍历完毕
  1149. utils.showLog("原生插屏广告遍历完毕,无法展示!");
  1150. if (this.ServerConfig.intersititial_first_ad == "native") {
  1151. utils.showLog("开始展示小游戏插屏广告!");
  1152. this._createMiniGameInsertAd();
  1153. }
  1154. }
  1155. });
  1156. nativeInsertAd.onError((err) => {
  1157. utils.showLog("原生插屏广告资源拉取失败!" + err.code + err.msg);
  1158. this._curPosIdIndexNativeInser++;
  1159. if (this._curPosIdIndexNativeInser < utils.config.huaweiConfig.nativeInsertIds.length) {
  1160. this._createNativeInsertAd(callback);
  1161. } else {
  1162. this._curPosIdIndexNativeInser = 0;
  1163. // 原生广告遍历完毕
  1164. utils.showLog("原生插屏广告遍历完毕,无法展示!");
  1165. if (this.ServerConfig.intersititial_style_level) {
  1166. if (this.ServerConfig.intersititial_style_mod[this.intersititialType - 1] != 2) {
  1167. utils.showLog("开始展示默认插屏!");
  1168. this._createMiniGameInsertAd();
  1169. }
  1170. else {
  1171. this.showNativeTryGameWidget();
  1172. }
  1173. }
  1174. else if (this.ServerConfig.intersititial_first_ad == "native") {
  1175. utils.showLog("开始展示小游戏插屏广告!");
  1176. this._createMiniGameInsertAd();
  1177. }
  1178. else {
  1179. this.showNativeTryGameWidget();
  1180. }
  1181. }
  1182. });
  1183. }
  1184. }
  1185. }
  1186. if (nativeInsertAd) {
  1187. nativeInsertAd.load();
  1188. } else {
  1189. this._curPosIdIndexNativeInser++;
  1190. if (utils.config.huaweiConfig.nativeInsertIds && this._curPosIdIndexNativeInser < utils.config.huaweiConfig.nativeInsertIds.length) {
  1191. this._createNativeInsertAd(callback);
  1192. } else {
  1193. this._curPosIdIndexNativeInser = 0;
  1194. // 原生广告遍历完毕
  1195. utils.showLog("原生插屏广告遍历完毕,无法展示!");
  1196. if (this.ServerConfig.intersititial_first_ad == "native") {
  1197. utils.showLog("开始展示小游戏插屏广告!");
  1198. this._createMiniGameInsertAd();
  1199. }
  1200. }
  1201. }
  1202. }
  1203. }
  1204. /**
  1205. * 获取原生广告数据
  1206. * @param args
  1207. */
  1208. getNativeAdData() {
  1209. if (!this._nativeAdObject) {
  1210. this._nativeAdObject = new YZ_NativeAdObject();
  1211. }
  1212. this._nativeAdObject._nativeObj = this._nativeAd;
  1213. this._nativeAdObject.data = this._nativeData;
  1214. // utils.showLog("获取原生数据 >>>", this._nativeData)
  1215. // setTimeout(() => {
  1216. // utils.showLog("延迟两秒重新请求原生广告数据");
  1217. // this._createNativeAd(args);
  1218. // }, 2000);
  1219. if (this._nativeData && this._nativeAd) {
  1220. this._nativeData = null;
  1221. return this._nativeAdObject;
  1222. }
  1223. return null;
  1224. }
  1225. // /**
  1226. // * 创建单个原生广告
  1227. // * @param completeCallback
  1228. // */
  1229. // createNativeAd(nativeItem?: YZ_NativeItem) {
  1230. // utils.showLog("_createNativeAd >>>>>");
  1231. // if (PlatUtils.IsHuaWei) {
  1232. // if (nativeItem) {
  1233. // this._curNativeItem = nativeItem;
  1234. // }
  1235. // utils.showLog("_curPosIdIndexSingleNative:" + this._curPosIdIndexSingleNative + " #this._nativeSingleAd.length" + this._nativeSingleAd.length);
  1236. // let nativeSingleAd = this._nativeSingleAd[this._curPosIdIndexSingleNative];
  1237. // if (!nativeSingleAd) {
  1238. // if (utils.config.huaweiConfig.nativeSingleAdIds
  1239. // && utils.config.huaweiConfig.nativeSingleAdIds[this._curPosIdIndexSingleNative]) {
  1240. // utils.showLog("创建原生广告。 posId:" + utils.config.huaweiConfig.nativeSingleAdIds[this._curPosIdIndexSingleNative]);
  1241. // //@ts-ignore
  1242. // nativeSingleAd = qg.createNativeAd({
  1243. // posId: utils.config.huaweiConfig.nativeSingleAdIds[this._curPosIdIndexSingleNative]
  1244. // });
  1245. // if (nativeSingleAd) {
  1246. // this._nativeSingleAd.push(nativeSingleAd);
  1247. // nativeSingleAd.onLoad((res) => {
  1248. // if (res && res.adList && res.adList.length > 0) {
  1249. // utils.showLog("原生广告资源拉取成功!");
  1250. // utils.showLog(JSON.stringify(res));
  1251. // res = JSON.parse(JSON.stringify(res));
  1252. // let data = res.adList[0];
  1253. // if (this._checkNativeDataValid(data)) {
  1254. // this._curPosIdIndexSingleNative = 0;
  1255. // this._nativeData = data;
  1256. // this._nativeAd = nativeSingleAd;
  1257. // // utils.showLog("callback >>>", callback);
  1258. // // callback(this.getNativeAdData());
  1259. // this._showNativeAd();
  1260. // return;
  1261. // } else {
  1262. // utils.showLog("原生广告资源不合法!");
  1263. // }
  1264. // }
  1265. // utils.showLog("原生广告资源出错!");
  1266. // this._curPosIdIndexSingleNative++;
  1267. // if (this._curPosIdIndexSingleNative < utils.config.huaweiConfig.nativeSingleAdIds.length) {
  1268. // this.createNativeAd();
  1269. // } else {
  1270. // this._curPosIdIndexSingleNative = 0;
  1271. // // 原生广告遍历完毕
  1272. // utils.showLog("单个原生广告ID遍历完毕,无法展示!");
  1273. // }
  1274. // });
  1275. // nativeSingleAd.onError((err) => {
  1276. // utils.showLog("原生单个广告资源拉取失败!" + err.code + err.msg);
  1277. // this._curPosIdIndexSingleNative++;
  1278. // if (this._curPosIdIndexSingleNative < utils.config.huaweiConfig.nativeSingleAdIds.length) {
  1279. // this.createNativeAd();
  1280. // } else {
  1281. // this._curPosIdIndexSingleNative = 0;
  1282. // // 原生广告遍历完毕
  1283. // utils.showLog("单个原生广告ID遍历完毕,无法展示!");
  1284. // }
  1285. // });
  1286. // }
  1287. // }
  1288. // }
  1289. // if (nativeSingleAd) {
  1290. // utils.showLog("nativeSingleAd reload");
  1291. // nativeSingleAd.load();
  1292. // } else {
  1293. // this._curPosIdIndexSingleNative++;
  1294. // if (utils.config.huaweiConfig.nativeSingleAdIds && this._curPosIdIndexSingleNative < utils.config.huaweiConfig.nativeSingleAdIds.length) {
  1295. // this.createNativeAd();
  1296. // } else {
  1297. // this._curPosIdIndexSingleNative = 0;
  1298. // // 原生广告遍历完毕
  1299. // utils.showLog("单个原生广告ID 遍历完毕,无法展示!");
  1300. // }
  1301. // }
  1302. // }
  1303. // }
  1304. /**
  1305. * 显示原生banner组件
  1306. */
  1307. _showNativeBanner(nativeBannerAd: any, data: any) {
  1308. if (PlatUtils.IsHuaWei) {
  1309. if (data) {
  1310. utils.showLog("显示原生banner");
  1311. if (this._bannerAd) {
  1312. this._bannerAd.hide();
  1313. }
  1314. if (!cc.isValid(this._nativeBannerNode) || (!this._nativeBannerNode && utils.config.otherconfig.nativeBanner)) {
  1315. this._nativeBannerNode = cc.instantiate(utils.config.otherconfig.nativeBanner);
  1316. this._nativeBannerNode.position = CompatibleTool.position(cc.winSize.width / 2, this._nativeBannerNode.height * this._nativeBannerNode.scaleY / 2);
  1317. cc.director.getScene().addChild(this._nativeBannerNode, 1000);
  1318. }
  1319. if (this._nativeBannerNode) {
  1320. this._nativeBannerNode.active = true;
  1321. let nativeBanner: YZ_NativeBanner = this._nativeBannerNode.getComponent("YZ_NativeBanner");
  1322. if (nativeBanner) {
  1323. nativeBanner.init(nativeBannerAd, data, this.getNativeBannerInfo());
  1324. } else {
  1325. utils.showLog("NativeBanner组件不存在!");
  1326. }
  1327. this._showBannerCallBack && this._showBannerCallBack();
  1328. this._showBannerCallBack = null;
  1329. } else {
  1330. utils.showLog("原生广告banner位没有创建!");
  1331. }
  1332. }
  1333. }
  1334. }
  1335. /**
  1336. * 显示原生插屏组件
  1337. */
  1338. _showNativeInsert(nativeInsertAd: any, data: any) {
  1339. if (PlatUtils.IsHuaWei) {
  1340. if (data) {
  1341. if ((!cc.isValid(this._nativeInsertNode)) || !this._nativeInsertNode && utils.config.otherconfig.nativeInsert) {
  1342. utils.showLog("创建原生插屏广告位");
  1343. this._nativeInsertNode = cc.instantiate(utils.config.otherconfig.nativeInsert);
  1344. this._nativeInsertNode.position = CompatibleTool.position(cc.winSize.width / 2, cc.winSize.height / 2);
  1345. cc.director.getScene().addChild(this._nativeInsertNode, 999999);
  1346. }
  1347. if (this._nativeInsertNode) {
  1348. let nativeInsert = this._nativeInsertNode.getComponent("YZ_NativeInsert");
  1349. if (nativeInsert) {
  1350. this._nativeInsertAdShowCount = 0;
  1351. nativeInsert.init(nativeInsertAd, data);
  1352. this._insertLastShowTime = new Date().getTime();
  1353. // utils.HuaweiTool.countInserShowCount();
  1354. // utils.showLog("原生插屏显示成功,当前显示次数=" + utils.HuaweiTool.insertAdShowCounts);
  1355. let closeType = this.ServerConfig.intersititial_open_close_banner;
  1356. if (closeType && closeType > 0) {
  1357. utils.showLog("Huawei 配置当前插屏显示成功后" + (closeType == 1 ? "销毁" : "隐藏") + "banner!");
  1358. this.HideBanner();
  1359. }
  1360. } else {
  1361. utils.showLog("NativeInsert组件不存在!");
  1362. }
  1363. } else {
  1364. utils.showLog("原生广告插屏位没有创建!");
  1365. }
  1366. }
  1367. }
  1368. }
  1369. public ShowCloseBtnBanner(location: BannerLocation = BannerLocation.Home, args: any) {
  1370. utils.showLog("ShowCloseBtnBanner >>>>>>>>>.");
  1371. let isMoveBtn = 0;
  1372. let fadeInTime = 0;
  1373. let btn: cc.Node = args.closeBtn;
  1374. let winHeight = cc.winSize.height;
  1375. btn.opacity = 0;
  1376. if (this.ServerConfig) {
  1377. if (this.ServerConfig.is_move_btn) {
  1378. isMoveBtn = this.ServerConfig.is_move_btn;
  1379. }
  1380. if (this.ServerConfig.close_btn_fade_in_time) {
  1381. fadeInTime = this.ServerConfig.close_btn_fade_in_time;
  1382. }
  1383. utils.showLog(isMoveBtn == 0 ? "显示banner,且按钮在上面" : `显示Banner,按钮居底部且${isMoveBtn}毫秒后移动`);
  1384. setTimeout(() => {
  1385. utils.showLog("延迟调用关闭按钮的Banner >>>>");
  1386. this.ShowBanner(location, args);
  1387. var adY = 240;
  1388. utils.showLog('utils - adY:' + adY);
  1389. if (adY > 0 && btn) {
  1390. btn.y = -(winHeight / 2 - adY) + btn.height;
  1391. utils.showLog("btnClose.y" + btn.y);
  1392. }
  1393. }, isMoveBtn);
  1394. setTimeout(() => {
  1395. btn.runAction(cc.fadeIn(0.3));
  1396. }, fadeInTime * 1000);
  1397. }
  1398. }
  1399. _checkNativeInsertDataValid(data: any) {
  1400. if (!data) {
  1401. return false;
  1402. }
  1403. return data.title && ((data.iconUrlList && data.iconUrlList.length > 0) || (data.imgUrlList && data.imgUrlList.length > 0));
  1404. }
  1405. _checkNativeDataValid(data: any) {
  1406. utils.showLog("checkNativeDataValid");
  1407. if (!data) {
  1408. return false;
  1409. }
  1410. if (data.imgUrlList && data.imgUrlList.length > 0) {
  1411. return true;
  1412. }
  1413. if (data.title && data.desc && data.clickBtnTxt && ((data.iconUrlList && data.iconUrlList.length > 0))) {
  1414. return true;
  1415. }
  1416. return false;
  1417. }
  1418. /**
  1419. * 验证是否显示激励插屏
  1420. */
  1421. private checkRewardInsertIsShow() {
  1422. // let jumpList = utils.getRecommondGameList();
  1423. // if (utils.isSupportnavigateToMiniGame()) {
  1424. // if (utils.HuaweiTool
  1425. // && utils.HuaweiTool.ServerConfig
  1426. // && utils.HuaweiTool.ServerConfig.is_reward_intersititia) {
  1427. // if (utils.HuaweiTool.ServerConfig.is_reward_intersititia == "true"
  1428. // && jumpList && jumpList.length > 0) {
  1429. // utils.showLog("激励插屏显示环境验证通过!");
  1430. // return true;
  1431. // } else {
  1432. // utils.showLog("is_reward_intersititia 参数为false,激励插屏组件不显示!");
  1433. // return false;
  1434. // }
  1435. // } else {
  1436. // utils.showLog("配置中没有is_reward_intersititia参数,激励插屏组件组件不显示!");
  1437. // return false;
  1438. // }
  1439. // }
  1440. // utils.showLog("当前平台不支持小程序跳转!");
  1441. // return false;
  1442. }
  1443. _rewardInsertNode: cc.Node = null;
  1444. /**
  1445. * 显示激励插屏组件
  1446. */
  1447. public showRewardInsert() {
  1448. utils.showLog("show reward");
  1449. // let self = this;
  1450. // if (!this.checkRewardInsertIsShow()) {
  1451. // self._videoCallback && self._videoCallback(false, "暂无视频广告!");
  1452. // utils.adManager.videoCallBack = null;
  1453. // return;
  1454. // }
  1455. // if (((!cc.isValid(this._rewardInsertNode)) || !this._rewardInsertNode) && utils.config.otherconfig.rewardInsert) {
  1456. // utils.showLog("创建激励插屏广告");
  1457. // this._rewardInsertNode = cc.instantiate(utils.config.otherconfig.rewardInsert);
  1458. // this._rewardInsertNode.position = CompatibleTool.position(cc.winSize.width / 2, cc.winSize.height / 2);
  1459. // cc.director.getScene().addChild(this._rewardInsertNode, 9999);
  1460. // }
  1461. // if (this._rewardInsertNode) {
  1462. // let rewardInsert: RewardInsert = this._rewardInsertNode.getComponent("RewardInsert");
  1463. // if (rewardInsert) {
  1464. // rewardInsert.isShow = false;
  1465. // utils.showLog("显示激励插屏组件!");
  1466. // } else {
  1467. // utils.showLog("RewardInsert组件不存在!");
  1468. // }
  1469. // } else {
  1470. // utils.showLog("激励插屏没有创建!");
  1471. // }
  1472. }
  1473. public hideRewardInsert() {
  1474. if (this._rewardInsertNode) {
  1475. let rewardInsert: RewardInsert = this._rewardInsertNode.getComponent("RewardInsert");
  1476. if (rewardInsert) {
  1477. rewardInsert.hide();
  1478. utils.showLog("隐藏激励插屏组件!");
  1479. } else {
  1480. utils.showLog("RewardInsert组件不存在!");
  1481. }
  1482. } else {
  1483. utils.showLog("激励插屏没有创建!");
  1484. }
  1485. }
  1486. // public ShowStatementRecomment(): cc.Node {
  1487. // if (PlatUtils.IsHuaWei) {
  1488. // utils.showLog("curPosIdIndexNativeInsert:", this._curPosIdIndexNativeInser);
  1489. // let nativeInsertAd = this._nativeInsertAd[this._curPosIdIndexNativeInser];
  1490. // if (!nativeInsertAd) {
  1491. // if (utils.config.huaweiConfig.nativeInsertIds
  1492. // && utils.config.huaweiConfig.nativeInsertIds[this._curPosIdIndexNativeInser]) {
  1493. // utils.showLog("创建原生结算页面广告。 posId:", utils.config.huaweiConfig.nativeInsertIds[this._curPosIdIndexNativeInser]);
  1494. // nativeInsertAd = qg.createNativeAd({
  1495. // posId: utils.config.huaweiConfig.nativeInsertIds[this._curPosIdIndexNativeInser]
  1496. // });
  1497. // if (nativeInsertAd) {
  1498. // this._nativeInsertAd.push(nativeInsertAd);
  1499. // nativeInsertAd.onLoad((res) => {
  1500. // if (res && res.adList && res.adList.length > 0) {
  1501. // utils.showLog("原生原生结算页面资源拉取成功!");
  1502. // utils.showLog(JSON.stringify(res));
  1503. // res = JSON.parse(JSON.stringify(res));
  1504. // let data = res.adList[0];
  1505. // if (this._checkNativeDataValid(data)) {
  1506. // this._curPosIdIndexNativeInser = 0;
  1507. // // this._showNativeInsert(nativeInsertAd, data);
  1508. // // return this.cre;
  1509. // } else {
  1510. // utils.showLog("原生结算页面资源不合法!");
  1511. // }
  1512. // }
  1513. // utils.showLog("原生结算页面资源出错!");
  1514. // this._curPosIdIndexNativeInser++;
  1515. // if (this._curPosIdIndexNativeInser < utils.config.huaweiConfig.nativeInsertIds.length) {
  1516. // this.ShowStatementRecomment();
  1517. // } else {
  1518. // this._curPosIdIndexNativeInser = 0;
  1519. // // 原生广告遍历完毕
  1520. // utils.showLog("原生结算页面遍历完毕,无法展示!");
  1521. // }
  1522. // });
  1523. // nativeInsertAd.onError((err) => {
  1524. // utils.showLog("原生结算页面资源拉取失败!", err.code, err.msg);
  1525. // this._curPosIdIndexNativeInser++;
  1526. // if (this._curPosIdIndexNativeInser < utils.config.huaweiConfig.nativeInsertIds.length) {
  1527. // this.ShowStatementRecomment();
  1528. // } else {
  1529. // this._curPosIdIndexNativeInser = 0;
  1530. // // 原生广告遍历完毕
  1531. // utils.showLog("原生结算页面遍历完毕,无法展示!");
  1532. // }
  1533. // });
  1534. // }
  1535. // }
  1536. // }
  1537. // if (nativeInsertAd) {
  1538. // nativeInsertAd.load();
  1539. // } else {
  1540. // this._curPosIdIndexNativeInser++;
  1541. // if (utils.config.huaweiConfig.nativeInsertIds && this._curPosIdIndexNativeInser < utils.config.huaweiConfig.nativeInsertIds.length) {
  1542. // this.ShowStatementRecomment();
  1543. // } else {
  1544. // this._curPosIdIndexNativeInser = 0;
  1545. // // 原生广告遍历完毕
  1546. // utils.showLog("原生插屏广告遍历完毕,无法展示!");
  1547. // }
  1548. // }
  1549. // }
  1550. // return null;
  1551. // }
  1552. private checkIsShowStatementAd() {
  1553. if (utils.isSupportnavigateToMiniGame() && this.ServerConfig) {
  1554. return true;
  1555. }
  1556. utils.showLog("当前平台不支持游戏内跳转,6元素交叉推广组件不显示!");
  1557. return false;
  1558. }
  1559. /**
  1560. * 创建结算页面推广组件
  1561. */
  1562. public ShowStatementRecomment(showNativeAd: boolean = true): cc.Node {
  1563. if (this.checkIsShowStatementAd()) {
  1564. if (!showNativeAd) {
  1565. if (utils.config.otherconfig.crossWidget6) {
  1566. let node = cc.instantiate(utils.config.otherconfig.crossWidget6);
  1567. utils.showLog("只显示结算互推广告");
  1568. return node;
  1569. } else {
  1570. utils.showLog("未找到预制体 crossWidget6, 请查看CommonUtils组件上是否赋值!");
  1571. return null;
  1572. }
  1573. } else {
  1574. if (utils.config.otherconfig.statementRecomment) {
  1575. let node = cc.instantiate(utils.config.otherconfig.statementRecomment);
  1576. let statementRecomment: YZ_StatementRecommentAd = node.getComponent("YZ_StatementRecommentAd");
  1577. statementRecomment.showNativeAd = showNativeAd;
  1578. utils.showLog("显示结算互推和原生广告");
  1579. return node;
  1580. } else {
  1581. utils.showLog("未找到预制体 StatementRecomment, 请查看CommonUtils组件上是否赋值!");
  1582. return null;
  1583. }
  1584. }
  1585. } else {
  1586. return null;
  1587. }
  1588. }
  1589. _showNativeAd() {
  1590. if (this._curNativeItem) {
  1591. this._curNativeItem.init(this.getNativeAdData());
  1592. }
  1593. // let nativeItem: YZ_NativeItem = node.getComponent("YZ_NativeItem");
  1594. // utils.showLog("ShowSingleNativeAd <<<<<<<<<", nativeItem, "===", res)
  1595. // if (nativeItem)
  1596. }
  1597. /**
  1598. * 创建结算页面推广组件
  1599. */
  1600. public ShowSingleNativeAd(): cc.Node {
  1601. if (utils.config.otherconfig.singleNativeAd) {
  1602. // if (this.getNativeAdData()) {
  1603. let node = cc.instantiate(utils.config.otherconfig.singleNativeAd);
  1604. let nativeItem: YZ_NativeItem = node.getComponent("YZ_NativeItem");
  1605. nativeItem.showType = 2;
  1606. this._curNativeItem = nativeItem;
  1607. this.createNativeAd();
  1608. // this.createNativeAd((res) => {
  1609. // utils.showLog("ShowSingleNativeAd <<<<<<<<<", nativeItem, "===", res)
  1610. // if (nativeItem) nativeItem.init(res, 2);
  1611. // })
  1612. // if (this._nativeAdObject._nativeAdData) {
  1613. // statementRecomment.nativeData = this._nativeAdObject._nativeAdData;
  1614. // } else {
  1615. // utils.showLog("结算推广组件 >> 原生广告数据不存在!");
  1616. // return null;
  1617. // }
  1618. utils.showLog("单个原生广告创建成功!");
  1619. // this._createNativeAd();
  1620. return node;
  1621. // } else {
  1622. // utils.showLog("单个原生广告创建失败,原生广告数据不存在");
  1623. // return null;
  1624. // }
  1625. } else {
  1626. utils.showLog("未找到预制体 singleNativeAd, 请查看CommonUtils组件上是否赋值!");
  1627. return null;
  1628. }
  1629. }
  1630. /**
  1631. * 显示结算广告
  1632. * @param data 参数: closeBtn:
  1633. */
  1634. showStatementAds(data?: any): any {
  1635. // let result: any = { "type": 0, "node": null };
  1636. // if (this.ServerConfig && this.ServerConfig.statement_type) {
  1637. // let type = this.ServerConfig.statement_type;
  1638. // let spareType = this.ServerConfig.st_spare_type;
  1639. // let isSycn = this.ServerConfig.st_sync;
  1640. // let node: cc.Node = null;
  1641. // let resType: number = 0;
  1642. // switch (type) {
  1643. // case 1:
  1644. // utils.showLog("结算广告 >> 只显示小游戏插屏广告");
  1645. // if (!this.checkInsertAdShow()) {
  1646. // if (spareType && spareType > 0) {
  1647. // switch (spareType) {
  1648. // case 3:
  1649. // utils.showLog("结算广告 >> 小游戏插屏达到限制的次数 >> 显示备用组件 6个互推");
  1650. // node = this.ShowStatementRecomment(false);
  1651. // resType = 1;
  1652. // break;
  1653. // case 4:
  1654. // utils.showLog("结算广告 >> 小游戏插屏达到限制的次数 >> 显示备用组件 单个原生广告");
  1655. // node = this.ShowSingleNativeAd();
  1656. // resType = 2;
  1657. // break;
  1658. // case 5:
  1659. // utils.showLog("结算广告 >> 小游戏插屏达到限制的次数 >> 显示备用组件 3个互推+单个原生广告");
  1660. // node = this.ShowStatementRecomment();
  1661. // resType = 1;
  1662. // break;
  1663. // }
  1664. // }
  1665. // } else {
  1666. // this._createMiniGameInsertAd(true);
  1667. // }
  1668. // break;
  1669. // case 2:
  1670. // utils.showLog("结算广告 >> 显示插屏广告且判断优先级");
  1671. // if (!this.checkInsertAdShow()) {
  1672. // if (spareType) {
  1673. // switch (spareType) {
  1674. // case 3:
  1675. // utils.showLog("结算广告 >> 小游戏插屏达到限制的次数 >> 显示备用组件 6个互推");
  1676. // node = this.ShowStatementRecomment(false);
  1677. // resType = 1;
  1678. // break;
  1679. // case 4:
  1680. // utils.showLog("结算广告 >> 小游戏插屏达到限制的次数 >> 显示备用组件 单个原生广告");
  1681. // node = this.ShowSingleNativeAd();
  1682. // resType = 2;
  1683. // break;
  1684. // case 5:
  1685. // utils.showLog("结算广告 >> 小游戏插屏达到限制的次数 >> 显示备用组件 3个互推+单个原生广告");
  1686. // node = this.ShowStatementRecomment();
  1687. // resType = 1;
  1688. // break;
  1689. // }
  1690. // }
  1691. // } else {
  1692. // this.ShowInterstitial();
  1693. // }
  1694. // break;
  1695. // case 3:
  1696. // utils.showLog("结算广告 >> 显示插屏广告+6个互推");
  1697. // this.ShowInterstitial();
  1698. // node = this.ShowStatementRecomment(false);
  1699. // resType = 1;
  1700. // break;
  1701. // case 4:
  1702. // if (isSycn && isSycn == "true") {
  1703. // utils.showLog("结算广告 >> 显示插屏广告+单个原生广告");
  1704. // this.ShowInterstitial();
  1705. // node = this.ShowSingleNativeAd();
  1706. // } else {
  1707. // node = this.ShowSingleNativeAd();
  1708. // utils.showLog("结算广告 >> 只显示单个原生广告");
  1709. // }
  1710. // resType = 2;
  1711. // break;
  1712. // case 5:
  1713. // if (isSycn && isSycn == "true") {
  1714. // utils.showLog("结算广告 >> 显示插屏广告+ 3个互推+单个原生广告");
  1715. // this.ShowInterstitial();
  1716. // node = this.ShowStatementRecomment();
  1717. // } else {
  1718. // utils.showLog("结算广告 >> 只显示 3个互推+单个原生广告");
  1719. // node = this.ShowStatementRecomment();
  1720. // }
  1721. // resType = 1;
  1722. // break;
  1723. // case 6:
  1724. // if (isSycn && isSycn == "true") {
  1725. // utils.showLog("结算广告 >> 显示滚动互推+原生广告");
  1726. // node = this.ShowStatementRecomment(true);
  1727. // } else {
  1728. // utils.showLog("结算广告 >> 只显示滚动互推");
  1729. // node = this.ShowStatementRecomment();
  1730. // }
  1731. // resType = 1;
  1732. // break;
  1733. // default:
  1734. // utils.showLog("非法的结算广告类型,:" + type)
  1735. // break;
  1736. // }
  1737. // result.type = resType;
  1738. // result.node = node;
  1739. // return result;
  1740. // } else {
  1741. // utils.showLog("配置未初始化、或者没有配置结算广告!");
  1742. // return result;
  1743. // }
  1744. }
  1745. public showFullScreenVideo() {
  1746. }
  1747. public GameExit() { }
  1748. public Share() { }
  1749. }