CheckInView.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { _decorator, Button, Color, EditBox, find, instantiate, Label, Layout, Node, RichText, Sprite, SpriteFrame, tween, UIOpacity, UITransform } from 'cc';
  2. import { UIView } from '@scripts/framework/ui/UIView';
  3. import { AppKernel } from '@scripts/app/AppKernel';
  4. import { UIViewID } from '@scripts/define/UIDefine';
  5. import { UICloseReason } from '@scripts/framework/ui/UILayer';
  6. import { formatCurrency } from '@scripts/utils/Helper';
  7. import { addBtnClick } from '@scripts/utils/UIHelper';
  8. import { setSpriteFrame } from '@scripts/utils/ResHelper';
  9. const { ccclass, property } = _decorator;
  10. @ccclass('CheckInView')
  11. export class CheckInView extends UIView {
  12. readonly viewId = UIViewID.CheckInView;
  13. @property(Node)
  14. private btn_close: Node;
  15. @property(Node)
  16. private btn_viewClub: Node;
  17. @property(Sprite)
  18. private vip_icon: Sprite;
  19. @property(Sprite)
  20. private vip_title: Sprite;
  21. @property(Label)
  22. private vip_boost: Label;
  23. @property(Node)
  24. private vip_days: Node;
  25. @property(Node)
  26. private days: Node;
  27. @property(Node)
  28. private prop_template: Node;
  29. private _checkInData: any;
  30. protected async onBeforeOpen(_params: unknown): Promise<boolean> {
  31. const checkInStore = AppKernel.stores.checkInStore;
  32. const res = await checkInStore.reqCheckInInfo();
  33. return res;
  34. }
  35. protected onCreate(): void | Promise<void> {
  36. AppKernel.events.on('checkIn:update', this.updateCheckInView, this);
  37. addBtnClick(this.btn_close, this.onBtnCloseClick, this);
  38. addBtnClick(this.btn_viewClub, this.onBtnViewClubClick, this);
  39. this.days.children.forEach((day, index) => {
  40. addBtnClick(day, () => this.onDayClick(index), this);
  41. })
  42. }
  43. protected onBeforeClose(_reason: UICloseReason): boolean | Promise<boolean> {
  44. AppKernel.events.off('checkIn:update', this.updateCheckInView, this);
  45. return true;
  46. }
  47. private updateCheckInView() {
  48. this._checkInData = AppKernel.stores.checkInStore.checkInData;
  49. const { vip, days } = this._checkInData;
  50. this.updateDays(days);
  51. this.updateVipInfo(vip);
  52. }
  53. private updateDays(daysData = []) {
  54. daysData?.forEach((dayData, index) => {
  55. const { status, final_reward, vip_boost } = dayData;
  56. const day = this.days.children[index];
  57. if (!day) return;
  58. const bg_normal = day.getChildByName('bg_normal');
  59. const bg_current = day.getChildByName('bg_current');
  60. const lock = day.getChildByName('lock');
  61. const title = day.getChildByName('day');
  62. const rewards = day.getChildByName('rewards');
  63. const rewards_layout = rewards.getComponent(Layout);
  64. const light = day.getChildByName('light');
  65. const mask = day.getChildByName('mask');
  66. const signedIn = day.getChildByName('signedIn');
  67. const isClaimable = status == "claimable";
  68. const isClaimed = status == "claimed";
  69. const isLocked = status == "locked";
  70. const isLastDay = index == 6;
  71. if (isLastDay) {// 最后一天
  72. lock.active = isLocked ? true : false;
  73. light.active = isClaimable ? true : false;
  74. mask.active = isClaimed ? true : false;
  75. signedIn.active = isClaimed ? true : false;
  76. rewards.active = true;
  77. title.active = true;
  78. } else {
  79. bg_normal.active = isClaimable ? false : true;
  80. bg_current.active = isClaimable ? true : false;
  81. lock.active = isLocked ? true : false;
  82. title.active = true;
  83. rewards.active = true;
  84. light.active = isClaimable ? true : false;
  85. mask.active = isClaimed ? true : false;
  86. signedIn.active = isClaimed ? true : false;
  87. }
  88. this.stopLightBlink(light);
  89. if (isClaimable) this.startLightBlink(light);
  90. const rewardsNums = Object.keys(final_reward).length;
  91. for (const [key, val] of Object.entries(final_reward)) {
  92. let find_key = key;
  93. const temp = find(`${find_key}_${isLastDay ? "l" : "s"}`, this.prop_template);
  94. if (!temp) continue;
  95. const exist_reward = rewards.getChildByName(temp.name);
  96. const reward = !!exist_reward ? exist_reward : instantiate(temp);
  97. if (!!!exist_reward) {
  98. reward.parent = rewards;
  99. reward.active = true;
  100. }
  101. const num = reward.getChildByName('num');
  102. num.getComponent(Label).string = `${(Number(val) / 100).toFixed(2)}`;
  103. if (!isLastDay) rewards_layout.spacingX = rewardsNums > 2 ? 5 : 40;
  104. }
  105. const isTodayBoost = vip_boost?.enabled && vip_boost?.has_boost;
  106. this.vip_days.children[index].active = isTodayBoost;
  107. })
  108. }
  109. private updateVipInfo(vip) {
  110. const { vip_level, daily_bonus_boost_percent } = vip;
  111. setSpriteFrame(this.vip_icon, "textures/signIn/vipIcon/" + vip_level);
  112. setSpriteFrame(this.vip_title, "textures/signIn/vipTitle/" + vip_level);
  113. this.vip_boost.string = `+${daily_bonus_boost_percent - 100}% BOOST`;
  114. }
  115. private onBtnCloseClick() {
  116. AppKernel.ui.close(UIViewID.CheckInView);
  117. }
  118. startLightBlink(lightNode: Node) {
  119. const uiOpacity = lightNode.getComponent(UIOpacity) || lightNode.addComponent(UIOpacity);
  120. // 先重置初始透明度
  121. uiOpacity.opacity = 255;
  122. tween(uiOpacity)
  123. .to(0.6, { opacity: 0 })
  124. .to(0.6, { opacity: 255 })
  125. .union()
  126. .repeatForever() // 无限循环闪烁
  127. .start();
  128. }
  129. // 停止闪烁
  130. stopLightBlink(lightNode: Node) {
  131. const uiOpacity = lightNode.getComponent(UIOpacity);
  132. if (!uiOpacity) return;
  133. tween(uiOpacity).stop();
  134. uiOpacity.opacity = 255; // 恢复不透明
  135. }
  136. async onDayClick(index: number) {
  137. const { final_reward: reward, status } = this._checkInData.days[index];
  138. console.log('onDayClick', index, 'reward', reward, 'status', status);
  139. if (!reward || status != "claimable") return;
  140. const checkInStore = AppKernel.stores.checkInStore;
  141. const res = await checkInStore.reqCheckIn(index + 1);
  142. if (res) AppKernel.ui.open(UIViewID.RewardView, { reward });
  143. }
  144. private onBtnViewClubClick() {
  145. }
  146. }