RedeemMain.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import { AppKernel } from '@scripts/app/AppKernel';
  2. import { CASH_BASE } from '@scripts/constants/App';
  3. import { CURRENCY_TYPE } from '@scripts/define/Define';
  4. import { PayoutGears } from '@scripts/define/PayDefine';
  5. import { UIViewID } from '@scripts/define/UIDefine';
  6. import { formatCurrency, formatGoodsNum, formatMaskEmail, safeFloor } from '@scripts/utils/Helper';
  7. import { setSpriteFrame } from '@scripts/utils/ResHelper';
  8. import { addBtnClick, addTogClick, setAsLastSibling } from '@scripts/utils/UIHelper';
  9. import { _decorator, Component, EditBox, find, instantiate, Label, Node, ScrollView, Sprite, Toggle } from 'cc';
  10. const { ccclass, property } = _decorator;
  11. const FAST_MAX = -1;
  12. const FastNum = [20, 50, 100, FAST_MAX];
  13. @ccclass('RedeemMain')
  14. export class RedeemMain extends Component {
  15. @property(Label)
  16. lblAmount: Label;
  17. @property(Label)
  18. lblFee: Label;
  19. @property(Label)
  20. lblFeePrefix: Label;
  21. @property(Label)
  22. lblReceive: Label;
  23. @property(Label)
  24. lblFeeTips: Label;
  25. @property(EditBox)
  26. inputNum: EditBox;
  27. @property(Node)
  28. payoutItem: Node;
  29. @property(ScrollView)
  30. payoutScroll: ScrollView;
  31. @property(Label)
  32. lblAccount: Label;
  33. @property(Node)
  34. btnEdit: Node;
  35. @property(Node)
  36. btnConfirm: Node;
  37. @property(Node)
  38. btnGrey: Node;
  39. @property([Node])
  40. btnFasts: Node[] = []
  41. private m_payouts: PayoutItem[] = [];
  42. private m_payoutIdx = -1;
  43. private get m_nowPayout() { return PayoutGears[this.m_payoutIdx]; }
  44. private get m_inputAmount() {
  45. const amount = Number(this.inputNum.string);
  46. if (Number.isNaN(amount)) return 0;
  47. return amount;
  48. }
  49. init() {
  50. this.btnGrey.active = false;
  51. addBtnClick(this.btnEdit, this.onBtnEidtClick, this);
  52. addBtnClick(this.btnConfirm, this.onBtnConfirmClick, this);
  53. AppKernel.events.on('wallet:update', this.onWalletUpdate, this);
  54. this.inputNum.node.on(EditBox.EventType.TEXT_CHANGED, this.onInputNum, this);
  55. this.btnFasts.forEach((btnFast, index) => {
  56. btnFast.active = index < FastNum.length;
  57. if (index < FastNum.length) {
  58. const num = FastNum[index];
  59. const label = find("Label", btnFast).getComponent(Label);
  60. label.string = num === FAST_MAX ? "MAX" : `${num} ${CURRENCY_TYPE.SC}`;
  61. addBtnClick(btnFast, () => { this.onBtnFastClick(num) });
  62. }
  63. })
  64. this.initPayouts();
  65. this.onAmountUpdate(0);
  66. }
  67. protected onDestroy(): void {
  68. AppKernel.events.offTarget(this);
  69. }
  70. private onWalletUpdate() {
  71. const gear = this.m_nowPayout;
  72. const withdraw = AppKernel.stores.withdrawStore;
  73. const account = withdraw.getPayoutAccount(gear.type);
  74. this.lblAccount.string = formatMaskEmail(account);
  75. }
  76. private onAmountUpdate(amount: number) {
  77. this.updateBtnGrey();
  78. const feeRate = AppKernel.stores.mainStore.withdrawFeeRate;
  79. const fee = safeFloor(feeRate / 100 * amount);
  80. const receive = safeFloor(amount - fee);
  81. this.lblAmount.string = formatCurrency(amount, 2) + " " + CURRENCY_TYPE.SC;
  82. this.lblFee.string = formatCurrency(fee, 2) + " " + CURRENCY_TYPE.SC;
  83. this.lblReceive.string = formatCurrency(receive, 2) + " " + CURRENCY_TYPE.SC;
  84. }
  85. private onInputNum(eidtBox: EditBox) {
  86. let num = Number(eidtBox.string);
  87. if (Number.isNaN(num)) return;
  88. const sumCanExchange = AppKernel.stores.withdrawStore.sumCanExchange;
  89. if (num > sumCanExchange) {
  90. num = sumCanExchange;
  91. this.inputNum.string = sumCanExchange.toString();
  92. }
  93. this.onAmountUpdate(num)
  94. }
  95. private onBtnFastClick(num: number) {
  96. const sumCanExchange = AppKernel.stores.withdrawStore.sumCanExchange;
  97. const isMax = num === FAST_MAX;
  98. if (isMax) {
  99. this.onAmountUpdate(sumCanExchange);
  100. this.inputNum.string = sumCanExchange.toString();
  101. return;
  102. }
  103. const value = Math.min(num, sumCanExchange);
  104. this.inputNum.string = value.toString();
  105. this.onAmountUpdate(value);
  106. }
  107. private onBtnEidtClick() {
  108. AppKernel.ui.open(UIViewID.UIPayoutEditor, this.m_nowPayout.type);
  109. }
  110. private async onBtnConfirmClick() {
  111. const amount = this.m_inputAmount;
  112. if (amount < 0) return;
  113. const payoutType = this.m_nowPayout.type;
  114. const payoutName = this.m_nowPayout.name;
  115. const withdraw = AppKernel.stores.withdrawStore;
  116. const limit = withdraw.getPayoutRange(payoutType);
  117. const minVal = Math.max(limit.min, withdraw.minExchange);
  118. const maxVal = Math.min(limit.max, withdraw.maxExchange);
  119. if (amount < minVal || amount > maxVal) {
  120. const tips = AppKernel.i18n.t("payout.limit", minVal, maxVal);
  121. AppKernel.ui.toast(tips);
  122. return;
  123. }
  124. const account = withdraw.getPayoutAccount(payoutType);
  125. const feeRate = AppKernel.stores.mainStore.withdrawFeeRate;
  126. const fee = safeFloor(feeRate / 100 * amount);
  127. const receive = safeFloor(amount - fee);
  128. AppKernel.ui.open(UIViewID.UIPayoutConfrim, {
  129. amount,
  130. reallAmount: receive,
  131. feeAmount: fee,
  132. feePercent: feeRate,
  133. method: payoutName,
  134. account,
  135. payoutType,
  136. });
  137. // AppKernel.ui.loading(true);
  138. // await withdraw.reqWithdrawPayout(amount);
  139. // AppKernel.ui.loading(false);
  140. }
  141. private updateBtnGrey() {
  142. const amount = this.m_inputAmount;
  143. if (amount <= 0) {
  144. this.btnGrey.active = true;
  145. return;
  146. }
  147. const account = this.lblAccount.string;
  148. if (!account || account.length === 0) {
  149. this.btnGrey.active = true;
  150. return;
  151. }
  152. this.btnGrey.active = false;
  153. }
  154. updateView() {
  155. const feeRate = AppKernel.stores.mainStore.withdrawFeeRate;
  156. const feeStr = feeRate.toFixed(1);
  157. const leastNum = 0.5;
  158. this.lblFeePrefix.string = AppKernel.i18n.t("redeem.service_fee", feeStr)
  159. this.lblFeeTips.string = AppKernel.i18n.t("redeem.servoce_fee_desc", feeStr, leastNum.toFixed(2));
  160. }
  161. private initPayouts() {
  162. this.m_payoutIdx = -1;
  163. const gears = PayoutGears;
  164. for (let i = 0; i < gears.length; ++i) {
  165. let item = this.m_payouts[i];
  166. if (!item) {
  167. const itemNode = instantiate(this.payoutItem);
  168. itemNode.setParent(this.payoutScroll.content);
  169. item = this.m_payouts[i] = new PayoutItem(itemNode);
  170. addTogClick(item.toggle, this.onPayoutItemClick, this);
  171. }
  172. const gear = gears[i];
  173. item.lblName.string = gear.name;
  174. setSpriteFrame(item.icon, gear.icon);
  175. item.node.active = true;
  176. item.node.name = i.toString();
  177. setAsLastSibling(item.node);
  178. }
  179. for (let i = gears.length; i < this.m_payouts.length; ++i) {
  180. const item = this.m_payouts[i]
  181. item.node.active = false;
  182. }
  183. this.m_payouts[0].toggle.isChecked = true;
  184. this.onChoosePayout(0);
  185. }
  186. private onPayoutItemClick(toggle: Toggle) {
  187. const idx = Number(toggle.node.name);
  188. if (idx !== this.m_payoutIdx) {
  189. this.onChoosePayout(idx);
  190. }
  191. }
  192. private onChoosePayout(idx: number) {
  193. this.m_payoutIdx = idx;
  194. this.onWalletUpdate();
  195. this.updateBtnGrey();
  196. }
  197. }
  198. class PayoutItem {
  199. icon: Sprite;
  200. toggle: Toggle;
  201. lblName: Label;
  202. lblKind: Label;
  203. constructor(readonly node: Node) {
  204. this.toggle = node.getComponent(Toggle);
  205. this.icon = find("sptIcon", node).getComponent(Sprite);
  206. this.lblName = find("lblName", node).getComponent(Label);
  207. this.lblKind = find("lblKind", node).getComponent(Label);
  208. }
  209. }