QCrossWidgetItem.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 QCrossWidgetItem extends cc.Component {
  8. /*
  9. {
  10. "name": "翻滚的香肠大冒险",
  11. "appid": "wx2c4ed4218224b042",
  12. "icon": "https://xcx.youletd.com/img/icon/fgdxc.png",
  13. "logo": "https://xcx.youletd.com/img/logo/4.png",
  14. "is_jump": "true",
  15. "path": "",
  16. "qr_code": "https://xcx.youletd.com/img/qrcode/q_fgdxc.jpg"
  17. }
  18. */
  19. data: any = null;
  20. private _sprite: cc.Sprite = null;
  21. private _dataDirty: boolean = false;
  22. public _isReward: boolean = false;
  23. public _location: SubLocation = null;
  24. onLoad() {
  25. this._sprite = this.node.getComponent(cc.Sprite);
  26. }
  27. public init(data: any) {
  28. this.data = data;
  29. this._dataDirty = true;
  30. }
  31. update(dt: number) {
  32. if (this._dataDirty) {
  33. this._dataDirty = false;
  34. this.updateItem();
  35. }
  36. }
  37. onEnable() {
  38. this.node.on(cc.Node.EventType.TOUCH_END, this._onItemClickHandler, this);
  39. }
  40. onDisable() {
  41. this.node.targetOff(this);
  42. }
  43. private _onItemClickHandler() {
  44. console.log("_onItemClickHandler");
  45. if (PlatUtils.IsDouyin) {
  46. utils.Tool_Douyin.showMoreGamesModal();
  47. return;
  48. }
  49. if (this.data && this.data.appid) {
  50. this._postClickData(this.data.appid);
  51. }
  52. //如果是激励模式就直接跳转
  53. if (this._location == SubLocation.isReward) {
  54. utils.navigateToMiniGame(this.data, (ret) => {
  55. if (ret) {
  56. // 上报数据
  57. if (this.data && this.data.appid) {
  58. this._postData(this.data.appid);
  59. }
  60. utils.showLog("激励插屏跳转成功!下发奖励!");
  61. utils.adManager.videoCallBack && utils.adManager.videoCallBack(ret);
  62. utils.adManager.videoCallBack = null;
  63. } else {
  64. utils.showLog("激励插屏跳转失败!");
  65. utils.adManager.videoCallBack && utils.adManager.videoCallBack(false, "获取试玩奖励失败!");
  66. }
  67. utils.adManager.videoCallBack = null;
  68. utils.adManager.hideRewardInsert();
  69. });
  70. return;
  71. }
  72. if (this.data.is_jump && this.data.is_jump == "true" && this.data.appid) {
  73. utils.showLog("直接跳转!", this.data.appid);
  74. utils.navigateToMiniGame(this.data, (ret) => {
  75. if (ret) {
  76. // 上报数据
  77. if (this.data && this.data.appid) {
  78. this._postData(this.data.appid);
  79. }
  80. }
  81. });
  82. } else if (this.data.is_jump && this.data.is_jump == "false" && this.data.qr_code) {
  83. if (PlatUtils.IsWechat) {
  84. utils.showLog("二维码跳转!", this.data.qr_code);
  85. utils.wechatTool.previewImage(this.data.qr_code);
  86. // // 上报数据
  87. // if (this.data && this.data.appid) {
  88. // this._postData(this.data.appid);
  89. // }
  90. } else {
  91. utils.showLog("不支持二维码跳转!");
  92. }
  93. } else {
  94. utils.showLog("没有is_jump直接跳转!", this.data.appid);
  95. if (this.data.appid) {
  96. utils.navigateToMiniGame(this.data, (ret) => {
  97. if (ret) {
  98. // 上报数据
  99. if (this.data.appid) {
  100. this._postData(this.data.appid);
  101. }
  102. }
  103. });
  104. }
  105. }
  106. }
  107. updateItem() {
  108. if (this.data && this.data.logo) {
  109. let temp = cc.loader.getRes(this.data.logo);
  110. if (temp) {
  111. if (cc.isValid(this) && this._sprite) {
  112. let size: cc.Size = this.node.getContentSize();
  113. this._sprite.spriteFrame = new cc.SpriteFrame(temp);
  114. this.node.setContentSize(size);
  115. }
  116. } else {
  117. CompatibleTool.LoadRes(this.data.logo, (err, texture) => {
  118. if (!err && cc.isValid(this) && this._sprite) {
  119. let size: cc.Size = this.node.getContentSize();
  120. this._sprite.spriteFrame = new cc.SpriteFrame(texture);
  121. this.node.setContentSize(size);
  122. }
  123. });
  124. }
  125. }
  126. }
  127. /**
  128. * 上报跳转成功
  129. * @param appid
  130. */
  131. _postData(appid: string) {
  132. utils.postDataByLocation(appid, this._location, 1);
  133. }
  134. /**
  135. * 上报点击
  136. * @param appid
  137. */
  138. _postClickData(appid: string) {
  139. utils.postDataByLocation(appid, this._location, 0);
  140. }
  141. }