GameBoxListGameItem.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { utils } from "./Utils";
  2. import PlatUtils from "./PlatUtils";
  3. import CompatibleTool from "./CompatibleTool";
  4. const { ccclass, property } = cc._decorator;
  5. @ccclass
  6. export default class GameBoxListGameItem extends cc.Component {
  7. /*
  8. {
  9. "id": 274,
  10. "icon": "http://ff.td68x.com/xcx/ylyxhz/44r4.png",
  11. "app_path": "pages/index/index?ald_media_id=5778&ald_link_key=21c462bc56b283b3",
  12. "name": "快乐游戏盒子",
  13. "qr_code": "http://ff.td68x.com/xcx/ylyxhz/44xp.png",
  14. "home_type": 4,
  15. "home_name": "休闲精选",
  16. "app_id": ""
  17. */
  18. data: any = null;
  19. icon: cc.Sprite = null;
  20. labelName: cc.Label = null;
  21. labelShadow: cc.Label = null;
  22. _dataDirty: boolean = false;
  23. lastPostTime: number = 0; //最后一次上报时间
  24. onLoad() {
  25. let mask = this.node.getChildByName("Mask");
  26. this.icon = mask.getChildByName("Icon").getComponent(cc.Sprite);
  27. this.labelName = this.node.getChildByName("Label").getComponent(cc.Label);
  28. this.labelShadow = this.node.getChildByName("LabelShadow").getComponent(cc.Label);
  29. }
  30. public init(data: any) {
  31. this.data = data;
  32. this._dataDirty = true;
  33. }
  34. update(dt: number) {
  35. if (this._dataDirty) {
  36. this._dataDirty = false;
  37. this.updateItem();
  38. }
  39. }
  40. onEnable() {
  41. this.node.on(cc.Node.EventType.TOUCH_END, (event: cc.Event) => {
  42. this._postData(this.data.app_id);
  43. if (this.data.app_id && utils.wechatTool.checkAppId(this.data.app_id)) {
  44. utils.showLog("直接跳转!", this.data.app_id);
  45. //@ts-ignore
  46. wx.navigateToMiniProgram({
  47. appId: this.data.app_id,
  48. path: this.data.app_path ? this.data.app_path : "",
  49. success(res) {
  50. },
  51. fail(res) {
  52. // utils.showLog("跳转失败!", id);
  53. utils.showLog(`跳转失败! ; res=${JSON.stringify(res)}`);
  54. }
  55. });
  56. } else if (this.data.qr_code) {
  57. if (PlatUtils.IsWechat) {
  58. utils.showLog("二维码跳转!", this.data.qr_code);
  59. utils.wechatTool.previewImage(this.data.qr_code);
  60. } else {
  61. utils.showLog("不支持二维码跳转!");
  62. }
  63. }
  64. }, this);
  65. }
  66. onDisable() {
  67. this.node.targetOff(this);
  68. }
  69. updateItem() {
  70. if (this.data) {
  71. if (this.data.name) {
  72. let gameName: string = this.data.name;
  73. if (this.data.name.length > 4) {
  74. gameName = gameName.slice(0, 4);
  75. gameName += "...";
  76. }
  77. this.labelName.string = gameName;
  78. this.labelShadow.string = gameName;
  79. }
  80. if (this.data.icon) {
  81. CompatibleTool.LoadRes(this.data.icon, (err, texture) => {
  82. if (!err && cc.isValid(this) && this.icon && this.icon.spriteFrame) {
  83. this.icon.spriteFrame = new cc.SpriteFrame(texture);
  84. }
  85. });
  86. }
  87. }
  88. }
  89. _postData(appid: string) {
  90. // let curTime: number = new Date().getTime();
  91. // let interval: number = (curTime - this.lastPostTime) / 1000;
  92. // if (interval >= 3) {
  93. let url: string = `https://apps.youlesp.com/gbs?m=rclickV2&app_id=${appid}&game_id=${utils.config.wechatconfig.appID}`;
  94. utils.showLog("上报数据, url=", url);
  95. utils.commomHttpRequest(url, (ret, data) => {
  96. if (ret) {
  97. // this.lastPostTime = curTime;
  98. utils.showLog("数据上报成功!");
  99. } else {
  100. utils.showLog("数据上报失败!");
  101. }
  102. });
  103. // } else {
  104. // cc.warn("上报的间隔时间小于3秒");
  105. // }
  106. }
  107. }