TryGameNode.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { utils } from "./Utils";
  2. import { SubLocation } from "./YZ_Constant";
  3. import PlatUtils from "./PlatUtils";
  4. import CompatibleTool from "./CompatibleTool";
  5. const { ccclass, property } = cc._decorator;
  6. @ccclass
  7. export default class TryGameNode extends cc.Component {
  8. _data: any = null;
  9. _icon: cc.Sprite = null;
  10. _nameLabel: cc.Label = null;
  11. _gameJumpInterval: number = -1;
  12. _jumpInfo: any = null;
  13. _index: number = -1;
  14. _jumping: boolean = false; // 控制是否在跳转
  15. _isFirst: boolean = false;
  16. _mask: cc.Node = null;
  17. onLoad() {
  18. this._icon = cc.find("Mask/Icon", this.node).getComponent(cc.Sprite);
  19. this._mask = cc.find("Mask", this.node);
  20. // this._nameLabel = cc.find("NameLabel", this.node).getComponent(cc.Label);
  21. // this._nameLabel.string = "";
  22. }
  23. /**
  24. *
  25. * @param data 交叉推广数据
  26. * data:{
  27. "jump_list": [
  28. { // 交叉推广挂件内容信息
  29. "icon": "http://xcx.youletd.com/img/icon/fgdxc.png",
  30. "name": "翻滚的香肠大冒险",
  31. "path": "",
  32. "js_jump": "true",
  33. "qr_code": "http://xcx.youletd.com/img/qrcode/q_fgdxc.jpg",
  34. "appid": "wx2c4ed4218224b042"
  35. }
  36. ]
  37. * }
  38. *
  39. */
  40. public init(data: any) {
  41. this._data = data;
  42. if (this._data) {
  43. this._jumpInfo = this._data.jump_list;
  44. this._gameJumpInterval = this._data.jump_refresh_time;
  45. this._isFirst = true;
  46. this.node.active = false;
  47. } else {
  48. this.node.active = false;
  49. }
  50. }
  51. /**
  52. * 跳转成功过后显示下一个
  53. */
  54. showNextItem() {
  55. this.unscheduleAllCallbacks();
  56. this.jump();
  57. this.schedule(this.jump, this._gameJumpInterval);
  58. }
  59. onEnable() {
  60. this.jump();
  61. this.schedule(this.jump, this._gameJumpInterval);
  62. this.node.on(cc.Node.EventType.TOUCH_START, (event) => {
  63. if (this._jumpInfo[this._index] && this._jumpInfo[this._index].appid) {
  64. utils.showLog(`小游戏跳转! info=${this._jumpInfo[this._index]}`);
  65. if (PlatUtils.IsDouyin) {
  66. utils.Tool_Douyin.showMoreGamesModal();
  67. } else if (PlatUtils.IsQQ) {
  68. utils.adManager.ShowAppBox(true);
  69. } else {
  70. let index: number = this._index;
  71. this._postClickData(this._jumpInfo[index].appid);
  72. utils.navigateToMiniGame(this._jumpInfo[index], (ret) => {
  73. if (ret) {
  74. // 上报数据
  75. if (this._jumpInfo && this._jumpInfo[index] && this._jumpInfo[index].appid) {
  76. this._postData(this._jumpInfo[index].appid);
  77. }
  78. }
  79. });
  80. }
  81. this.showNextItem();
  82. }
  83. }, this);
  84. }
  85. onDisable() {
  86. this.unscheduleAllCallbacks();
  87. this.node.targetOff(this);
  88. }
  89. jump() {
  90. if (this._jumping) return;
  91. this._jumping = true;
  92. this._index = this._index + 1;
  93. if (this._index >= this._jumpInfo.length) {
  94. this._index = 0;
  95. }
  96. let contentSize = this._icon.node.getContentSize();
  97. if (this._jumpInfo[this._index] && this._jumpInfo[this._index].icon) {
  98. // this._setName(this._jumpInfo[this._index].name);
  99. let remoteUrl = this._jumpInfo[this._index].icon;
  100. CompatibleTool.LoadRes(remoteUrl, (err, texture) => {
  101. if (!err && cc.isValid(this) && this._icon) {
  102. this._icon.spriteFrame = new cc.SpriteFrame(texture);
  103. this._icon.node.setContentSize(contentSize);
  104. if (this._isFirst) {
  105. utils.showLog("当前试玩第一次加载!!!!!");
  106. this._mask.active = true;
  107. this._isFirst = false;
  108. }
  109. }
  110. this._jumping = false;
  111. });
  112. } else {
  113. this._jumping = false;
  114. }
  115. }
  116. _setName(name: string) {
  117. if (!name) {
  118. this._nameLabel.string = "";
  119. } else {
  120. if (name.length > 5) {
  121. this._nameLabel.string = name.slice(0, 5) + "...";
  122. this._nameLabel.node.scale = 0.7;
  123. } else {
  124. this._nameLabel.string = name;
  125. }
  126. }
  127. }
  128. // _postData(appid: string) {
  129. // utils.postData(appid);
  130. // }
  131. /**
  132. * 上报跳转成功
  133. * @param appid
  134. */
  135. _postData(appid: string) {
  136. utils.postDataByLocation(appid, SubLocation.isTryGame, 1);
  137. }
  138. /**
  139. * 上报点击
  140. * @param appid
  141. */
  142. _postClickData(appid: string) {
  143. utils.postDataByLocation(appid, SubLocation.isTryGame, 0);
  144. }
  145. }