WeeklyPassPurchase.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { _decorator, Button, Color, EditBox, instantiate, Label, Node, RichText, Sprite, SpriteFrame, UITransform } from 'cc';
  2. import { UIViewID } from '@scripts/define/UIDefine';
  3. import { UICloseReason } from '@scripts/framework/ui/UILayer';
  4. import { addBtnClick } from '@scripts/utils/UIHelper';
  5. import { AppKernel } from '@scripts/app/AppKernel';
  6. import { SelectBox } from '@scripts/components/selectBox/SelectBox';
  7. import { UIPopup } from '@scripts/framework/ui/UIPopup';
  8. import { formatNumShort } from '@scripts/utils/Helper';
  9. import { REWARD_OPEN_TYPE } from '../RewardView/RewardView';
  10. const { ccclass, property } = _decorator;
  11. @ccclass('WeeklyPassPurchase')
  12. export class WeeklyPassPurchase extends UIPopup {
  13. readonly viewId = UIViewID.WeeklyPassPurchase;
  14. @property(Node)
  15. private icon: Node = null;
  16. @property(Label)
  17. private desc: Label = null;
  18. @property(Node)
  19. private rewards: Node = null;
  20. @property(Node)
  21. private btn_close: Node = null;
  22. @property(Node)
  23. private btn_buy: Node = null;
  24. private _tier: any = {};
  25. private _gear: any = {};
  26. private _index: number = 0;
  27. protected onOpen(_params): void | Promise<void> {
  28. this._tier = _params?.tier;
  29. this._gear = _params?.gear;
  30. this._index = _params?.index;
  31. this.updateView();
  32. }
  33. protected onCreate(): void | Promise<void> {
  34. AppKernel.events.on("hall:pay-finished", this.handlePayFinished, this);
  35. addBtnClick(this.btn_close, this.onBtnCloseClick, this);
  36. addBtnClick(this.btn_buy, this.onBtnBuyClick, this);
  37. }
  38. private updateView() {
  39. const money = this._gear?.money;
  40. this.icon?.children?.forEach((node, idx) => node.active = idx === this._index);
  41. let str = ``;
  42. switch (this._index) {
  43. case 0:
  44. str = `GOLD PASS -$${money} -7x24H`;
  45. break;
  46. case 1:
  47. str = `SILVER PASS -$${money} -7x24H`;
  48. break;
  49. case 2:
  50. str = `IRON PASS -$${money} -7x24H`;
  51. break;
  52. }
  53. this.desc.string = str;
  54. this.btn_buy.getChildByName("label").getComponent(Label).string = `BUY $${money}`;
  55. const reward = {
  56. sc: this._gear?.sc,
  57. gc: this._gear?.gc,
  58. cp: this._gear?.cp
  59. };
  60. this.rewards?.children?.forEach(item => item.active = false);
  61. for (const [key, val] of Object.entries(reward)) {
  62. const propKey = `prop_${key}`;
  63. const item = this.rewards.getChildByName(propKey);
  64. if (!item || val == 0) continue;
  65. item.active = true;
  66. const num = item.getChildByName('num');
  67. const label = num.getComponent(Label);
  68. if (key == "sc" || key == "gc") {
  69. // label.string = `${(Number(val) / 100).toFixed(2)}`;
  70. label.string = formatNumShort(String(val), 999);
  71. } else {
  72. // label.string = `${val}`;
  73. label.string = formatNumShort(String(val), 999, 1);
  74. }
  75. }
  76. }
  77. private onBtnCloseClick() {
  78. AppKernel.ui.close(this.viewId);
  79. }
  80. private onBtnBuyClick() {
  81. const payServices = AppKernel.services.paymentServices;
  82. const formatGear = this.formatGoods(this._gear);
  83. payServices.startPayment(formatGear, 0);
  84. }
  85. private handlePayFinished() {
  86. AppKernel.stores.weeklyPassStore.reqWeeklyPassInfo();
  87. const tier = this._tier ?? {};
  88. this.closeSelf();
  89. AppKernel.ui.open(UIViewID.RewardView, {
  90. reward: tier?.instant_reward,
  91. type: REWARD_OPEN_TYPE.INSTANT
  92. });
  93. }
  94. private formatGoods(goods: any) {
  95. goods.gears = JSON.parse(goods.gear);
  96. goods.cp = Number(goods.cp);
  97. goods.cp_reward = Number(goods.cp_reward);
  98. goods.cp_reward_display_percent = Number(goods.cp_reward_display_percent);
  99. goods.gc = Number(goods.gc);
  100. goods.gc_reward = Number(goods.gc_reward);
  101. goods.gc_reward_display_percent = Number(goods.gc_reward_display_percent);
  102. goods.gc_vip_reward = Number(goods.gc_vip_reward);
  103. goods.gc_vip_reward_display_percent = Number(goods.gc_vip_reward_display_percent);
  104. goods.sc = Number(goods.sc);
  105. goods.sc_reward = Number(goods.sc_reward);
  106. goods.sc_reward_display_percent = Number(goods.sc_reward_display_percent);
  107. return goods;
  108. }
  109. }