index.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import React, { useEffect, useState } from 'react';
  2. import { Button, Form, Grid, Header, Segment, Statistic } from 'semantic-ui-react';
  3. import { API, showError, showInfo, showSuccess } from '../../helpers';
  4. const TopUp = () => {
  5. const [redemptionCode, setRedemptionCode] = useState('');
  6. const [topUpLink, setTopUpLink] = useState('');
  7. const [userQuota, setUserQuota] = useState(0);
  8. const topUp = async () => {
  9. if (redemptionCode === '') {
  10. showInfo('请输入充值码!')
  11. return;
  12. }
  13. const res = await API.post('/api/user/topup', {
  14. key: redemptionCode
  15. });
  16. const { success, message, data } = res.data;
  17. if (success) {
  18. showSuccess('充值成功!');
  19. setUserQuota((quota) => {
  20. return quota + data;
  21. });
  22. setRedemptionCode('');
  23. } else {
  24. showError(message);
  25. }
  26. };
  27. const openTopUpLink = () => {
  28. if (!topUpLink) {
  29. showError('超级管理员未设置充值链接!');
  30. return;
  31. }
  32. window.open(topUpLink, '_blank');
  33. };
  34. const getUserQuota = async ()=>{
  35. let res = await API.get(`/api/user/self`);
  36. const {success, message, data} = res.data;
  37. if (success) {
  38. setUserQuota(data.quota);
  39. } else {
  40. showError(message);
  41. }
  42. }
  43. useEffect(() => {
  44. let status = localStorage.getItem('status');
  45. if (status) {
  46. status = JSON.parse(status);
  47. if (status.top_up_link) {
  48. setTopUpLink(status.top_up_link);
  49. }
  50. }
  51. getUserQuota().then();
  52. }, []);
  53. return (
  54. <Segment>
  55. <Header as='h3'>充值额度</Header>
  56. <Grid columns={2} stackable>
  57. <Grid.Column>
  58. <Form>
  59. <Form.Input
  60. placeholder='兑换码'
  61. name='redemptionCode'
  62. value={redemptionCode}
  63. onChange={(e) => {
  64. setRedemptionCode(e.target.value);
  65. }}
  66. />
  67. <Button color='green' onClick={openTopUpLink}>
  68. 获取兑换码
  69. </Button>
  70. <Button color='yellow' onClick={topUp}>
  71. 充值
  72. </Button>
  73. </Form>
  74. </Grid.Column>
  75. <Grid.Column>
  76. <Statistic.Group widths='one'>
  77. <Statistic>
  78. <Statistic.Value>{userQuota.toLocaleString()}</Statistic.Value>
  79. <Statistic.Label>剩余额度</Statistic.Label>
  80. </Statistic>
  81. </Statistic.Group>
  82. </Grid.Column>
  83. </Grid>
  84. </Segment>
  85. );
  86. };
  87. export default TopUp;