import { ToCent, ToYuan } from "oak-domain/lib/utils/money"; import assert from "assert"; import { RefundExceedMax } from "../../../types/Exception"; import { generateNewIdAsync } from "oak-domain/lib/utils/uuid"; import { combineFilters } from 'oak-domain/lib/store/filter'; export default OakComponent({ properties: { accountId: '', withdrawAccountFilter: {}, onNewWithdrawAccount: () => undefined, onCreateWithdraw: (id) => undefined, onGoToHistory: () => undefined, onGoToWaManage: () => undefined, }, formData({ features }) { const { accountId, withdrawAccountFilter } = this.props; const [account] = features.cache.get('account', { data: { id: 1, avail: 1, refundable: 1, }, filter: { id: accountId, } }); const { avail, refundable } = account; const refundAmount = Math.min(avail, refundable); const manualAmount = avail - refundAmount; const { value, withdrawData } = this.state; const { system } = features.application.getApplication(); const withdrawable = !!system?.payConfig?.withdrawLoss; const withdrawRatio = system?.payConfig?.withdrawLoss.conservative === false && system.payConfig.withdrawLoss.ratio; const withdrawLossText = typeof withdrawRatio === 'number' ? this.t('helps.content.loss.ratio', { value: withdrawRatio }) : this.t('helps.content.loss.conservative'); return { withdrawCreate: withdrawData && { ...withdrawData, refund$withdraw: withdrawData.refund$withdraw?.map((ele) => ele.data), withdrawTransfer$withdraw: withdrawData.withdrawTransfer$withdraw?.map((ele) => ele.data), }, withdrawable, withdrawLossText, executale: value > 0, account, refundAmount, refundAmountYuan: ToYuan(refundAmount), manualAmount, manualAmountYuan: ToYuan(manualAmount), avail, availYuan: ToYuan(avail), userId: features.token.getUserId(), }; }, features: ['cache', 'application', 'token'], lifetimes: { ready() { const { withdrawAccountFilter } = this.props; const waFilter = combineFilters('withdrawAccount', this.features.cache.getSchema(), [ withdrawAccountFilter, { entity: 'user', entityId: this.features.token.getUserId(), channel: { enabled: true, }, } ]); this.setState({ waFilter, }); } }, data: { value: 0, valueYuan: 0, showMethodHelp: false, showLossHelp: false, withdrawData: null, t(k, p) { return this.t(k, p); }, chooseWa: false, withdrawAccountId: '', waFilter: undefined, pickWithdrawChannelMp(id) { this.pickWithdrawChannel(id); }, restartAllMp() { this.restartAll(); } }, methods: { setValue(valueYuan) { let valueYuan2 = typeof valueYuan === 'string' ? parseInt(valueYuan) : valueYuan; const value = valueYuan2 ? ToCent(valueYuan2) : null; this.setState({ value, valueYuan: valueYuan2 }, () => this.reRender()); }, switchHelp(type) { switch (type) { case 'method': { this.setState({ showMethodHelp: !this.state.showMethodHelp, showLossHelp: false, }); break; } case 'loss': { this.setState({ showMethodHelp: false, showLossHelp: !this.state.showLossHelp, }); break; } } }, async createWithdrawData() { const { value, refundAmount, avail, withdrawAccountId } = this.state; if (value) { if (value > avail) { this.setMessage({ type: 'error', content: this.t('error::withdraw.overflow'), }); } else if (value <= refundAmount) { this.resetCreateData(); } else if (!withdrawAccountId) { this.setState({ chooseWa: true, }); } } }, async resetCreateData() { const { value, withdrawAccountId } = this.state; try { const { result: withdrawData } = (await this.features.cache.exec('getWithdrawCreateData', { accountId: this.props.accountId, price: value, withdrawAccountId, })); let loss = 0; this.setState({ withdrawData, }, () => this.reRender()); } catch (err) { if (err instanceof RefundExceedMax) { this.features.cache.refresh('account', { data: { id: 1, avail: 1, refundable: 1, }, filter: { id: this.props.accountId, } }); } this.setState({ chooseWa: false, withdrawAccountId: '', }); throw err; } }, restartAll() { this.setState({ withdrawData: null, withdrawAccountId: '', chooseWa: false, }, () => this.reRender()); }, async createWithdraw() { const { withdrawData } = this.state; assert(withdrawData); const id = withdrawData.id; await this.features.cache.exec('operate', { entity: 'withdraw', operation: { id: await generateNewIdAsync(), data: withdrawData, action: 'create', } }); const { onCreateWithdraw } = this.props; onCreateWithdraw && onCreateWithdraw(id); }, pickWithdrawChannel(id) { this.setState({ chooseWa: false, withdrawAccountId: id, }, () => this.resetCreateData()); }, InputValueMp(input) { const { detail, } = input; const { value } = detail; const { availYuan } = this.state; if (parseInt(value) > availYuan) { this.setValue(availYuan); } else if (parseInt(value) > 0) { this.setValue(value); } else { this.setValue(0); } }, setValueMp(e) { const value = e.currentTarget.dataset.value; this.setValue(value); }, switchHelpMp(e) { const value = e.currentTarget.dataset.value; this.switchHelp(value); }, goBack() { this.navigateBack(); }, onGoToHistoryMp() { const { onGoToHistory } = this.props; onGoToHistory && onGoToHistory(); }, onGoToWaManageMp() { this.props.onGoToWaManage(); } }, });