GameItem.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { utils } from "./Utils";
  2. import PlatUtils from "./PlatUtils";
  3. import { SubLocation } from "./YZ_Constant";
  4. import CompatibleTool from "./CompatibleTool";
  5. const { ccclass, property } = cc._decorator;
  6. @ccclass
  7. export default class GameItem extends cc.Component {
  8. /*
  9. {
  10. "icon": "http://ff.td68x.com/xcx/ylyxhz/451m.png",
  11. "name": "点亮它的色彩",
  12. "path": "",
  13. "qr_code": "",
  14. "appid": "wx35562d816d9ed9fb"
  15. }
  16. */
  17. data: any = null;
  18. icon: cc.Sprite = null;
  19. labelName: cc.Label = null;
  20. labelShadow: cc.Label = null;
  21. _dataDirty: boolean = false;
  22. _redPoint: cc.Node = null;
  23. public _location: SubLocation = null;
  24. mask: cc.Node = null;
  25. _subNameLength: number = 4;
  26. onLoad() {
  27. this.mask = this.node.getChildByName("Mask");
  28. this.icon = this.mask.getChildByName("Icon").getComponent(cc.Sprite);
  29. this.labelName = this.node.getChildByName("Label").getComponent(cc.Label);
  30. this.labelShadow = this.node.getChildByName("LabelShadow").getComponent(cc.Label);
  31. if (this._location && (this._location == SubLocation.isYzBanner || this._location == SubLocation.isScrollbar || this._location == SubLocation.isMoreGame || this._location == SubLocation.isBoxInsertAd || this._location == SubLocation.isBeforGameOverAd)) {
  32. this._redPoint = this.mask.getChildByName("redpoint");
  33. if (this.data && this.data.red && this.data.red != "0") {
  34. if (this.data.red == "1") {
  35. this._redPoint.getComponent("YZ_ActionScale").isRunAction = false;
  36. }
  37. } else {
  38. this._redPoint && this._redPoint.destroy();
  39. }
  40. }
  41. }
  42. public init(data: any, location: SubLocation) {
  43. this.data = data;
  44. this._dataDirty = true;
  45. this._location = location;
  46. let checkSubString = [SubLocation.isBoxInsertAd, SubLocation.isBeforGameOverAd, SubLocation.isVerticalPanel]
  47. if (checkSubString.indexOf(this._location) > -1) {
  48. this._subNameLength = 6;
  49. } else if (this._location == SubLocation.isMoreGame) {
  50. this._subNameLength = 8;
  51. }
  52. }
  53. update(dt: number) {
  54. if (this._dataDirty) {
  55. this._dataDirty = false;
  56. this.updateItem();
  57. }
  58. }
  59. onEnable() {
  60. if (!this.data) return;
  61. this.node.on(cc.Node.EventType.TOUCH_END, (event: cc.Event) => {
  62. if (PlatUtils.IsDouyin) {
  63. utils.Tool_Douyin.showMoreGamesModal();
  64. return;
  65. }
  66. this._postClickData(this.data.appid);
  67. if (this.data.is_jump && this.data.is_jump == "true" && this.data.appid) {
  68. utils.showLog("直接跳转!", this.data.appid);
  69. utils.navigateToMiniGame(this.data, (ret) => {
  70. if (ret) {
  71. // 上报数据
  72. if (this.data && this.data.appid) {
  73. this._postData(this.data.appid);
  74. }
  75. }
  76. });
  77. } else if (this.data.is_jump && this.data.is_jump == "false" && this.data.qr_code) {
  78. if (PlatUtils.IsWechat) {
  79. utils.showLog("二维码跳转!", this.data.qr_code);
  80. utils.wechatTool.previewImage(this.data.qr_code);
  81. // 上报数据
  82. if (this.data && this.data.appid) {
  83. this._postData(this.data.appid);
  84. }
  85. } else {
  86. utils.showLog("不支持二维码跳转!");
  87. }
  88. } else {
  89. utils.showLog("没有is_jump直接跳转!", this.data.appid);
  90. if (this.data.appid) {
  91. utils.navigateToMiniGame(this.data, (ret) => {
  92. if (ret) {
  93. // 上报数据
  94. if (this.data.appid) {
  95. this._postData(this.data.appid);
  96. }
  97. }
  98. });
  99. }
  100. }
  101. }, this);
  102. }
  103. onDisable() {
  104. this.node.targetOff(this);
  105. }
  106. updateItem() {
  107. if (this.data) {
  108. if (this.data.name) {
  109. let gameName: string = this.data.name;
  110. if (this.data.name.length > this._subNameLength) {
  111. gameName = gameName.slice(0, this._subNameLength);
  112. gameName += "...";
  113. }
  114. this.labelName.string = gameName;
  115. this.labelShadow.string = gameName;
  116. }
  117. if (this.data.icon) {
  118. CompatibleTool.LoadRes(this.data.icon, (err, texture) => {
  119. if (!err && cc.isValid(this) && this.icon && this.icon.spriteFrame) {
  120. this.icon.spriteFrame = new cc.SpriteFrame(texture);
  121. }
  122. });
  123. }
  124. } else {
  125. this.mask.active = false;
  126. }
  127. }
  128. /**
  129. * 上报跳转成功
  130. * @param appid
  131. */
  132. _postData(appid: string) {
  133. utils.postDataByLocation(appid, this._location, 1);
  134. }
  135. /**
  136. * 上报点击
  137. * @param appid
  138. */
  139. _postClickData(appid: string) {
  140. utils.postDataByLocation(appid, this._location, 0);
  141. }
  142. }