GameBoxSlideItem.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { utils } from "./Utils";
  2. import PlatUtils from "./PlatUtils";
  3. import CompatibleTool from "./CompatibleTool";
  4. const { ccclass, property } = cc._decorator;
  5. /**
  6. * 游戏盒子顶部的幻灯片
  7. */
  8. @ccclass
  9. export default class GameBoxSlideItem extends cc.Component {
  10. data: any = null;
  11. itemSprite: cc.Sprite = null;
  12. _dataDirty: boolean = false;
  13. lastPostTime: number = 0; //最后一次上报时间
  14. onLoad() {
  15. this.itemSprite = this.node.getComponent(cc.Sprite);
  16. }
  17. // start() {
  18. // this.initUi();
  19. // this.initListener();
  20. // }
  21. /**
  22. * 初始化UI
  23. */
  24. protected initUi(): void {
  25. }
  26. /**
  27. * 初始化监听事件
  28. */
  29. protected initListener(): void {
  30. }
  31. /**
  32. * 初始化数据
  33. */
  34. public initData(data: string): void {
  35. utils.showLog("#slideData=", data)
  36. this.data = data;
  37. this._dataDirty = true;
  38. }
  39. update(dt: number) {
  40. if (this._dataDirty) {
  41. this._dataDirty = false;
  42. this.updateItem();
  43. }
  44. }
  45. onEnable() {
  46. this.node.on(cc.Node.EventType.TOUCH_END, (event: cc.Event) => {
  47. this._postData(this.data.app_id);
  48. if (this.data.app_id && utils.wechatTool.checkAppId(this.data.app_id)) {
  49. utils.showLog("直接跳转!", this.data.app_id);
  50. //@ts-ignore
  51. wx.navigateToMiniProgram({
  52. appId: this.data.app_id,
  53. path: this.data.app_path ? this.data.app_path : "",
  54. success(res) {
  55. },
  56. fail(res) {
  57. // utils.showLog("跳转失败!", id);
  58. utils.showLog(`跳转失败! res=${JSON.stringify(res)}`);
  59. }
  60. });
  61. } else if (this.data.qr_code) {
  62. if (PlatUtils.IsWechat) {
  63. utils.showLog("二维码跳转!", this.data.qr_code);
  64. utils.wechatTool.previewImage(this.data.qr_code);
  65. // 上报数据
  66. // if (this.data && this.data.app_id) {
  67. // this._postData(this.data.app_id);
  68. // }
  69. } else {
  70. utils.showLog("不支持二维码跳转!");
  71. }
  72. }
  73. }, this);
  74. }
  75. onDisable() {
  76. this.node.targetOff(this);
  77. }
  78. updateItem() {
  79. if (this.data && this.data.banner) {
  80. CompatibleTool.LoadRes(this.data.banner, (err, texture) => {
  81. utils.showLog(err, "err")
  82. if (!err && cc.isValid(this) && this.itemSprite) {
  83. utils.showLog(this.node.active, "node <<<")
  84. this.itemSprite.spriteFrame = new cc.SpriteFrame(texture);
  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. // update (dt) {}
  108. }