| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- import React, {useEffect, useState} from 'react';
- import {Button, Form, Grid, Header, Segment, Statistic} from 'semantic-ui-react';
- import {API, showError, showInfo, showSuccess} from '../../helpers';
- import {renderNumber, renderQuota} from '../../helpers/render';
- const TopUp = () => {
- const [redemptionCode, setRedemptionCode] = useState('');
- const [topUpCode, setTopUpCode] = useState('');
- const [topUpCount, setTopUpCount] = useState(10);
- const [amount, setAmount] = useState(0);
- const [topUpLink, setTopUpLink] = useState('');
- const [userQuota, setUserQuota] = useState(0);
- const [isSubmitting, setIsSubmitting] = useState(false);
- const topUp = async () => {
- if (redemptionCode === '') {
- showInfo('请输入充值码!')
- return;
- }
- setIsSubmitting(true);
- try {
- const res = await API.post('/api/user/topup', {
- key: redemptionCode
- });
- const {success, message, data} = res.data;
- if (success) {
- showSuccess('充值成功!');
- setUserQuota((quota) => {
- return quota + data;
- });
- setRedemptionCode('');
- } else {
- showError(message);
- }
- } catch (err) {
- showError('请求失败');
- } finally {
- setIsSubmitting(false);
- }
- };
- const openTopUpLink = () => {
- if (!topUpLink) {
- showError('超级管理员未设置充值链接!');
- return;
- }
- window.open(topUpLink, '_blank');
- };
- const onlineTopUp = async (payment) => {
- if (amount === 0) {
- await getAmount();
- }
- try {
- const res = await API.post('/api/user/pay', {
- amount: parseInt(topUpCount),
- top_up_code: topUpCode,
- PaymentMethod: payment
- });
- if (res !== undefined) {
- const {message, data} = res.data;
- // showInfo(message);
- if (message === 'success') {
- let params = data
- let url = res.data.url
- let form = document.createElement('form')
- form.action = url
- form.method = 'POST'
- form.target = '_blank'
- for (let key in params) {
- let input = document.createElement('input')
- input.type = 'hidden'
- input.name = key
- input.value = params[key]
- form.appendChild(input)
- }
- document.body.appendChild(form)
- form.submit()
- document.body.removeChild(form)
- } else {
- showError(data);
- // setTopUpCount(parseInt(res.data.count));
- // setAmount(parseInt(data));
- }
- } else {
- showError(res);
- }
- } catch (err) {
- console.log(err);
- } finally {
- }
- }
- const getUserQuota = async () => {
- let res = await API.get(`/api/user/self`);
- const {success, message, data} = res.data;
- if (success) {
- setUserQuota(data.quota);
- } else {
- showError(message);
- }
- }
- useEffect(() => {
- let status = localStorage.getItem('status');
- if (status) {
- status = JSON.parse(status);
- if (status.top_up_link) {
- setTopUpLink(status.top_up_link);
- }
- }
- getUserQuota().then();
- }, []);
- const renderAmount = () => {
- console.log(amount);
- return amount + '元';
- }
- const getAmount = async (value) => {
- if (value === undefined) {
- value = topUpCount;
- }
- try {
- const res = await API.post('/api/user/amount', {
- amount: parseFloat(value),
- top_up_code: topUpCode
- });
- if (res !== undefined) {
- const {message, data} = res.data;
- // showInfo(message);
- if (message === 'success') {
- setAmount(parseInt(data));
- } else {
- showError(data);
- // setTopUpCount(parseInt(res.data.count));
- // setAmount(parseInt(data));
- }
- } else {
- showError(res);
- }
- } catch (err) {
- console.log(err);
- } finally {
- }
- }
- return (
- <div>
- <Segment>
- <Header as='h3'>充值额度</Header>
- <Grid columns={2} stackable>
- <Grid.Column>
- <Form>
- <Form.Input
- placeholder='兑换码'
- name='redemptionCode'
- value={redemptionCode}
- onChange={(e) => {
- setRedemptionCode(e.target.value);
- }}
- />
- <Button color='green' onClick={openTopUpLink}>
- 获取兑换码
- </Button>
- <Button color='yellow' onClick={topUp} disabled={isSubmitting}>
- {isSubmitting ? '兑换中...' : '兑换'}
- </Button>
- </Form>
- </Grid.Column>
- <Grid.Column>
- <Statistic.Group widths='one'>
- <Statistic>
- <Statistic.Value>{renderQuota(userQuota)}</Statistic.Value>
- <Statistic.Label>剩余额度</Statistic.Label>
- </Statistic>
- </Statistic.Group>
- </Grid.Column>
- </Grid>
- </Segment>
- <Segment>
- <Header as='h3'>在线充值,最低1</Header>
- <Grid columns={2} stackable>
- <Grid.Column>
- <Form>
- <Form.Input
- placeholder='充值金额,最低1'
- name='redemptionCount'
- type={'number'}
- value={topUpCount}
- autoComplete={'off'}
- onChange={async (e) => {
- setTopUpCount(e.target.value);
- await getAmount(e.target.value);
- }}
- />
- {/*<Form.Input*/}
- {/* placeholder='充值码,如果你没有充值码,可不填写'*/}
- {/* name='redemptionCount'*/}
- {/* value={topUpCode}*/}
- {/* onChange={(e) => {*/}
- {/* setTopUpCode(e.target.value);*/}
- {/* }}*/}
- {/*/>*/}
- <Button color='blue' onClick={
- async () => {
- onlineTopUp('zfb')
- }
- }>
- 支付宝
- </Button>
- <Button color='green' onClick={
- async () => {
- onlineTopUp('wx')
- }
- }>
- 微信
- </Button>
- </Form>
- </Grid.Column>
- <Grid.Column>
- <Statistic.Group widths='one'>
- <Statistic>
- <Statistic.Value>{renderAmount()}</Statistic.Value>
- <Statistic.Label>支付金额</Statistic.Label>
- </Statistic>
- </Statistic.Group>
- </Grid.Column>
- </Grid>
- </Segment>
- </div>
- );
- };
- export default TopUp;
|