267 lines
10 KiB
JavaScript
Raw Normal View History

2023-08-14 22:16:32 +08:00
import React, {useEffect, useState} from 'react';
2023-11-08 00:40:12 +08:00
import {Confirm} from 'semantic-ui-react';
2023-11-03 22:38:17 +08:00
import {API, isMobile, showError, showInfo, showSuccess} from '../../helpers';
2023-08-14 22:16:32 +08:00
import {renderNumber, renderQuota} from '../../helpers/render';
2023-11-08 00:40:12 +08:00
import {Col, Layout, Row, Typography, Card, Button, Form, Divider, Space, Modal} from "@douyinfe/semi-ui";
import Title from "@douyinfe/semi-ui/lib/es/typography/title";
const TopUp = () => {
2023-08-14 22:16:32 +08:00
const [redemptionCode, setRedemptionCode] = useState('');
const [topUpCode, setTopUpCode] = useState('');
const [topUpCount, setTopUpCount] = useState(10);
2023-11-08 00:40:12 +08:00
const [amount, setAmount] = useState(0.0);
2023-08-14 22:16:32 +08:00
const [topUpLink, setTopUpLink] = useState('');
const [userQuota, setUserQuota] = useState(0);
const [isSubmitting, setIsSubmitting] = useState(false);
2023-10-11 14:57:55 +08:00
const [open, setOpen] = useState(false);
const [payWay, setPayWay] = useState('');
2023-08-14 22:16:32 +08:00
const topUp = async () => {
if (redemptionCode === '') {
2023-11-08 00:40:12 +08:00
showInfo('请输入兑换码!')
2023-08-14 22:16:32 +08:00
return;
}
setIsSubmitting(true);
try {
const res = await API.post('/api/user/topup', {
key: redemptionCode
});
const {success, message, data} = res.data;
if (success) {
2023-11-08 00:40:12 +08:00
showSuccess('兑换成功!');
Modal.success({title: '兑换成功!', content: '成功兑换额度:' + renderQuota(data), centered: true});
2023-08-14 22:16:32 +08:00
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');
};
2023-10-11 14:57:55 +08:00
const preTopUp = async (payment) => {
2023-08-14 22:16:32 +08:00
if (amount === 0) {
await getAmount();
}
2023-10-11 14:57:55 +08:00
setPayWay(payment)
setOpen(true);
}
const onlineTopUp = async () => {
if (amount === 0) {
await getAmount();
}
setOpen(false);
2023-08-14 22:16:32 +08:00
try {
const res = await API.post('/api/user/pay', {
amount: parseInt(topUpCount),
top_up_code: topUpCode,
2023-11-24 14:12:27 +08:00
payment_method: payWay
2023-08-14 22:16:32 +08:00
});
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 {
2023-09-09 03:11:42 +08:00
showError(data);
2023-08-14 22:16:32 +08:00
// setTopUpCount(parseInt(res.data.count));
2023-09-09 03:11:42 +08:00
// setAmount(parseInt(data));
2023-08-14 22:16:32 +08:00
}
} else {
showError(res);
}
} catch (err) {
console.log(err);
} finally {
}
}
2023-08-14 22:16:32 +08:00
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);
}
}
2023-08-14 22:16:32 +08:00
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 + '元';
}
2023-08-14 22:16:32 +08:00
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') {
2023-11-08 00:40:12 +08:00
setAmount(parseFloat(data));
2023-08-14 22:16:32 +08:00
} else {
2023-09-09 05:04:49 +08:00
showError(data);
2023-08-14 22:16:32 +08:00
// setTopUpCount(parseInt(res.data.count));
2023-09-09 05:04:49 +08:00
// setAmount(parseInt(data));
2023-08-14 22:16:32 +08:00
}
} else {
showError(res);
}
} catch (err) {
console.log(err);
} finally {
}
}
2023-10-11 14:57:55 +08:00
const handleCancel = () => {
setOpen(false);
}
2023-08-14 22:16:32 +08:00
return (
<div>
2023-11-03 22:38:17 +08:00
<Layout>
<Layout.Header>
<h3>我的钱包</h3>
</Layout.Header>
<Layout.Content>
2023-11-08 00:40:12 +08:00
<Modal
title="确定要充值吗"
visible={open}
onOk={onlineTopUp}
onCancel={handleCancel}
maskClosable={false}
size={'small'}
centered={true}
>
<p>充值数量{topUpCount}</p>
<p>充值金额{renderAmount()}</p>
<p>是否确认充值</p>
</Modal>
<div style={{marginTop: 20, display: 'flex', justifyContent: 'center'}}>
2023-11-03 22:38:17 +08:00
<Card
2023-11-08 00:40:12 +08:00
style={{width: '500px', padding: '20px'}}
2023-11-03 22:38:17 +08:00
>
2023-11-08 00:40:12 +08:00
<Title level={3} style={{textAlign: 'center'}}>余额 {renderQuota(userQuota)}</Title>
<div style={{marginTop: 20}}>
<Divider>
兑换余额
</Divider>
<Form>
<Form.Input
field={'redemptionCode'}
label={'兑换码'}
placeholder='兑换码'
name='redemptionCode'
value={redemptionCode}
onChange={(value) => {
setRedemptionCode(value);
}}
/>
<Space>
{
topUpLink ?
<Button type={'primary'} theme={'solid'} onClick={openTopUpLink}>
获取兑换码
</Button> : null
}
<Button type={"warning"} theme={'solid'} onClick={topUp}
disabled={isSubmitting}>
2023-11-03 22:38:17 +08:00
{isSubmitting ? '兑换中...' : '兑换'}
</Button>
2023-11-08 00:40:12 +08:00
</Space>
</Form>
</div>
<div style={{marginTop: 20}}>
<Divider>
在线充值
</Divider>
<Form>
<Form.Input
field={'redemptionCount'}
label={'充值金额:' + renderAmount()}
placeholder='充值数量'
name='redemptionCount'
type={'number'}
value={topUpCount}
onChange={async (value) => {
setTopUpCount(value);
await getAmount(value);
}}
/>
<Space>
<Button type={'primary'} theme={'solid'} onClick={
2023-11-03 22:38:17 +08:00
async () => {
preTopUp('zfb')
}
}>
支付宝
</Button>
2023-11-08 00:40:12 +08:00
<Button style={{backgroundColor: 'rgba(var(--semi-green-5), 1)'}}
type={'primary'}
theme={'solid'} onClick={
2023-11-03 22:38:17 +08:00
async () => {
preTopUp('wx')
}
}>
微信
</Button>
2023-11-08 00:40:12 +08:00
</Space>
</Form>
</div>
2023-11-03 22:38:17 +08:00
</Card>
</div>
</Layout.Content>
</Layout>
2023-08-14 22:16:32 +08:00
</div>
);
};
export default TopUp;