YZ_NativeSplashView.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import CompatibleTool from "./CompatibleTool";
  2. import PlatUtils from "./PlatUtils";
  3. import { utils } from "./Utils";
  4. const { ccclass, property } = cc._decorator;
  5. @ccclass
  6. export default class YZ_NativeSplashView extends cc.Component {
  7. _panel: cc.Node = null;
  8. _sourceLable: cc.Label = null;
  9. _titleLabel: cc.Label = null;
  10. _desLabel: cc.Label = null;
  11. _icon: cc.Sprite = null;
  12. // _mask: cc.Node = null;
  13. _btnLabel: cc.Label = null;
  14. _img: cc.Sprite = null;
  15. _timeLabel: cc.Label = null;
  16. _nativeAd: any = null;
  17. _data: any = null;
  18. _dataDirty: boolean = false;
  19. public get ServerConfig() {
  20. return utils.ServerConfig;
  21. }
  22. bgTexture: cc.SpriteFrame;
  23. btnTexture: cc.SpriteFrame;
  24. _btnDownLoad: cc.Node = null;
  25. ratio: number = 1;
  26. canSkip: boolean = true; //直接跳过
  27. onLoad() {
  28. if (utils.otherConfig && utils.otherConfig.group) {
  29. this.node.group = utils.otherConfig.group;
  30. }
  31. if (cc.winSize.height < cc.winSize.width) {
  32. // 横屏游戏
  33. this.ratio = cc.winSize.width / 1920 * 0.75;
  34. this._panel = this.node.getChildByName("LandscapePanel");
  35. } else {
  36. this.ratio = cc.winSize.width / 1080;
  37. this._panel = this.node.getChildByName("PortraitPanel");
  38. }
  39. this._panel.active = true;
  40. // cc.game.addPersistRootNode(this.node);
  41. let canSkip = utils.getConfigByKey("native_splash_can_skip");
  42. if (canSkip == "false") {
  43. this.canSkip = false;
  44. }
  45. // this._panel = this.node.getChildByName("Panel");
  46. this._panel.scale = this.ratio;
  47. this._icon = this._panel.getChildByName("Icon").getComponent(cc.Sprite);
  48. this._img = this._panel.getChildByName("Img").getComponent(cc.Sprite);
  49. this._titleLabel = this._panel.getChildByName("TitleLabel").getComponent(cc.Label);
  50. this._desLabel = this._panel.getChildByName("DesLabel").getComponent(cc.Label);
  51. // this._mask = this.node.getChildByName("Mask");
  52. this._sourceLable = this._img.node.getChildByName("source").getComponent(cc.Label);
  53. this._btnDownLoad = this._panel.getChildByName("Btn_Download");
  54. this._btnLabel = this._btnDownLoad.getComponentInChildren(cc.Label);
  55. this._timeLabel = this._panel.getChildByName("time").children[0].getComponent(cc.Label);
  56. if (PlatUtils.IsHuaWei) {
  57. //@ts-ignore
  58. qg.onShow(this._reportAdShow.bind(this));
  59. }
  60. if (utils.getConfigByKey("native_splash_full_cilck") == "true") {
  61. this._btnDownLoad.active = true;
  62. }
  63. // this._panel.scale = ratio;
  64. }
  65. onDestroy() {
  66. if (PlatUtils.IsHuaWei) {
  67. //@ts-ignore
  68. qg.offShow(this._reportAdShow)
  69. this._nativeAd.destroy();
  70. cc.director.emit("SplashViewOff");
  71. }
  72. }
  73. init(nativeAd: any, data: any) {
  74. this._nativeAd = nativeAd;
  75. this._data = data;
  76. this._dataDirty = true;
  77. }
  78. update(dt: number) {
  79. if (this._data && this._dataDirty) {
  80. this._panel.active = true;
  81. // this._mask.active = true;
  82. this._dataDirty = false;
  83. this._updateContent();
  84. }
  85. if (!this._data) {
  86. this._panel.active = false;
  87. // this._mask.active = false;
  88. }
  89. }
  90. _updateContent() {
  91. if (this._data) {
  92. // 上报原生插屏广告
  93. this._reportAdShow();
  94. if (PlatUtils.IsHuaWei) {
  95. this._data.icon = "";
  96. }
  97. // utils.nativeInsertShowCount++;
  98. utils.showLog("data:", JSON.stringify(this._data));
  99. this._titleLabel.string = this._data.title ? this._data.title : "";
  100. this._desLabel.string = this._data.desc ? this._data.desc : "";
  101. this._panel.getChildByName("Btn_Install").getComponentInChildren(cc.Label).string = this._data.clickBtnTxt ? this._data.clickBtnTxt : "查看详情";
  102. // this._btnLabel.string = this._data.clickBtnTxt ? this._data.clickBtnTxt : "查看详情";
  103. // utils.showLog("this._btnLabel.string" + this._btnLabel.string);
  104. if (this._data.imgUrlList && this._data.imgUrlList.length > 0) {
  105. // 有图片,优先显示图片
  106. this._titleLabel.node.active = true;
  107. this._icon.node.active = false;
  108. this._img.node.active = true;
  109. this._desLabel.node.active = true;
  110. let t: number = 5;
  111. let task = setInterval(() => {
  112. t--;
  113. if (t == 0) {
  114. this._timeLabel.string = `跳过`;
  115. } else if (t == -1) {
  116. clearInterval(task);
  117. this.node.destroy();
  118. } else {
  119. this._timeLabel.string = `点击跳过${t}s`;
  120. }
  121. }, 1000)
  122. utils.showLog("有图片,优先显示图片");
  123. CompatibleTool.LoadRes(this._data.imgUrlList[0], (err, res) => {
  124. utils.showLog("加载图片信息 ...." + err);
  125. if (!err && cc.isValid(this) && this._img) {
  126. this._img.spriteFrame = new cc.SpriteFrame(res);
  127. }
  128. });
  129. } else if (PlatUtils.IsHuaWei && this._data.icon) {
  130. // 有icon
  131. utils.showLog("没有图片,显示icon");
  132. this._titleLabel.node.active = true;
  133. this._icon.node.active = true;
  134. this._img.node.active = false;
  135. this._desLabel.node.active = true;
  136. CompatibleTool.LoadRes(this._data.icon, (err, res) => {
  137. if (!err && cc.isValid(this) && this._icon) {
  138. this._icon.spriteFrame = new cc.SpriteFrame(res);
  139. }
  140. });
  141. }
  142. else {
  143. utils.showLog("什么都没有!!!")
  144. }
  145. if (PlatUtils.IsHuaWei) {
  146. this._sourceLable.string = this._data.source ? this._data.source : "";
  147. }
  148. }
  149. }
  150. onBtnClickHandler(event: cc.Event, data: any) {
  151. switch (event.target.name) {
  152. case "time":
  153. if (this.canSkip) {
  154. this.node.destroy();
  155. } else {
  156. if (this._timeLabel.string == "跳过") {
  157. this.node.destroy();
  158. }
  159. }
  160. // this._panel.active = false;
  161. // this._mask.active = false;
  162. break;
  163. case "Btn_Close": {
  164. this.node.destroy();
  165. // this._panel.active = false;
  166. // this._mask.active = false;
  167. break;
  168. }
  169. case "Img":
  170. case "Icon":
  171. case "Btn_Install":
  172. this._reportAdClick();
  173. break;
  174. case "Btn_Download": {
  175. this._reportAdClick();
  176. break;
  177. }
  178. }
  179. }
  180. _reportAdShow() {
  181. if (this._data) {
  182. if (this._nativeAd && this._panel.active) {
  183. utils.showLog("上报原生插屏广告展示! adId:" + this._data.adId + " #active=" + this.node.active);
  184. this._nativeAd.reportAdShow({
  185. adId: this._data.adId
  186. });
  187. }
  188. }
  189. }
  190. _reportAdClick() {
  191. if (this._data) {
  192. utils.showLog("上报原生开屏广告点击! adId:" + this._data.adId);
  193. if (this._nativeAd) {
  194. this._nativeAd.reportAdClick({
  195. adId: this._data.adId
  196. });
  197. this._panel.active = false;
  198. // this._mask.active = false;
  199. if (PlatUtils.IsOPPO) {
  200. utils.oppoTool.countNativeInserClick();
  201. } else if (PlatUtils.IsVIVO) {
  202. utils.Tool_Vivo.countNativeInserClick();
  203. }
  204. if (PlatUtils.IsHuaWei) {
  205. this.node.destroy();
  206. this.downLoadAd();
  207. }
  208. }
  209. }
  210. }
  211. downLoadAd() {
  212. return;
  213. const resultCode = this._nativeAd.startDownload({
  214. adId: this._data.adId
  215. })
  216. utils.showLog('原生广告主动下载 resumeDownloadresultCode = ' + resultCode);
  217. }
  218. }