AdAgentWechat.ts 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. import AdAgent from "./AdAgent";
  2. import { AttributedKey, AttributedType, AttributedValue, BannerLocation, SubLocation } from "./YZ_Constant";
  3. import PlatUtils from "./PlatUtils";
  4. import { utils } from "./Utils";
  5. import RewardInsert from "./RewardInsert";
  6. import CompatibleTool from "./CompatibleTool";
  7. import BeforGameOverRecGamesPanel from "./BeforGameOverRecGamesPanel";
  8. import { CustomAdInfo } from "./CommonConfig";
  9. const { ccclass, property } = cc._decorator;
  10. @ccclass
  11. export default class AdAgentWechat extends AdAgent {
  12. _curBannerAd: any = null;
  13. _videoAd: any = null;
  14. _videoCallback: Function = null;
  15. _insertAd: any = null;
  16. _rewardInsertNode: cc.Node = null;
  17. public get ServerConfig() {
  18. return utils.wechatTool.ServerConfig;
  19. }
  20. _sysInfo: any = null;
  21. public get sysInfo() {
  22. if (this._sysInfo) return this._sysInfo;
  23. this._sysInfo = utils.wechatTool.getSystemInfo();
  24. return this._sysInfo;
  25. }
  26. public Init() {
  27. if (PlatUtils.IsWechat) {
  28. utils.registerServerInitEvent(() => {
  29. this._initVideoAd();
  30. this._initInsertAd();
  31. if (utils.getConfigByKey("game_time_report")) {
  32. utils.scheduleOnce(() => {
  33. utils.wechatTool.reportAttributedEvent(AttributedType.GameAddiction, AttributedKey.GameAddiction, AttributedValue.GameTimeAction);
  34. }, utils.getConfigByKey("game_time_report"));
  35. }
  36. }, this);
  37. }
  38. }
  39. _initVideoAd() {
  40. if (PlatUtils.IsWechat) {
  41. if (!utils.wechatTool.isOverMinVersion("2.0.4")) {
  42. utils.showLog("当前版本不支持视频广告!");
  43. return;
  44. }
  45. if (!this._videoAd) {
  46. utils.showLog("初始化视频!");
  47. if (!utils.config.wechatconfig.videoId) {
  48. utils.showLog("视频ID配置错误!");
  49. return;
  50. }
  51. utils.showLog("视频广告ID:", utils.config.wechatconfig.videoId.trim());
  52. //@ts-ignore
  53. this._videoAd = wx.createRewardedVideoAd({
  54. adUnitId: utils.config.wechatconfig.videoId
  55. });
  56. let self = this;
  57. if (this._videoAd) {
  58. utils.showLog("初始化注册视频回调!");
  59. this._videoAd.onLoad(function () {
  60. utils.showLog("激励视频加载成功");
  61. // this._isVideoLoaded = true;
  62. }.bind(this));
  63. this._videoAd.onError(function (err) {
  64. utils.showLog("激励视频加载失败!", err.code, err.msg);
  65. // this._isVideoLoaded = false;
  66. }.bind(this));
  67. this._videoAd.onClose(function (res) {
  68. // this._isVideoLoaded = false;
  69. if (res && res.isEnded || res === undefined) {
  70. // 正常播放结束,可以下发游戏奖励
  71. utils.showLog('激励视频广告完成,发放奖励');
  72. if (self._videoCallback) {
  73. self._videoCallback(true, "");
  74. self._videoCallback = null;
  75. }
  76. } else {
  77. // 播放中途退出,不下发游戏奖励
  78. utils.showLog('激励视频广告取消关闭,不发放奖励');
  79. if (self._videoCallback) {
  80. self._videoCallback(false, "观看完视频才能获得奖励!");
  81. self._videoCallback = null;
  82. }
  83. }
  84. }.bind(this));
  85. } else {
  86. utils.showLog("激励视频初始化失败!");
  87. }
  88. }
  89. }
  90. }
  91. _bannerSizePercent: number = 0.1;
  92. _bannerBottom: number = 0;
  93. _oldBannerLocation: BannerLocation = BannerLocation.None;
  94. _createBanner(location: BannerLocation, args: any = null) {
  95. if (PlatUtils.IsWechat) {
  96. let locationTmp: BannerLocation = location;
  97. let argsTmp: any = args;
  98. if (argsTmp && argsTmp.width) {
  99. this._bannerSizePercent = argsTmp.width < 0 ? 0 : argsTmp.width;
  100. this._bannerSizePercent = argsTmp.width > 1 ? 1 : argsTmp.width;
  101. }
  102. if (argsTmp && argsTmp.bottom) {
  103. this._bannerBottom = argsTmp.bottom < 0 ? 0 : argsTmp.bottom;
  104. this._bannerBottom = argsTmp.bottom > cc.winSize.height ? cc.winSize.height : argsTmp.bottom;
  105. }
  106. let params = {
  107. adUnitId: utils.config.wechatconfig.getBannerId(locationTmp),
  108. style: {
  109. left: 0,
  110. width: 300,
  111. top: 0
  112. }
  113. };
  114. if (this._oldBannerLocation != locationTmp && this._curBannerAd) {
  115. this._curBannerAd.destroy();
  116. this._oldBannerLocation = locationTmp;
  117. }
  118. //@ts-ignore
  119. let curBannerAd = wx.createBannerAd(params);
  120. if (curBannerAd) {
  121. curBannerAd.onError((err) => {
  122. utils.showLog("广告条加载失败! ", JSON.stringify(err));
  123. if (curBannerAd) {
  124. curBannerAd.destroy();
  125. }
  126. if (this.ServerConfig.banner_first_ad && this.ServerConfig.banner_first_ad == "default") {
  127. this.showNativeBannerAd(location, args);
  128. }
  129. });
  130. let self = this;
  131. curBannerAd.onLoad(() => {
  132. curBannerAd.show().then(() => {
  133. let old = self._curBannerAd;
  134. if (old) {
  135. old.destroy();
  136. }
  137. self._curBannerAd = curBannerAd;
  138. utils.showLog("默认Banner广告显示成功!");
  139. this.HideNativeBanner();
  140. }).catch((err) => {
  141. utils.showLog("Banner广告出错", JSON.stringify(err));
  142. if (curBannerAd) {
  143. curBannerAd.destroy();
  144. }
  145. if (this.ServerConfig.banner_first_ad && this.ServerConfig.banner_first_ad == "default") {
  146. this.showNativeBannerAd(location, args);
  147. }
  148. });
  149. });
  150. curBannerAd.onResize((res) => {
  151. curBannerAd.style.width = utils.wechatTool.getSystemInfo().screenWidth * self._bannerSizePercent;
  152. curBannerAd.style.left = (utils.wechatTool.getSystemInfo().screenWidth - res.width) * 0.5;
  153. if (self._bannerBottom == cc.winSize.height) {
  154. curBannerAd.style.top = 0;
  155. } else {
  156. curBannerAd.style.top = utils.wechatTool.getSystemInfo().screenHeight - res.height - self._bannerBottom;
  157. }
  158. });
  159. } else {
  160. utils.showLog("广告条创建失败!");
  161. }
  162. }
  163. }
  164. createCustomADBanner(location: BannerLocation = BannerLocation.Game, args: any = null) {
  165. if (PlatUtils.IsWechat) {
  166. let locationTmp: BannerLocation = location;
  167. if (!utils.config.wechatconfig.getBannerId(locationTmp)) {
  168. locationTmp = BannerLocation.Home;
  169. }
  170. let argsTmp: any = args;
  171. if (argsTmp && argsTmp.width) {
  172. this._bannerSizePercent = argsTmp.width < 0 ? 0 : argsTmp.width;
  173. this._bannerSizePercent = argsTmp.width > 1 ? 1 : argsTmp.width;
  174. }
  175. if (argsTmp && argsTmp.bottom) {
  176. this._bannerBottom = argsTmp.bottom < 0 ? 0 : argsTmp.bottom;
  177. this._bannerBottom = argsTmp.bottom > cc.winSize.height ? cc.winSize.height : argsTmp.bottom;
  178. }
  179. let params = {
  180. adUnitId: utils.config.wechatconfig.getBannerId(locationTmp),
  181. style: {
  182. left: 0,
  183. width: 300,
  184. top: 0
  185. }
  186. };
  187. if (this._oldBannerLocation != locationTmp && this._curBannerAd) {
  188. this._curBannerAd.destroy();
  189. this._oldBannerLocation = locationTmp;
  190. }
  191. //@ts-ignore
  192. let curBannerAd = wx.createBannerAd(params);
  193. if (curBannerAd) {
  194. curBannerAd.onError((err) => {
  195. utils.showLog("广告条加载失败! ", JSON.stringify(err));
  196. if (curBannerAd) {
  197. curBannerAd.destroy();
  198. }
  199. });
  200. let self = this;
  201. curBannerAd.onLoad(() => {
  202. cc.director.on("CloseCustomADPanel", (() => {
  203. curBannerAd.show().then(() => {
  204. let old = self._curBannerAd;
  205. if (old) {
  206. old.destroy();
  207. }
  208. self._curBannerAd = curBannerAd;
  209. utils.showLog("Banner广告显示成功!");
  210. let interval: number = 18;
  211. if (utils.wechatTool.ServerConfig.refresh_ad_time) {
  212. interval = utils.wechatTool.ServerConfig.refresh_ad_time;
  213. }
  214. clearInterval(self._showBannerTimerId);
  215. //@ts-ignore
  216. self._showBannerTimerId = setInterval(function () {
  217. utils.showLog(`显示Banner广告!location:${locationTmp}; args:${JSON.stringify(argsTmp)}; 间隔时间:${utils.wechatTool.ServerConfig.refresh_ad_time}`);
  218. self._showBannerTimer(locationTmp, argsTmp);
  219. }.bind(self), interval * 1000);
  220. }).catch((err) => {
  221. utils.showLog("Banner广告出错", JSON.stringify(err));
  222. if (curBannerAd) {
  223. curBannerAd.destroy();
  224. }
  225. });
  226. cc.director.targetOff(self);
  227. }), self);
  228. });
  229. curBannerAd.onResize((res) => {
  230. curBannerAd.style.width = utils.wechatTool.getSystemInfo().screenWidth * self._bannerSizePercent;
  231. curBannerAd.style.left = (utils.wechatTool.getSystemInfo().screenWidth - res.width) * 0.5;
  232. if (self._bannerBottom == cc.winSize.height) {
  233. curBannerAd.style.top = 0;
  234. } else {
  235. curBannerAd.style.top = utils.wechatTool.getSystemInfo().screenHeight - res.height - self._bannerBottom;
  236. }
  237. });
  238. } else {
  239. utils.showLog("广告条创建失败!");
  240. }
  241. }
  242. }
  243. _initInsertAd() {
  244. if (PlatUtils.IsWechat) {
  245. if (!utils.wechatTool.isOverMinVersion("2.6.0")) {
  246. utils.showLog("当前版本不支持插屏广告!");
  247. return;
  248. }
  249. if (!this._insertAd) {
  250. utils.showLog("初始化插屏广告!");
  251. if (!utils.config.wechatconfig.insertId) {
  252. utils.showLog("插屏广告ID配置错误!");
  253. return;
  254. }
  255. utils.showLog("插屏广告ID:", utils.config.wechatconfig.insertId.trim());
  256. //@ts-ignore
  257. this._insertAd = wx.createInterstitialAd({
  258. adUnitId: utils.config.wechatconfig.insertId.trim()
  259. });
  260. if (this._insertAd) {
  261. this._insertAd.onLoad(function () {
  262. utils.showLog("插屏广告拉取成功!");
  263. });
  264. this._insertAd.onError((err) => {
  265. utils.showLog("插屏广告拉取失败!", JSON.stringify(err));
  266. if (this.ServerConfig.intersititial_first_ad && this.ServerConfig.intersititial_first_ad == "default") {
  267. this.showNativeIntersititialAd();
  268. }
  269. });
  270. this._insertAd.onClose(function () {
  271. utils.showLog("插屏广告被关闭!");
  272. });
  273. } else {
  274. utils.showLog("插屏组件初始化失败!");
  275. }
  276. }
  277. }
  278. }
  279. _showBannerTimer(location: BannerLocation, args: any) {
  280. let locationTmp: BannerLocation = location;
  281. let argsTmp: any = args;
  282. utils.showLog(`显示Banner广告xxx!location:${locationTmp}; args:${JSON.stringify(argsTmp)}; 间隔时间:${utils.wechatTool.ServerConfig.refresh_ad_time};优先级:${this.ServerConfig.banner_first_ad}`);
  283. if (this.ServerConfig.banner_first_ad && this.ServerConfig.banner_first_ad == "native") {
  284. this.showNativeBannerAd(location, args);
  285. } else {
  286. if (locationTmp == BannerLocation.None) {
  287. utils.showLog("未定义的BannerLocation,", locationTmp);
  288. } else {
  289. if (utils.config.wechatconfig.getBannerId(locationTmp)) {
  290. this._createBanner(locationTmp, argsTmp);
  291. } else {
  292. utils.showLog(`未找到位置为 ${locationTmp} 的广告ID!`);
  293. this._createBanner(BannerLocation.Home, argsTmp);
  294. }
  295. }
  296. }
  297. }
  298. _showBannerTimerId: number = 0;
  299. public ShowBanner(location: BannerLocation = BannerLocation.Home, args: any = null) {
  300. if (PlatUtils.IsWechat) {
  301. if (!utils.wechatTool.isOverMinVersion("2.0.4")) {
  302. utils.showLog("当前版本不支持Banner广告!");
  303. return;
  304. }
  305. if (utils.wechatTool.ServerConfig) {
  306. let locationTmp: BannerLocation = location;
  307. let argsTmp: any = args;
  308. this._showBannerTimer(locationTmp, argsTmp);
  309. let interval: number = 18;
  310. if (utils.wechatTool.ServerConfig.refresh_ad_time) {
  311. interval = utils.wechatTool.ServerConfig.refresh_ad_time;
  312. }
  313. clearInterval(this._showBannerTimerId);
  314. //@ts-ignore
  315. this._showBannerTimerId = setInterval(function () {
  316. utils.showLog(`显示Banner广告!location:${locationTmp}; args:${JSON.stringify(argsTmp)}; 间隔时间:${utils.wechatTool.ServerConfig.refresh_ad_time}`);
  317. this._showBannerTimer(locationTmp, argsTmp);
  318. }.bind(this), interval * 1000);
  319. } else {
  320. utils.showLog("服务器配置数据未初始化!");
  321. }
  322. }
  323. }
  324. public HideBanner(location: BannerLocation = BannerLocation.Home) {
  325. if (PlatUtils.IsWechat) {
  326. utils.showLog("隐藏广告条");
  327. clearInterval(this._showBannerTimerId);
  328. this.HideDefaultBanner();
  329. this.HideNativeBanner();
  330. }
  331. }
  332. private HideDefaultBanner(): void {
  333. this._curBannerAd && this._curBannerAd.hide();
  334. }
  335. private HideNativeBanner(): void {
  336. this.nativeBannerAd && this.nativeBannerAd.hide();
  337. }
  338. public ShowInterstitial(location: BannerLocation = BannerLocation.Home) {
  339. if (PlatUtils.IsWechat) {
  340. if (!utils.wechatTool.isOverMinVersion("2.6.0")) {
  341. utils.showLog("当前版本不支持插屏广告!");
  342. return;
  343. }
  344. let delayTime: number = 0;
  345. if (utils.wechatTool
  346. && utils.wechatTool.ServerConfig
  347. && utils.wechatTool.ServerConfig.intersititia_delay_show_time) {
  348. delayTime = utils.wechatTool.ServerConfig.intersititia_delay_show_time;
  349. }
  350. utils.showLog(`插屏广告延时展示! delayTime:${delayTime}秒`);
  351. setTimeout(() => {
  352. if (this.ServerConfig && this.ServerConfig.intersititial_first_ad && this.ServerConfig.intersititial_first_ad == "box") {
  353. utils.showLog("优先展示盒子插屏广告!");
  354. if (this.canShowBoxInsertAd()) {
  355. this._createBoxInsertAd();
  356. } else {
  357. utils.showLog("盒子插屏广告展示失败,展示默认插屏!");
  358. this._createMiniGameInsertAd();
  359. }
  360. } else if (this.ServerConfig && this.ServerConfig.intersititial_first_ad && this.ServerConfig.intersititial_first_ad == "native") {
  361. this.showNativeIntersititialAd();
  362. } else {
  363. utils.showLog("优先展示小游戏插屏广告!");
  364. this._createMiniGameInsertAd();
  365. }
  366. }, delayTime * 1000);
  367. }
  368. }
  369. _createBoxInsertAd() {
  370. utils.rewardCallFunc = null;
  371. utils.rewardCloseFunc = null;
  372. if (utils.config && utils.config.otherconfig.beforGameOverRecGamesPanel && utils.getRecommondGameList()) {
  373. let panel = cc.instantiate(utils.config.otherconfig.beforGameOverRecGamesPanel);
  374. panel.zIndex = 999999;
  375. let morePanel: BeforGameOverRecGamesPanel = panel.getComponent("BeforGameOverRecGamesPanel");
  376. cc.director.getScene().addChild(panel);
  377. morePanel._location = SubLocation.isBoxInsertAd;
  378. morePanel.init(utils.getRecommondGameList());
  379. morePanel.show();
  380. } else {
  381. utils.showLog("互推插屏展示失败!");
  382. }
  383. }
  384. /**
  385. * 是否能够显示互推插屏
  386. */
  387. canShowBoxInsertAd() {
  388. let jumpList: any = utils.getRecommondGameList();
  389. if (jumpList && jumpList.length > 0) {
  390. return true;
  391. }
  392. return false;
  393. }
  394. isFirstShowInsertAd: boolean = true; //首次展示广告
  395. _createMiniGameInsertAd() {
  396. if (this._insertAd) {
  397. this._insertAd.show().then(() => {
  398. utils.showLog("插屏广告展示成功!");
  399. if (this.isFirstShowInsertAd && utils.getConfigByKey("insert_ad_first_show_active") == "true") {
  400. this.isFirstShowInsertAd = false;
  401. utils.wechatTool.reportAttributedEvent(AttributedType.GameAddiction, AttributedKey.GameAddiction, AttributedValue.InsertAdFirstShowAction);
  402. }
  403. }).catch((err) => {
  404. utils.showLog("插屏广告展示失败!", JSON.stringify(err));
  405. if (this.ServerConfig.intersititial_first_ad && this.ServerConfig.intersititial_first_ad == "default") {
  406. this.showNativeIntersititialAd();
  407. }
  408. // if (this.ServerConfig && this.ServerConfig.intersititial_first_ad == "default") {
  409. // utils.showLog("优先展示默认插屏,显示备选盒子插屏!");
  410. // if (this.canShowBoxInsertAd()) {
  411. // this._createBoxInsertAd();
  412. // }
  413. // }
  414. });
  415. } else {
  416. utils.showLog("插屏广告未初始化");
  417. if (this.ServerConfig.intersititial_first_ad && this.ServerConfig.intersititial_first_ad == "default") {
  418. this.showNativeIntersititialAd();
  419. }
  420. // if (this.ServerConfig && this.ServerConfig.intersititial_first_ad == "default") {
  421. // utils.showLog("优先展示默认插屏,显示备选盒子插屏!");
  422. // if (this.canShowBoxInsertAd()) {
  423. // this._createBoxInsertAd();
  424. // }
  425. // }
  426. }
  427. }
  428. public ShowVideo(callback: Function) {
  429. if (PlatUtils.IsWechat) {
  430. this._videoCallback = callback;
  431. if (this.checkRewardInsertIsShow() && utils.wechatTool
  432. && utils.wechatTool.ServerConfig
  433. && utils.wechatTool.ServerConfig.reward_first_ad && utils.wechatTool.ServerConfig.reward_first_ad != "video") {
  434. utils.showLog("<<<服务器默认优先展示激励插屏>>>");
  435. this.showRewardInsert();
  436. return;
  437. }
  438. if (!utils.wechatTool.isOverMinVersion("2.0.4")) {
  439. utils.showLog("当前版本不支持视频广告!");
  440. if (this._videoCallback) {
  441. // this._videoCallback(false, "暂无视频广告!");
  442. this.showRewardInsert();
  443. }
  444. return;
  445. }
  446. if (this._videoAd) {
  447. this._videoAd.show().then(function () {
  448. utils.showLog("视频显示成功!");
  449. }.bind(this)).catch(function (err) {
  450. utils.showLog("视频未加载!");
  451. this._videoAd.load();
  452. if (this._videoCallback) {
  453. // this._videoCallback(false, "暂无视频广告!");
  454. this.showRewardInsert();
  455. }
  456. }.bind(this));
  457. } else {
  458. utils.showLog("视频未初始化!");
  459. if (this._videoCallback) {
  460. // this._videoCallback(false, "暂无视频广告!");
  461. this.showRewardInsert();
  462. }
  463. }
  464. }
  465. }
  466. /**
  467. * 验证是否显示激励插屏
  468. */
  469. private checkRewardInsertIsShow() {
  470. let jumpList = utils.getRecommondGameList();
  471. if (utils.isSupportnavigateToMiniGame()) {
  472. if (utils.wechatTool
  473. && utils.wechatTool.ServerConfig
  474. && utils.wechatTool.ServerConfig.is_reward_intersititia) {
  475. if (utils.wechatTool.ServerConfig.is_reward_intersititia == "true"
  476. && jumpList && jumpList.length > 0) {
  477. utils.showLog("激励插屏显示环境验证通过!");
  478. return true;
  479. } else {
  480. cc.warn("is_reward_intersititia 参数为false,激励插屏组件不显示!");
  481. return false;
  482. }
  483. } else {
  484. cc.warn("配置中没有is_reward_intersititia参数,激励插屏组件组件不显示!");
  485. return false;
  486. }
  487. }
  488. utils.showLog("当前平台不支持小程序跳转!");
  489. return false;
  490. }
  491. /**
  492. * 显示激励插屏组件
  493. */
  494. public showRewardInsert() {
  495. utils.showLog("show reward");
  496. if (!this.checkRewardInsertIsShow()) {
  497. utils.adManager.videoCallBack && utils.adManager.videoCallBack(false, "暂无视频广告!");
  498. utils.adManager.videoCallBack = null;
  499. return;
  500. }
  501. if (((!cc.isValid(this._rewardInsertNode)) || !this._rewardInsertNode) && utils.config.otherconfig.rewardInsert) {
  502. utils.showLog("创建激励插屏广告");
  503. this._rewardInsertNode = cc.instantiate(utils.config.otherconfig.rewardInsert);
  504. this._rewardInsertNode.position = CompatibleTool.position(cc.winSize.width / 2, cc.winSize.height / 2);
  505. cc.director.getScene().addChild(this._rewardInsertNode, 9999);
  506. }
  507. if (this._rewardInsertNode) {
  508. let rewardInsert: RewardInsert = this._rewardInsertNode.getComponent("RewardInsert");
  509. if (rewardInsert) {
  510. rewardInsert.isShow = false;
  511. utils.showLog("显示激励插屏组件!");
  512. } else {
  513. utils.showLog("RewardInsert组件不存在!");
  514. }
  515. } else {
  516. utils.showLog("激励插屏没有创建!");
  517. }
  518. }
  519. public hideRewardInsert() {
  520. if (this._rewardInsertNode) {
  521. let rewardInsert: RewardInsert = this._rewardInsertNode.getComponent("RewardInsert");
  522. if (rewardInsert) {
  523. rewardInsert.hide();
  524. utils.showLog("隐藏激励插屏组件!");
  525. } else {
  526. utils.showLog("RewardInsert组件不存在!");
  527. }
  528. } else {
  529. utils.showLog("激励插屏没有创建!");
  530. }
  531. }
  532. public ShowCloseBtnBanner(location: BannerLocation = BannerLocation.Home, args: any) {
  533. if (PlatUtils.IsWechat) {
  534. if (utils.wechatTool && !utils.wechatTool.isOverMinVersion("2.0.4")) {
  535. utils.showLog("当前版本不支持Banner广告!");
  536. return;
  537. }
  538. if (utils.wechatTool.ServerConfig && (!utils.wechatTool.ServerConfig.isMoveBtn || utils.wechatTool.ServerConfig.isMoveBtn != "true")) {
  539. utils.showLog("服务器没有开启移动按钮,不显示广告!");
  540. return;
  541. }
  542. if (utils.wechatTool && utils.wechatTool.ServerConfig) {
  543. if (PlatUtils.IsWechat) {
  544. let locationTmp: BannerLocation = location;
  545. // let argsTmp: any = args;
  546. let params = {
  547. adUnitId: utils.config.wechatconfig.getBannerId(locationTmp),
  548. style: {
  549. left: 0,
  550. width: 300,
  551. top: 0
  552. }
  553. };
  554. if (this._oldBannerLocation != locationTmp && this._curBannerAd) {
  555. this._curBannerAd.destroy();
  556. this._oldBannerLocation = locationTmp;
  557. }
  558. //@ts-ignore
  559. let curBannerAd = wx.createBannerAd(params);
  560. if (curBannerAd) {
  561. curBannerAd.onError((err) => {
  562. utils.showLog("广告条加载失败! ", JSON.stringify(err));
  563. if (curBannerAd) {
  564. curBannerAd.destroy();
  565. }
  566. });
  567. let self = this;
  568. curBannerAd.onLoad(() => {
  569. curBannerAd.show().then(() => {
  570. let old = self._curBannerAd;
  571. if (old) {
  572. old.destroy();
  573. }
  574. self._curBannerAd = curBannerAd;
  575. let closeBtn = args.closeBtn;
  576. if (!closeBtn) {
  577. return;
  578. }
  579. // closeBtn.active = true;
  580. //调整关闭按钮位置
  581. let winHeight = cc.winSize.height;
  582. var adY = self.getBannerAdHeight();
  583. utils.showLog('utils - adY:', adY);
  584. if (adY > 0) {
  585. closeBtn.y = -(winHeight / 2 - adY) + closeBtn.height;
  586. utils.showLog("btnClose.y", closeBtn.y);
  587. }
  588. utils.showLog("关闭按钮---Banner广告显示成功!");
  589. }).catch((err) => {
  590. utils.showLog("Banner广告出错", JSON.stringify(err));
  591. if (curBannerAd) {
  592. curBannerAd.destroy();
  593. }
  594. });
  595. });
  596. curBannerAd.onResize((res) => {
  597. curBannerAd.style.width = utils.wechatTool.getSystemInfo().screenWidth * 0.6;
  598. curBannerAd.style.left = (utils.wechatTool.getSystemInfo().screenWidth - res.width) * 0.5;
  599. // if (self._bannerBottom == cc.winSize.height) {
  600. // curBannerAd.style.top = 0;
  601. // } else {
  602. curBannerAd.style.top = utils.wechatTool.getSystemInfo().screenHeight - res.height;
  603. // }
  604. });
  605. } else {
  606. utils.showLog("广告条创建失败!");
  607. }
  608. }
  609. } else {
  610. utils.showLog("服务器配置数据未初始化!");
  611. }
  612. }
  613. }
  614. /**
  615. * 是否能显示6个元素的交叉推广组件
  616. */
  617. public canShowCrossWidget6() {
  618. if (PlatUtils.IsWechat) {
  619. if (utils.isSupportnavigateToMiniGame()) {
  620. return true;
  621. } else {
  622. cc.warn("当前平台不支持游戏内跳转,6元素交叉推广组件不显示!");
  623. return false;
  624. }
  625. }
  626. }
  627. /**
  628. * 显示6元素交叉推广组件
  629. */
  630. public showCrossWidget6(): cc.Node {
  631. if (this.canShowCrossWidget6()) {
  632. if (utils.config.otherconfig.crossWidget6) {
  633. return cc.instantiate(utils.config.otherconfig.crossWidget6);
  634. } else {
  635. utils.showLog("未找到预制体 CrossWidget6, 请查看CommonUtils组件上是否赋值!");
  636. }
  637. }
  638. return null;
  639. }
  640. /**
  641. * 显示结算广告
  642. * @param data 参数: closeBtn:
  643. * statement_type
  644. * 1:只显示小游戏插屏广告
  645. * 2:只显示6个互推广告
  646. * 3:显示插屏广告+6个互推
  647. */
  648. showStatementAds(data?: any): any {
  649. let result: any = { "type": 0, "node": null };
  650. let node: cc.Node = null;
  651. this.ShowInterstitial();
  652. if (utils.canShowCrossWidget6()) {
  653. utils.showLog("服务器配置显示6个互推组件");
  654. node = this.showCrossWidget6();
  655. result.type = 1;
  656. result.node = node;
  657. return result;
  658. }
  659. return result;
  660. // if (this.ServerConfig && this.ServerConfig.statement_type) {
  661. // // let type = this.ServerConfig.statement_type;
  662. // let node: cc.Node = null;
  663. // let resType: number = 0;
  664. // this.ShowInterstitial();
  665. // if (this.canShowCrossWidget6()) {
  666. // node = this.showCrossWidget6();
  667. // resType = 1;
  668. // }
  669. // // switch (type) {
  670. // // case 1:
  671. // // utils.showLog("结算广告 >> 只显示小游戏插屏广告");
  672. // // this.ShowInterstitial();
  673. // // break;
  674. // // case 2:
  675. // // utils.showLog("结算广告 >> 只显示6个互推广告");
  676. // // node = this.showCrossWidget6();
  677. // // resType = 1;
  678. // // break;
  679. // // case 3:
  680. // // utils.showLog("结算广告 >> 显示插屏广告+6个互推");
  681. // // this.ShowInterstitial();
  682. // // node = this.showCrossWidget6();
  683. // // resType = 1;
  684. // // break;
  685. // // default:
  686. // // utils.showLog("非法的结算广告类型,:", type)
  687. // // break;
  688. // // }
  689. // result.type = resType;
  690. // result.node = node;
  691. // return result;
  692. // } else {
  693. // cc.warn("配置未初始化!");
  694. // return result;
  695. // }
  696. }
  697. /**
  698. * 获取banner广告高度
  699. */
  700. public getBannerAdHeight() {
  701. if (this._curBannerAd) {
  702. let i = this._curBannerAd.style.realHeight * 2;
  703. if (i === null || i === undefined || isNaN(i)) {
  704. return 0;
  705. } else {
  706. return i;
  707. }
  708. }
  709. return 0;
  710. }
  711. _customAdObjs: any[] = [];
  712. public showNativeTryGameWidget(params: any = null) {
  713. if (!utils.wechatTool.isOverMinVersion("2.11.1")) {
  714. utils.showLog("当前版本不支持原生广告!");
  715. return;
  716. }
  717. if (!this.ServerConfig) {
  718. utils.showLog("组件未初始化!");
  719. return;
  720. }
  721. if (!utils.config.wechatconfig.bannerBoxId) {
  722. utils.showLog("原生广告ID不存在");
  723. return;
  724. }
  725. let top = 0;
  726. let left = 0;
  727. if (params.top) {
  728. top = params.top / cc.winSize.height * this.sysInfo.screenHeight
  729. } else {
  730. top = this.sysInfo.screenHeight - 112 - params.bottom / cc.winSize.height * this.sysInfo.screenHeight;
  731. }
  732. if (params.left) {
  733. left = params.left / cc.winSize.width * this.sysInfo.screenWidth;
  734. } else if (params.right) {
  735. left = this.sysInfo.screenWidth - 70 - params.right / cc.winSize.width * this.sysInfo.screenWidth;
  736. }
  737. //@ts-ignore
  738. let customAd = wx.createCustomAd({
  739. // adUnitId: utils.config.wechatconfig.bannerBoxId,
  740. adUnitId: utils.config.wechatconfig.bannerBoxId,
  741. adIntervals: 30,
  742. style: {
  743. left: left,
  744. top: top
  745. }
  746. });
  747. customAd.onError((erro) => {
  748. utils.showLog("原生广告异常!>>>>> #code=" + erro.errCode + " #msg=" + erro.errMsg);
  749. if (params.onError) {
  750. utils.showLog("执行微信原生广告异常回调!")
  751. params.onError();
  752. }
  753. })
  754. customAd.onLoad(() => {
  755. utils.showLog("微信原生广告加载成功!");
  756. customAd.show().then(() => {
  757. if (params.onSuccess) {
  758. utils.showLog("执行微信原生广告显示成功回调!")
  759. params.onSuccess();
  760. }
  761. utils.showLog("微信原生广告显示成功!");
  762. }).catch((err) => {
  763. utils.showLog("微信原生广告显示失败!");
  764. });
  765. })
  766. this._customAdObjs.push(customAd);
  767. }
  768. _nativeCustomAdObjs: any[] = [];
  769. public showCustomAd(params: any = null) {
  770. if (!utils.wechatTool.isOverMinVersion("2.11.1")) {
  771. utils.showLog("当前版本不支持原生模版广告!");
  772. return;
  773. }
  774. if (!this.ServerConfig) {
  775. utils.showLog("组件未初始化!");
  776. return;
  777. }
  778. if (utils.config.wechatconfig.customAdInfos.length < 1) {
  779. utils.showLog("原生广告配置不存在");
  780. return;
  781. }
  782. let customAdInfo: CustomAdInfo = utils.config.wechatconfig.getCustomAdInfoInfo(params.location);
  783. if (!customAdInfo) {
  784. utils.showLog("当前位置未配置模版广告!");
  785. return;
  786. }
  787. // utils.showLog("显示原生模版广告:位置:" + params.location + ",配置:" + customAdInfo.toStrong);
  788. let top = 0;
  789. let left = 0;
  790. // about:左右居中
  791. // updown:上下
  792. // all:上下和左右居中
  793. // false:自定义对齐方式
  794. if (customAdInfo.is_center == "false") {
  795. if (customAdInfo.top > -1) {
  796. top = customAdInfo.top / cc.winSize.height * this.sysInfo.screenHeight
  797. } else {
  798. top = this.sysInfo.screenHeight - customAdInfo.height - customAdInfo.bottom / cc.winSize.height * this.sysInfo.screenHeight;
  799. }
  800. if (customAdInfo.left > -1) {
  801. left = customAdInfo.left / cc.winSize.width * this.sysInfo.screenWidth;
  802. } else {
  803. left = this.sysInfo.screenWidth - customAdInfo.width - customAdInfo.right / cc.winSize.width * this.sysInfo.screenWidth;
  804. }
  805. } else if (customAdInfo.is_center == "updown") {
  806. utils.showLog("原生模版上下居中对齐!")
  807. if (customAdInfo.left > -1) {
  808. left = customAdInfo.left / cc.winSize.width * this.sysInfo.screenWidth;
  809. } else {
  810. left = this.sysInfo.screenWidth - customAdInfo.width - customAdInfo.right / cc.winSize.width * this.sysInfo.screenWidth;
  811. }
  812. top = (this.sysInfo.screenHeight - customAdInfo.height) / 2;
  813. } else if (customAdInfo.is_center == "about") {
  814. utils.showLog("原生模版左右居中对齐!")
  815. left = (this.sysInfo.screenWidth - customAdInfo.width) / 2;
  816. if (customAdInfo.top > -1) {
  817. top = customAdInfo.top / cc.winSize.height * this.sysInfo.screenHeight
  818. } else {
  819. top = this.sysInfo.screenHeight - customAdInfo.height - customAdInfo.bottom / cc.winSize.height * this.sysInfo.screenHeight;
  820. }
  821. }
  822. else {
  823. utils.showLog("原生模版上下左右居中对齐!")
  824. left = (this.sysInfo.screenWidth - customAdInfo.width) / 2;
  825. top = (this.sysInfo.screenHeight - customAdInfo.height) / 2;
  826. }
  827. //@ts-ignore
  828. let customAd = wx.createCustomAd({
  829. adUnitId: customAdInfo.id,
  830. adIntervals: customAdInfo.refresh_time,
  831. style: {
  832. left: left,
  833. top: top,
  834. width: customAdInfo.width
  835. }
  836. });
  837. customAd.location = params.location;
  838. utils.showLog("customAd style left=" + left + ",top=" + top + "; screenWidth=" + this.sysInfo.screenWidth + ", screenHeight=" + this.sysInfo.screenHeight)
  839. customAd.onError((erro) => {
  840. utils.showLog("原生广告异常!>>>>> #code=" + erro.errCode + " #msg=" + erro.errMsg);
  841. if (params.onError) {
  842. utils.showLog("执行微信原生广告异常回调!")
  843. params.onError();
  844. }
  845. })
  846. customAd.onLoad(() => {
  847. utils.showLog("微信原生广告加载成功!");
  848. customAd.show().then(() => {
  849. if (params.onSuccess) {
  850. utils.showLog("执行微信原生广告显示成功回调!")
  851. params.onSuccess();
  852. }
  853. utils.showLog("微信原生广告显示成功!");
  854. }).catch((err) => {
  855. utils.showLog("微信原生广告显示失败!");
  856. });
  857. })
  858. this._customAdObjs.push(customAd);
  859. }
  860. /**
  861. * 隐藏原生模版广告
  862. */
  863. hideCustomAd(params?: any) {
  864. for (let i = 0; i < this._customAdObjs.length; i++) {
  865. if (this._customAdObjs[i]) {
  866. if (params && "location" in params) {
  867. if (this._customAdObjs[i].location == params.location) {
  868. utils.showLog("隐藏位置:" + params.location + "的原生模版广告");
  869. this._customAdObjs[i].destroy()
  870. this._customAdObjs.splice(i, 1)
  871. }
  872. } else {
  873. this._customAdObjs[i].destroy()
  874. this._customAdObjs.splice(i, 1)
  875. }
  876. }
  877. }
  878. if (!params || !("location" in params)) utils.showLog("隐藏所有位置的原生模版广告");
  879. }
  880. /**
  881. * 隐藏原生试玩广告
  882. */
  883. hideNativeTryGameWidget() {
  884. for (let i = 0; i < this._customAdObjs.length; i++) {
  885. if (this._customAdObjs[i]) {
  886. this._customAdObjs[i].destroy()
  887. }
  888. }
  889. for (let i = 0; i < this._customAdObjs.length; i++) {
  890. this._customAdObjs.splice(i, 1)
  891. }
  892. }
  893. nativeBannerAd: any = null;
  894. public showNativeBannerAd(location: BannerLocation, args: any = null) {
  895. utils.showLog("展示原生banner广告!");
  896. if (!utils.wechatTool.isOverMinVersion("2.11.1")) {
  897. utils.showLog("当前版本不支持原生模版广告!");
  898. return;
  899. }
  900. if (!this.ServerConfig) {
  901. utils.showLog("组件未初始化!");
  902. return;
  903. }
  904. if (!utils.config.wechatconfig.nativeBannerId) {
  905. utils.showLog("原生Banner广告ID配置不存在");
  906. return;
  907. }
  908. let bannerWidth = 325;
  909. let bannerHeight = 100;
  910. let left = (this.sysInfo.screenWidth - bannerWidth) / 2;
  911. let top = this.sysInfo.screenHeight - bannerHeight;
  912. if (this.nativeBannerAd) {
  913. if (this.nativeBannerAd.isShow()) {
  914. utils.showLog("原生Banner正在显示,不重新创建!")
  915. } else {
  916. this.nativeBannerAd.show().then(() => {
  917. utils.showLog("原生Banner广告显示成功!");
  918. this.HideDefaultBanner();
  919. }).catch((err) => {
  920. utils.showLog("原生Banner广告显示失败!");
  921. this.nativeBannerAd && this.nativeBannerAd.destroy();
  922. this.nativeBannerAd = null;
  923. if (this.ServerConfig.banner_first_ad && this.ServerConfig.banner_first_ad == "native") {
  924. this._createBanner(location, args);
  925. }
  926. });
  927. }
  928. return;
  929. }
  930. //@ts-ignore
  931. let customAd = wx.createCustomAd({
  932. adUnitId: utils.config.wechatconfig.nativeBannerId,
  933. adIntervals: utils.wechatTool.ServerConfig.refresh_ad_time || 30,
  934. style: {
  935. left: left,
  936. top: top,
  937. width: bannerWidth
  938. }
  939. });
  940. utils.showLog("native nanner ad style left=" + left + ",top=" + top + "; screenWidth=" + this.sysInfo.screenWidth + ", screenHeight=" + this.sysInfo.screenHeight)
  941. customAd.onError((erro) => {
  942. utils.showLog("原生Banner广告异常!>>>>> #code=" + erro.errCode + " #msg=" + erro.errMsg);
  943. this.nativeBannerAd && this.nativeBannerAd.destroy();
  944. this.nativeBannerAd = null;
  945. if (this.ServerConfig.banner_first_ad && this.ServerConfig.banner_first_ad == "native") {
  946. this._createBanner(location, args);
  947. }
  948. })
  949. customAd.onLoad(() => {
  950. utils.showLog("原生Banner广告加载成功!");
  951. customAd.show().then(() => {
  952. this.nativeBannerAd && this.nativeBannerAd.destroy();
  953. this.nativeBannerAd = customAd;
  954. utils.showLog("原生Banner广告显示成功!");
  955. this.HideDefaultBanner();
  956. }).catch((err) => {
  957. utils.showLog("原生Banner广告显示失败!");
  958. this.nativeBannerAd && this.nativeBannerAd.destroy();
  959. this.nativeBannerAd = null;
  960. if (this.ServerConfig.banner_first_ad && this.ServerConfig.banner_first_ad == "native") {
  961. this._createBanner(location, args);
  962. }
  963. });
  964. })
  965. }
  966. nativeIntersititialAd: any = null;
  967. public showNativeIntersititialAd() {
  968. utils.showLog("展示原生插屏广告!");
  969. if (!utils.wechatTool.isOverMinVersion("2.11.1")) {
  970. utils.showLog("当前版本不支持原生模版广告!");
  971. return;
  972. }
  973. if (!this.ServerConfig) {
  974. utils.showLog("组件未初始化!");
  975. return;
  976. }
  977. if (!utils.config.wechatconfig.nativeInsertIds) {
  978. utils.showLog("原生插屏广告ID配置不存在");
  979. return;
  980. }
  981. let insertWidth = 345;
  982. let insertHeight = 420;
  983. if (cc.winSize.width > cc.winSize.height) {
  984. insertHeight = 300
  985. }
  986. let left = (this.sysInfo.screenWidth - insertWidth) / 2;
  987. let top = (this.sysInfo.screenHeight - insertHeight) / 2;
  988. if (this.nativeIntersititialAd) {
  989. if (this.nativeIntersititialAd.isShow()) {
  990. utils.showLog("原生插屏正在显示,不重新创建!")
  991. } else {
  992. this.nativeIntersititialAd.show().then(() => {
  993. utils.showLog("原生插屏广告显示成功!");
  994. }).catch((err) => {
  995. utils.showLog("原生插屏广告显示失败!");
  996. this.nativeIntersititialAd && this.nativeIntersititialAd.destroy();
  997. this.nativeIntersititialAd = null;
  998. if (this.ServerConfig.intersititial_first_ad && this.ServerConfig.intersititial_first_ad == "native") {
  999. this._createMiniGameInsertAd();
  1000. }
  1001. });
  1002. }
  1003. return;
  1004. }
  1005. //@ts-ignore
  1006. let customAd = wx.createCustomAd({
  1007. adUnitId: utils.config.wechatconfig.nativeInsertIds,
  1008. adIntervals: 60,
  1009. style: {
  1010. left: left,
  1011. top: top,
  1012. width: insertWidth
  1013. }
  1014. });
  1015. utils.showLog("native insert ad style left=" + left + ",top=" + top + "; screenWidth=" + this.sysInfo.screenWidth + ", screenHeight=" + this.sysInfo.screenHeight)
  1016. customAd.onError((erro) => {
  1017. utils.showLog("原生插屏广告异常!>>>>> #code=" + erro.errCode + " #msg=" + erro.errMsg);
  1018. this.nativeIntersititialAd && this.nativeIntersititialAd.destroy();
  1019. this.nativeIntersititialAd = null;
  1020. if (this.ServerConfig.intersititial_first_ad && this.ServerConfig.intersititial_first_ad == "native") {
  1021. this._createMiniGameInsertAd();
  1022. }
  1023. })
  1024. customAd.onLoad(() => {
  1025. utils.showLog("原生插屏广告加载成功!");
  1026. customAd.show().then(() => {
  1027. this.nativeIntersititialAd && this.nativeIntersititialAd.destroy();
  1028. this.nativeIntersititialAd = customAd;
  1029. utils.showLog("原生插屏广告显示成功!");
  1030. }).catch((err) => {
  1031. utils.showLog("原生插屏广告显示失败!");
  1032. this.nativeIntersititialAd && this.nativeIntersititialAd.destroy();
  1033. this.nativeIntersititialAd = null;
  1034. if (this.ServerConfig.intersititial_first_ad && this.ServerConfig.intersititial_first_ad == "native") {
  1035. this._createMiniGameInsertAd();
  1036. }
  1037. });
  1038. })
  1039. }
  1040. }