GameBox.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import GameBoxSlideItem from "./GameBoxSlideItem";
  2. import { utils } from "./Utils";
  3. import GameBoxListItem from "./GameBoxListItem";
  4. const { ccclass, property } = cc._decorator;
  5. /**
  6. * 游戏盒子
  7. */
  8. @ccclass
  9. export default class GameBox extends cc.Component {
  10. // onLoad () {}
  11. @property({ type: cc.PageView, tooltip: "顶部滚动视图" })
  12. slidePageView: cc.PageView = null;
  13. @property({ type: cc.Node, tooltip: "推荐列表" })
  14. listContent: cc.Node = null;
  15. @property({ type: cc.Node, tooltip: "顶部的幻灯片节点" })
  16. topSlideNode: cc.Node = null;
  17. @property({ type: cc.Node, tooltip: "推荐列表节点" })
  18. recommentNode: cc.Node = null;
  19. @property({ type: cc.Node, tooltip: "推荐列表Banner节点" })
  20. recommentBanner: cc.Node = null;
  21. _boxInfo: any = null;
  22. _dataDirty: boolean = false;
  23. _slideList: GameBoxSlideItem[] = null;
  24. _appIdList: string[] = [];
  25. onLoad() {
  26. this.initListener();
  27. }
  28. onEnable() {
  29. this.postData();
  30. utils.registerServerInitEvent(() => {
  31. this._setNodeVisible();
  32. }, this);
  33. }
  34. onDisable() {
  35. utils.unregisterServerInitEvent(this);
  36. }
  37. _setNodeVisible() {
  38. this.initData();
  39. if (this._boxInfo) {
  40. this.initUi();
  41. } else {
  42. this.node.destroy();
  43. }
  44. }
  45. /**
  46. * 初始化UI
  47. */
  48. protected initUi(): void {
  49. this.slidePageView.removeAllPages();
  50. this.listContent.removeAllChildren();
  51. utils.showLog("gamebox initUi")
  52. if (!this._boxInfo) return;
  53. let slideData: any = this._boxInfo.banners;
  54. let listData: any = this._boxInfo.infos
  55. for (let i = 0; i < slideData.length; i++) {
  56. let slideNode: cc.Node = cc.instantiate(this.topSlideNode);
  57. let slideItem: GameBoxSlideItem = slideNode.getComponent("GameBoxSlideItem");
  58. slideNode.active = true;
  59. slideItem.initData(slideData[i]);
  60. this.slidePageView.addPage(slideNode);
  61. }
  62. for (let i = 0; i < listData.length; i++) {
  63. let listNode: cc.Node = cc.instantiate(this.recommentNode);
  64. let listItem: GameBoxListItem = listNode.getComponent("GameBoxListItem");
  65. let data = listData[i];
  66. listItem.init(data)
  67. this.listContent.addChild(listNode);
  68. if (data.banner) {
  69. let banner: cc.Node = cc.instantiate(this.recommentBanner);
  70. let slideItem: GameBoxSlideItem = banner.getComponent("GameBoxSlideItem");
  71. banner.active = true;
  72. slideItem.initData(data.banner);
  73. this.listContent.addChild(banner);
  74. }
  75. }
  76. this.startAutoChange();
  77. }
  78. /**
  79. * 开启轮播
  80. */
  81. startAutoChange() {
  82. this.schedule(() => {
  83. let count = this.slidePageView.getPages().length;
  84. let index = this.slidePageView.getCurrentPageIndex();
  85. index = ((index < count) && (index + 1 !== count)) ? (index + 1) : 0;
  86. this.slidePageView.scrollToPage(index, 2);
  87. }, 3); //3秒一换
  88. }
  89. /**
  90. * 初始化监听事件
  91. */
  92. newPage: cc.Node = null;
  93. protected initListener(): void {
  94. // var _pool = new cc.NodePool();
  95. // this.slidePageView.node.on('scroll-began', () => {
  96. // if (this.slidePageView.getCurrentPageIndex() == this.slidePageView.content.childrenCount - 1) {
  97. // if (!this.newPage) {
  98. // this.newPage = cc.instantiate(this.slidePageView.getPages()[0]);
  99. // }
  100. // this.slidePageView.addPage(this.newPage);
  101. // }
  102. // });
  103. // this.slidePageView.node.on('scroll-ended', () => {
  104. // if (this.slidePageView.getPages().length == 4) {
  105. // _pool.put(this.slidePageView.content.children[1]);
  106. // this.newPage = _pool.get();
  107. // this.slidePageView.getPages().splice(1, 1);
  108. // }
  109. // })
  110. }
  111. /**
  112. * 初始化数据
  113. * @param {boxInfo} 游戏盒子的json数据
  114. */
  115. public initData(): void {
  116. utils.showLog("gamebox initData")
  117. this._boxInfo = utils._wechatTool.gameBoxServerConfig;
  118. if (this._boxInfo) {
  119. utils.showLog("Gamebox 游戏盒子数据:", JSON.stringify(this._boxInfo));
  120. } else {
  121. utils.showLog("Gamebox 游戏盒子数据获取失败!", this._boxInfo);
  122. }
  123. }
  124. postData() {
  125. let url: string = `https://apps.youlesp.com/gbs?m=rclickV2&app_id=100000000001&game_id=${utils.config.wechatconfig.appID}`;
  126. utils.showLog("上报数据, url=", url);
  127. utils.commomHttpRequest(url, (ret, data) => {
  128. if (ret) {
  129. utils.showLog("数据上报成功!");
  130. } else {
  131. utils.showLog("数据上报失败!");
  132. }
  133. });
  134. }
  135. update(dt) {
  136. if (this._dataDirty) {
  137. this._dataDirty = false;
  138. this.node.active = true;
  139. }
  140. }
  141. }