LoginRewardCtrl.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. const loginReward = require('../data/loginReward');
  2. const GameModule = require('../utils/GameModule');
  3. const {GameNotificationKey, WechatShareType} = require('../utils/GameEnum');
  4. const InviteApi = require('../net/InviteApi');
  5. const ArtistManager = require("../utils/ArtistManager");
  6. const TapTapTool = require("../utils/TapTapTool");
  7. const AlertManager = require('../utils/AlertManager');
  8. const WeChat = require('../net/WeChat');
  9. const DWTool = require('../utils/DWTool');
  10. const GameRedDot = require('../utils/GameEnum').GameRedDot;
  11. cc.Class({
  12. extends: cc.Component,
  13. properties: {
  14. signButton: cc.Button,
  15. signButtonSpriteFrame: [cc.SpriteFrame],
  16. shareNode: cc.Node,
  17. shareLabel: cc.Label,
  18. shareSelectedNode: cc.Node,
  19. layout: cc.Layout,
  20. itemPrefab: cc.Prefab,
  21. starSprite: cc.Sprite,
  22. starRichText: cc.RichText,
  23. },
  24. // LIFE-CYCLE CALLBACKS:
  25. onLoad () {
  26. this.shareNode.on(cc.Node.EventType.TOUCH_END, this.changeShareType, this);
  27. this.refreshButton();
  28. },
  29. start () {
  30. for (var i = 0; i < loginReward.length - 1; i++) {
  31. let itemModel = loginReward[i];
  32. let item = cc.instantiate(this.itemPrefab);
  33. this.layout.node.addChild(item);
  34. item.getComponent('LoginRewardItem').init(itemModel);
  35. if (this.reward == undefined) {
  36. if (itemModel.rewardId > GameGlobal.userLoginReward.rewardCount) {
  37. this.reward = itemModel;
  38. if (window.tt != undefined) {
  39. this.shareLabel.string = '分享领取双倍签到奖励';
  40. }
  41. }
  42. }
  43. }
  44. //
  45. let itemModel = loginReward[loginReward.length - 1];
  46. if (itemModel.starId > 0) {
  47. let imageId = 50000 + itemModel.starId;
  48. ArtistManager.loadStarAvatarSpriteFrame(imageId, this.starSprite);
  49. this.star = GameGlobal.BuildingManager.getStarInfo(itemModel.starId);
  50. this.starRichText.string = `<b><outline color=#ff6e19 width=2>累计登录7天\n即可获得${this.star.name}签约资格~</outline></b>`;
  51. if (GameGlobal.userLoginReward.rewardCount >= 6 && !GameGlobal.userLoginReward.isLoginReward) {
  52. this.shareLabel.string = '分享到群炫耀';
  53. if (window.tt != undefined) {
  54. this.shareLabel.string = '分享炫耀';
  55. }
  56. this.reward = itemModel;
  57. }
  58. }
  59. },
  60. closeNode() {
  61. GameModule.audioMng.playClickButton();
  62. this.node.destroy();
  63. },
  64. changeShareType() {
  65. this.shareSelectedNode.active = !this.shareSelectedNode.active;
  66. },
  67. signLoginReward() {
  68. GameModule.audioMng.playClickButton();
  69. this.signButton.interactable = false;
  70. //是否选择分享翻倍奖励
  71. if (this.shareSelectedNode.active) {
  72. if (CC_WECHATGAME) {
  73. GameEvent.on(GameNotificationKey.ShowShareAction, this, (type, isOk) => {
  74. if (type != WechatShareType.LoginReward) { return; }
  75. if (isOk) {
  76. this.shareActionCallback();
  77. } else {
  78. this.signButton.interactable = true;
  79. }
  80. GameEvent.off(GameNotificationKey.ShowShareAction, this);
  81. });
  82. WeChat.shareAction(WechatShareType.LoginReward, () => {
  83. }, () => {
  84. this.signButton.interactable = true;
  85. console.log('分享失败或取消');
  86. });
  87. } else {
  88. this.gainReward(1);
  89. }
  90. } else {
  91. this.gainReward(0);
  92. }
  93. },
  94. shareActionCallback() {
  95. this.gainReward(1);
  96. GameEvent.off(GameNotificationKey.ShowShareAction, this);
  97. },
  98. refreshButton() {
  99. if (GameGlobal.userLoginReward.isLoginReward) {
  100. this.signButton.interactable = false;
  101. this.signButton.getComponent(cc.Sprite).spriteFrame = this.signButtonSpriteFrame[1];
  102. } else {
  103. this.signButton.interactable = true;
  104. this.signButton.getComponent(cc.Sprite).spriteFrame = this.signButtonSpriteFrame[0];
  105. }
  106. },
  107. gainReward(isShare) {
  108. InviteApi.postLoginReward(this.reward.rewardId, isShare, (responseData) => {
  109. GameGlobal.userLoginReward.isLoginReward = true;
  110. GameGlobal.userLoginReward.rewardCount = this.reward.rewardId;
  111. TapTapTool.removeRedDot(GameRedDot.sign);
  112. this.refreshButton();
  113. var gold = TapTapTool.goldStrToClass(this.reward.coin);
  114. if (this.reward.diamond > 0) {
  115. GameModule.audioMng.playGetAward();
  116. let diamond = isShare ? this.reward.diamond * 2 : this.reward.diamond;
  117. let text = `钻石 x ${diamond}`;
  118. AlertManager.showActGiftAlert('diamond', text);
  119. GameModule.userInfo.updateUserRes({
  120. diamond: parseInt(diamond)
  121. });
  122. } else if (gold.n > 0) {
  123. GameModule.audioMng.playGetAward();
  124. gold = isShare ? TapTapTool.multiple(gold, 2) : gold;
  125. let text = `金币 x ${TapTapTool.parseToString(gold)}`;
  126. AlertManager.showActGiftAlert('coin', text);
  127. GameModule.userInfo.updateUserRes({
  128. gold: gold
  129. });
  130. } else if (this.reward.starId > 0) {
  131. this.showGetStar();
  132. }
  133. if (this.reward.rewardId >= 7) {
  134. GameEvent.fire(GameNotificationKey.LoginRewardGainFinish);
  135. } else {
  136. GameEvent.fire(GameNotificationKey.LoginRewardGainOneDay, this.reward.rewardId);
  137. }
  138. }, (error) => {
  139. this.signButton.interactable = true;
  140. GameGlobal.commonAlert.showCommonErrorAlert("签到失败");
  141. });
  142. },
  143. showGetStar() {
  144. GameModule.audioMng.playGetAward();
  145. let imageId = 50000 + this.reward.starId;
  146. let desc = "<color=#d5ba9c>签约条件</c><br/><br/><color=#540904>第七天登录 </c><img src='star_true'/>";
  147. DWTool.loadResPrefab("./prefabs/star/getStarAlert")
  148. .then((result) => {
  149. let alert = cc.instantiate(result);
  150. cc.find('Canvas').addChild(alert);
  151. alert.getComponent('StarGetStarAlert').initGetStar(imageId, `${this.star.name}`, desc);
  152. });
  153. GameEvent.fire(GameNotificationKey.RefreshSignArtistList);
  154. }
  155. // update (dt) {},
  156. });