WithdrawStore.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import {
  2. APP_WITHDRAW_BASE_INFO,
  3. APP_WITHDRAW_PAYOUT,
  4. APP_WITHDRAW_RECORD,
  5. APP_WITHDRAW_SET_INFO,
  6. success
  7. } from "@scripts/api";
  8. import { Http } from "@scripts/request";
  9. import { IWithdrawBaseInfo, IWithdrawRecordItem, IWithdrawRecordPageData, IWithdrawSetInfoRequest } from "./data";
  10. import { CASH_BASE } from "@scripts/constants/App";
  11. import { PayoutType } from "@scripts/define/PayDefine";
  12. import { checkResponse } from "@scripts/api/ApiHelper";
  13. import { AppKernel } from "@scripts/app/AppKernel";
  14. class WithdrawStore {
  15. private _baseInfo: IWithdrawBaseInfo = null;
  16. private _records: IWithdrawRecordItem[] = null;
  17. public get baseInfo() { return this._baseInfo; }
  18. public get records() { return this._records; }
  19. public get minExchange() { return Math.floor(this.baseInfo.MinExchange / CASH_BASE); }
  20. public get maxExchange() { return Math.floor(this.baseInfo.MaxExchange / CASH_BASE); }
  21. public get sumCanExchange() {
  22. const SumCanExchange = this.baseInfo.SumCanExchange;
  23. return Math.floor(SumCanExchange / CASH_BASE);
  24. }
  25. async reqBaseInfo(): Promise<boolean> {
  26. const res = await Http.call<IWithdrawBaseInfo>(APP_WITHDRAW_BASE_INFO);
  27. if (success(res) && res.data) {
  28. this._baseInfo = this.parseWithdrawBaseInfo(res.data);
  29. AppKernel.events.emit('wallet:update');
  30. return true;
  31. }
  32. return false;
  33. }
  34. async reqWithdrawRecord(arg: any = {}) {
  35. const params: Record<string, unknown> = {
  36. paypass: arg.paypass || '',
  37. };
  38. if (arg.score_type !== undefined) params.score_type = arg.score_type;
  39. if (arg.sort_by !== undefined) params.sort_by = arg.sort_by;
  40. if (arg.sort !== undefined) params.sort = arg.sort;
  41. const res = await Http.call<IWithdrawRecordPageData>(APP_WITHDRAW_RECORD, params);
  42. if (success(res) && res.data) {
  43. const list = Array.isArray(res.data.data) ? res.data.data : [];
  44. this._records = list.map(item => {
  45. item.ServiceFee = Number(item.ServiceFee);
  46. item.WithDraw = Number(item.WithDraw);
  47. item.State = Number(item.State);
  48. item.PixType = Number(item.PixType);
  49. return item;
  50. })
  51. return true;
  52. }
  53. return false;
  54. }
  55. async reqWithdrawSetInfo(arg: IWithdrawSetInfoRequest): Promise<boolean> {
  56. const params = {
  57. cbPixType: arg.pixType,
  58. email: arg.paypal || '',
  59. PixNum: arg.cashTag || '',
  60. paypass: '',
  61. bank: '',
  62. account: '',
  63. userName: '',
  64. phone: '',
  65. PAN: '',
  66. IFSC: '',
  67. adhaar: '',
  68. branceBank: '',
  69. code: '',
  70. };
  71. const res = await Http.call(APP_WITHDRAW_SET_INFO, params);
  72. if (checkResponse(res)) {
  73. await this.reqBaseInfo();
  74. return true;
  75. }
  76. return false;
  77. }
  78. async reqWithdrawPayout(score: number): Promise<boolean> {
  79. score = score * CASH_BASE;
  80. const res = await Http.call(APP_WITHDRAW_PAYOUT, { paypass: '', score });
  81. if (checkResponse(res)) {
  82. await this.reqBaseInfo();
  83. this._records = null;
  84. return true;
  85. }
  86. return false;
  87. }
  88. private parseWithdrawBaseInfo(data: IWithdrawBaseInfo): IWithdrawBaseInfo {
  89. const info = { ...data };
  90. info.SumCanExchange = Number(info.SumCanExchange);
  91. const defaultCooldown = Number(info.withdraw_info_cooldown_seconds || 0);
  92. const cashappCooldown = Number(
  93. info.withdraw_info_precheck?.cashapp?.cooldown_seconds ?? defaultCooldown,
  94. );
  95. const paypalCooldown = Number(
  96. info.withdraw_info_precheck?.paypal?.cooldown_seconds ?? defaultCooldown,
  97. );
  98. info.MinExchange = Number(info.MinExchange);
  99. info.MaxExchange = Number(info.MaxExchange);
  100. info.cashapp_update_cd = cashappCooldown > 0 ? Date.now() + cashappCooldown * 1000 : 0;
  101. info.paypal_update_cd = paypalCooldown > 0 ? Date.now() + paypalCooldown * 1000 : 0;
  102. info.lastUpdateTs = Date.now() - Number(info.withdraw_info_update_time_pass || 0) * 1000;
  103. return info;
  104. }
  105. getPayoutAccount(type: PayoutType) {
  106. switch (type) {
  107. case PayoutType.CashApp:
  108. return this.baseInfo.PixNum;
  109. case PayoutType.Paypal:
  110. return this.baseInfo.EmailAddress;
  111. }
  112. }
  113. getPayoutRange(type: PayoutType) {
  114. const config = AppKernel.stores.mainStore.withdrawChannel;
  115. switch (type) {
  116. case PayoutType.CashApp:
  117. return config.cashapp;
  118. case PayoutType.Paypal:
  119. return config.paypal;
  120. }
  121. }
  122. }
  123. export const withdrawStore = new WithdrawStore();