YZ_NativeInsert.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. import PlatUtils from "./PlatUtils";
  2. import { utils } from "./Utils";
  3. import CompatibleTool from "./CompatibleTool";
  4. import YZ_Constant from "./YZ_Constant";
  5. import { YwAdType, YwAdStatus, AdEventParameter } from "./YouWanSDK/EventAdInfo";
  6. import YouWanAnalytics from "./YouWanSDK/YouWanAnalytics";
  7. const { ccclass, property } = cc._decorator;
  8. @ccclass
  9. export default class YZ_NativeInsert extends cc.Component {
  10. _panel: cc.Node = null;
  11. _sourceLable: cc.Label = null;
  12. _titleLabel: cc.Label = null;
  13. _desLabel: cc.Label = null;
  14. _icon: cc.Sprite = null;
  15. _mask: cc.Node = null;
  16. _btnLabel: cc.Label = null;
  17. _closeBtn: cc.Node = null;
  18. _img: cc.Sprite = null;
  19. _hideInstallBtn: cc.Node = null;
  20. _nativeAd: any = null;
  21. _data: any = null;
  22. _dataDirty: boolean = false;
  23. public get ServerConfig() {
  24. return utils.ServerConfig;
  25. }
  26. bgTexture: cc.SpriteFrame;
  27. btnTexture: cc.SpriteFrame;
  28. _btnDownLoad: cc.Node = null;
  29. _defaultCloseSize = 60;
  30. onLoad() {
  31. if (utils.otherConfig && utils.otherConfig.group) {
  32. this.node.group = utils.otherConfig.group;
  33. }
  34. cc.game.addPersistRootNode(this.node);
  35. this._panel = this.node.getChildByName("Panel");
  36. this._icon = this._panel.getChildByName("Icon").getComponent(cc.Sprite);
  37. this._img = this._panel.getChildByName("Img").getComponent(cc.Sprite);
  38. this._titleLabel = this._panel.getChildByName("TitleLabel").getComponent(cc.Label);
  39. this._desLabel = this._panel.getChildByName("DesLabel").getComponent(cc.Label);
  40. this._mask = this.node.getChildByName("Mask");
  41. this._sourceLable = this._panel.getChildByName("Img").getChildByName("source").getComponent(cc.Label);
  42. this._btnDownLoad = this._panel.getChildByName("Btn_Download");
  43. this._btnLabel = this._btnDownLoad.getComponentInChildren(cc.Label);
  44. this._hideInstallBtn = this._panel.getChildByName("Btn_HideInstall");
  45. this._closeBtn = this._hideInstallBtn.getChildByName("Btn_Close");
  46. let self = this;
  47. let bg = this._panel.getChildByName("bg").getComponent(cc.Sprite);
  48. let bgUrl = utils.ServerConfig.native_intersititial_bg_img;
  49. if (bgUrl && !this.bgTexture) {
  50. CompatibleTool.LoadRes(bgUrl, (err, texture) => {
  51. if (!err && cc.isValid(self) && self._panel) {
  52. let size: cc.Size = self._panel.getChildByName("bg").getContentSize();
  53. self.bgTexture = new cc.SpriteFrame(texture);
  54. bg.spriteFrame = self.bgTexture;
  55. self._panel.getChildByName("bg").setContentSize(size);
  56. }
  57. });
  58. }
  59. let back = this._panel.getChildByName("Btn_Install").getChildByName("Background").getComponent(cc.Sprite);
  60. let backUrl = utils.ServerConfig.native_intersititial_btn_img;
  61. if (backUrl && !this.btnTexture) {
  62. CompatibleTool.LoadRes(backUrl, (err, texture) => {
  63. if (!err && cc.isValid(self) && self._panel) {
  64. let size: cc.Size = self._panel.getChildByName("Btn_Install").getContentSize();
  65. self.btnTexture = new cc.SpriteFrame(texture);
  66. back.spriteFrame = self.btnTexture;
  67. self._panel.getChildByName("Btn_Install").setContentSize(size);
  68. }
  69. });
  70. }
  71. let ratio: number = 1;
  72. if (cc.winSize.height < cc.winSize.width) {
  73. // 横屏游戏
  74. ratio = cc.winSize.width / 1920 * 0.7;
  75. // if (PlatUtils.IsVIVO) {
  76. // ratio = cc.winSize.width / 1920 * 0.7 * (1280 / utils.cur_tool.SysInfo.screenWidth);
  77. // } else {
  78. // if (!PlatUtils.IsHuaWei) {
  79. // this._panel.width = cc.winSize.width / 2;
  80. // this._panel.height = cc.winSize.height * 0.9;
  81. // this._desLabel.getComponent(cc.Widget).bottom = 200;
  82. // this._desLabel.getComponent(cc.Widget).updateAlignment();
  83. // this._panel.getChildByName("Btn_Install").getComponent(cc.Widget).bottom = 50;
  84. // this._panel.getChildByName("Btn_Install").getComponent(cc.Widget).updateAlignment();
  85. // this._img.node.getComponent(cc.Widget).top = (this._titleLabel.node.y - this._desLabel.node.y) / 3;
  86. // this._img.node.getComponent(cc.Widget).bottom = (this._titleLabel.node.y - this._desLabel.node.y) / 2
  87. // this._img.node.getComponent(cc.Widget).updateAlignment();
  88. // this._titleLabel.node.getComponent(cc.Widget).top = 70;
  89. // this._titleLabel.node.getComponent(cc.Widget).updateAlignment();
  90. // this._closeBtn.getComponent(cc.Widget).top = 65;
  91. // this._closeBtn.getComponent(cc.Widget).updateAlignment();
  92. // }else{
  93. this._panel.scale = ratio;
  94. // }
  95. } else {
  96. this._panel.getComponent(cc.Widget).isAlignBottom = true;
  97. this._panel.getComponent(cc.Widget).bottom = 378;
  98. this._panel.getComponent(cc.Widget).updateAlignment();
  99. ratio = cc.winSize.width / 1080;
  100. this._panel.scale = ratio;
  101. }
  102. /**
  103. if (PlatUtils.IsHuaWei) {
  104. let closeBtnRange = this.ServerConfig.intersititia_close_but_range ? this.ServerConfig.intersititia_close_but_range : this._closeBtn.getContentSize().height;
  105. let closeBtnSize = this.ServerConfig.intersititia_close_but_size ? this.ServerConfig.intersititia_close_but_size : this._closeBtn.getContentSize().height;
  106. this._closeBtn.setContentSize(cc.size(closeBtnRange, closeBtnRange));
  107. this._closeBtn.getChildByName("Background").setContentSize(cc.size(closeBtnSize, closeBtnSize));
  108. this._btnDownLoad.active = false;
  109. //@ts-ignore
  110. // qg.onShow(this._reportAdShow.bind(this));
  111. }
  112. */
  113. if (PlatUtils.IsXiaoMi) {
  114. this._panel.getChildByName("Xm_Mask").active = true;
  115. }
  116. // this._panel.scale = ratio;
  117. }
  118. onEnable() {
  119. if (PlatUtils.IsHuaWei) {
  120. utils.showLog("注册原生插屏监听事件 >>>>>.");
  121. cc.game.targetOff(this);
  122. cc.game.on(cc.game.EVENT_SHOW, () => {
  123. console.log("HuaWeiOnShow >>>>>>");
  124. this._reportAdShow();
  125. }, this);
  126. }
  127. if (PlatUtils.IsOPPO || PlatUtils.IsVIVO || PlatUtils.IsHuaWei || PlatUtils.IsXiaoMi) {
  128. this._panel.on(cc.Node.EventType.TOUCH_START, (event: cc.Event) => {
  129. this._reportAdClick();
  130. }, this);
  131. cc.game.on(YZ_Constant.YZ_NativeAdClick, () => {
  132. this._reportAdClick();
  133. }, this);
  134. }
  135. }
  136. onDestroy() {
  137. if (PlatUtils.IsHuaWei) {
  138. cc.game.targetOff(this);
  139. utils.adManager.ShowBanner();
  140. }
  141. }
  142. init(nativeAd: any, data: any) {
  143. this._nativeAd = nativeAd;
  144. this._data = data;
  145. this._dataDirty = true;
  146. }
  147. update(dt: number) {
  148. if (this._data && this._dataDirty) {
  149. this._panel.active = true;
  150. this._mask.active = true;
  151. this._dataDirty = false;
  152. this._updateContent();
  153. }
  154. if (!this._data) {
  155. this._panel.active = false;
  156. this._mask.active = false;
  157. }
  158. }
  159. _updateContent() {
  160. // this.ServerConfig.intersititia_close_but_range = 10;
  161. // this.ServerConfig.intersititia_close_but_size = 90;
  162. if (this._data) {
  163. // 上报原生插屏广告
  164. this._reportAdShow();
  165. utils.nativeInsertShowCount++;
  166. if (this.ServerConfig.first_interstitial_delayed_level_close) {
  167. if (utils.nativeInsertShowCount > 3 && this.ServerConfig.interstitial_delayed_level_close) {
  168. utils.nativeInsertResizeCloseBtnShowCount++;
  169. if (utils.nativeInsertResizeCloseBtnShowCount % this.ServerConfig.interstitial_delayed_level_close == 0) {
  170. utils.showLog(`原生插屏关闭按钮设置服务器配置大小${this.ServerConfig.intersititia_close_but_size}`);
  171. let closeBtnSize = this.ServerConfig.intersititia_close_but_size ? this.ServerConfig.intersititia_close_but_size : this._defaultCloseSize;
  172. this._hideInstallBtn.setContentSize(cc.size(closeBtnSize, closeBtnSize));
  173. }
  174. } else {
  175. utils.showLog(`原生插屏关闭按钮,不满足服务器条件,设置为默认大小!`);
  176. this._hideInstallBtn.setContentSize(cc.size(this._defaultCloseSize, this._defaultCloseSize));
  177. }
  178. } else {
  179. let closeBtnSize = this.ServerConfig.intersititia_close_but_size ? this.ServerConfig.intersititia_close_but_size : this._defaultCloseSize;
  180. this._hideInstallBtn.setContentSize(cc.size(closeBtnSize, closeBtnSize));
  181. }
  182. this._closeBtn.getChildByName("Background").opacity = CC_DEBUG ? 255 : 0;
  183. this._hideInstallBtn.opacity = this.ServerConfig.intersititia_close_but_alpha ? this.ServerConfig.intersititia_close_but_alpha : 255;
  184. let closeBtnRange = this.ServerConfig.intersititia_close_but_range ? this.ServerConfig.intersititia_close_but_range : this._defaultCloseSize;
  185. this._closeBtn.setContentSize(cc.size(closeBtnRange, closeBtnRange));
  186. this._titleLabel.string = this._data.title ? this._data.title : "";
  187. this._desLabel.string = this._data.desc ? this._data.desc : "";
  188. this._btnLabel.string = this._data.clickBtnTxt ? this._data.clickBtnTxt : "查看详情";
  189. if (this._data.imgUrlList && this._data.imgUrlList.length > 0) {
  190. // 有图片,优先显示图片
  191. this._titleLabel.node.active = true;
  192. this._icon.node.active = false;
  193. this._img.node.active = true;
  194. this._desLabel.node.active = true;
  195. CompatibleTool.LoadRes(this._data.imgUrlList[0], (err, res) => {
  196. utils.showLog("加载图片信息 ...." + err);
  197. if (!err && cc.isValid(this) && this._img) {
  198. this._img.spriteFrame = new cc.SpriteFrame(res);
  199. }
  200. });
  201. } else if (PlatUtils.IsOPPO && this._data.iconUrlList && this._data.iconUrlList.length > 0) {
  202. // 有icon
  203. this._titleLabel.node.active = true;
  204. this._icon.node.active = true;
  205. this._img.node.active = false;
  206. this._desLabel.node.active = true;
  207. CompatibleTool.LoadRes(this._data.iconUrlList[0], (err, res) => {
  208. if (!err && cc.isValid(this) && this._icon) {
  209. this._icon.spriteFrame = new cc.SpriteFrame(res);
  210. }
  211. });
  212. } else if ((PlatUtils.IsVIVO || PlatUtils.IsHuaWei) && this._data.icon) {
  213. // 有icon
  214. this._titleLabel.node.active = true;
  215. this._icon.node.active = true;
  216. this._img.node.active = false;
  217. this._desLabel.node.active = true;
  218. CompatibleTool.LoadRes(this._data.icon, (err, res) => {
  219. if (!err && cc.isValid(this) && this._icon) {
  220. this._icon.spriteFrame = new cc.SpriteFrame(res);
  221. }
  222. });
  223. }
  224. this._hideInstallBtn.active = false;
  225. this._closeBtn.active = false;
  226. // if(this.ServerConfig.intersititia_close_delay_time == 0){
  227. // this._closeBtn.active = true;
  228. // this._hideInstallBtn.active = true;
  229. // }else{
  230. this.scheduleOnce(() => {
  231. this._closeBtn.active = true;
  232. this._hideInstallBtn.active = true;
  233. }, this.ServerConfig.intersititia_close_delay_time || 0);
  234. // }
  235. if (PlatUtils.IsHuaWei) {
  236. this._sourceLable.string = this._data.source ? this._data.source : "";
  237. }
  238. }
  239. }
  240. onBtnClickHandler(event: cc.Event, data: any) {
  241. console.log("event.target.name:", event.target.name);
  242. switch (event.target.name) {
  243. case "Btn_Close": {
  244. this._panel.active = false;
  245. this._mask.active = false;
  246. break;
  247. }
  248. case "Btn_HideInstall":
  249. case "Img":
  250. case "Btn_Install":
  251. this._reportAdClick();
  252. break;
  253. case "Btn_Download": {
  254. if (utils.ServerConfig.native_intersititial_click_range && utils.ServerConfig.native_intersititial_click_range == 2) {
  255. utils.showLog("服务器native_intersititial_click_range配置为2,只能点击安装按钮算安装");
  256. return;
  257. }
  258. this._reportAdClick();
  259. break;
  260. }
  261. }
  262. }
  263. _reportAdShow() {
  264. if (this._data) {
  265. if (this._nativeAd && this._panel.active) {
  266. YouWanAnalytics.EventAdWithObj(YwAdType.NATIVE_INTERSITITIAL, YwAdStatus.SHOW_SUCCESS, new AdEventParameter(this._data.adId));
  267. utils.showLog("上报原生插屏广告展示! adId:" + this._data.adId + " #active=" + this.node.active);
  268. this._nativeAd.reportAdShow({
  269. adId: this._data.adId
  270. });
  271. }
  272. }
  273. }
  274. _reportAdClick() {
  275. if (this._data) {
  276. utils.showLog("上报原生插屏广告点击! adId:" + this._data.adId);
  277. if (this._nativeAd) {
  278. YouWanAnalytics.EventAdWithObj(YwAdType.NATIVE_INTERSITITIAL, YwAdStatus.CLICK, new AdEventParameter(this._data.adId));
  279. this._nativeAd.reportAdClick({
  280. adId: this._data.adId
  281. });
  282. // 服务器控制点击后是否关闭插屏广告
  283. if (this.ServerConfig.intersititial_click_close ? (this.ServerConfig.intersititial_click_close == "true") : true) {
  284. this._panel.active = false;
  285. this._mask.active = false;
  286. }
  287. // 点击后调整关闭按钮大小和触发按钮相同的大小
  288. this._closeBtn.active = true;
  289. this._hideInstallBtn.active = true;
  290. this._closeBtn.setContentSize(cc.size(this.ServerConfig.intersititia_close_but_range, this.ServerConfig.intersititia_close_but_range));
  291. if (PlatUtils.IsOPPO) {
  292. utils.oppoTool.countNativeInserClick();
  293. } else if (PlatUtils.IsVIVO) {
  294. utils.Tool_Vivo.countNativeInserClick();
  295. }
  296. if (PlatUtils.IsHuaWei) {
  297. this.node.destroy();
  298. this.downLoadAd();
  299. }
  300. }
  301. }
  302. }
  303. downLoadAd() {
  304. return;
  305. const resultCode = this._nativeAd.startDownload({
  306. adId: this._data.adId
  307. })
  308. utils.showLog('原生广告主动下载 resumeDownloadresultCode = ' + resultCode);
  309. }
  310. }