RewardItem.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { AppKernel } from '@scripts/app/AppKernel';
  2. import { UIViewID } from '@scripts/define/UIDefine';
  3. import { UIView } from '@scripts/framework/ui/UIView';
  4. import { formatMailTimeToMMDDYYYY, formatNumShort, parseMailRewardStr } from '@scripts/utils/Helper';
  5. import { setOriginalSp, setSpriteFrame } from '@scripts/utils/ResHelper';
  6. import { addBtnClick, removeBtnClick } from '@scripts/utils/UIHelper';
  7. import { _decorator, Button, Component, Enum, instantiate, Label, Node, NodePool, Prefab, ProgressBar, Sprite, tween, UIOpacity, UITransform } from 'cc';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('RewardItem')
  10. export class RewardItem extends Component {
  11. @property(Node)
  12. private bg: Node = null;
  13. @property(Node)
  14. private bg_canClaim: Node = null;
  15. @property(Label)
  16. private title: Label = null;
  17. @property(Node)
  18. private rewards: Node = null;
  19. @property(Node)
  20. private light: Node = null;
  21. @property(Node)
  22. private lock: Node = null;
  23. @property(Node)
  24. private claimed: Node = null;
  25. private _data: any = {};
  26. private _claiming: boolean = false;
  27. onLoad() {
  28. // 只绑定一次!
  29. addBtnClick(this.node, this.handleClaim, this);
  30. }
  31. initItem(data: any = {}) {
  32. this._data = data;
  33. const { day, purchase_times, status, reward } = this._data;
  34. this.title.string = `C${purchase_times} . DAY${day}`;
  35. const claimable = status == "claimable";
  36. const locked = status == "locked";
  37. const claimed = status == "claimed";
  38. const unclaimed = status == "unclaimed";
  39. this.bg.active = !claimable;
  40. this.bg_canClaim.active = claimable;
  41. this.light.active = claimable;
  42. this.stopLightBlink(this.light);
  43. if (claimable) this.startLightBlink(this.light);
  44. this.lock.active = locked;
  45. this.claimed.active = claimed;
  46. this.rewards?.children?.forEach(item => item.active = false);
  47. for (const [key, val] of Object.entries(reward)) {
  48. const propKey = `prop_${key}`;
  49. const item = this.rewards.getChildByName(propKey);
  50. if (!item || val == 0) continue;
  51. item.active = true;
  52. const num = item.getChildByName('num');
  53. const label = num.getComponent(Label);
  54. if (key == "sc" || key == "gc") {
  55. // label.string = `${(Number(val) / 100).toFixed(2)}`;
  56. label.string = formatNumShort(String(val), 999);
  57. } else {
  58. // label.string = `${val}`;
  59. label.string = formatNumShort(String(val), 999, 1);
  60. }
  61. }
  62. }
  63. async handleClaim() {
  64. if (this._claiming) return;
  65. this._claiming = true;
  66. const { day, subscription_id } = this._data;
  67. const payload = {
  68. day,
  69. subscription_id
  70. }
  71. try {
  72. const res = await AppKernel.stores.weeklyPassStore.reqWeeklyPassClaim(payload) as any;
  73. console.log("reqWeeklyPassClaim", res);
  74. if (res) AppKernel.ui.open(UIViewID.WeeklyPassClaim, { data: res });
  75. } finally {
  76. this._claiming = false;
  77. }
  78. }
  79. startLightBlink(lightNode: Node) {
  80. const uiOpacity = lightNode.getComponent(UIOpacity) || lightNode.addComponent(UIOpacity);
  81. // 先重置初始透明度
  82. uiOpacity.opacity = 255;
  83. tween(uiOpacity)
  84. .to(0.6, { opacity: 0 })
  85. .to(0.6, { opacity: 255 })
  86. .union()
  87. .repeatForever() // 无限循环闪烁
  88. .start();
  89. }
  90. // 停止闪烁
  91. stopLightBlink(lightNode: Node) {
  92. const uiOpacity = lightNode.getComponent(UIOpacity);
  93. if (!uiOpacity) return;
  94. tween(uiOpacity).stop();
  95. uiOpacity.opacity = 255; // 恢复不透明
  96. }
  97. }