index.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. import {renderNumber, renderQuota} from '../../helpers/render';
  5. const TopUp = () => {
  6. const [redemptionCode, setRedemptionCode] = useState('');
  7. const [topUpCode, setTopUpCode] = useState('');
  8. const [topUpCount, setTopUpCount] = useState(10);
  9. const [amount, setAmount] = useState(0);
  10. const [topUpLink, setTopUpLink] = useState('');
  11. const [userQuota, setUserQuota] = useState(0);
  12. const [isSubmitting, setIsSubmitting] = useState(false);
  13. const topUp = async () => {
  14. if (redemptionCode === '') {
  15. showInfo('请输入充值码!')
  16. return;
  17. }
  18. setIsSubmitting(true);
  19. try {
  20. const res = await API.post('/api/user/topup', {
  21. key: redemptionCode
  22. });
  23. const {success, message, data} = res.data;
  24. if (success) {
  25. showSuccess('充值成功!');
  26. setUserQuota((quota) => {
  27. return quota + data;
  28. });
  29. setRedemptionCode('');
  30. } else {
  31. showError(message);
  32. }
  33. } catch (err) {
  34. showError('请求失败');
  35. } finally {
  36. setIsSubmitting(false);
  37. }
  38. };
  39. const openTopUpLink = () => {
  40. if (!topUpLink) {
  41. showError('超级管理员未设置充值链接!');
  42. return;
  43. }
  44. window.open(topUpLink, '_blank');
  45. };
  46. const onlineTopUp = async (payment) => {
  47. if (amount === 0) {
  48. await getAmount();
  49. }
  50. try {
  51. const res = await API.post('/api/user/pay', {
  52. amount: parseInt(topUpCount),
  53. top_up_code: topUpCode,
  54. PaymentMethod: payment
  55. });
  56. if (res !== undefined) {
  57. const {message, data} = res.data;
  58. // showInfo(message);
  59. if (message === 'success') {
  60. let params = data
  61. let url = res.data.url
  62. let form = document.createElement('form')
  63. form.action = url
  64. form.method = 'POST'
  65. form.target = '_blank'
  66. for (let key in params) {
  67. let input = document.createElement('input')
  68. input.type = 'hidden'
  69. input.name = key
  70. input.value = params[key]
  71. form.appendChild(input)
  72. }
  73. document.body.appendChild(form)
  74. form.submit()
  75. document.body.removeChild(form)
  76. } else {
  77. showError(data);
  78. // setTopUpCount(parseInt(res.data.count));
  79. // setAmount(parseInt(data));
  80. }
  81. } else {
  82. showError(res);
  83. }
  84. } catch (err) {
  85. console.log(err);
  86. } finally {
  87. }
  88. }
  89. const getUserQuota = async () => {
  90. let res = await API.get(`/api/user/self`);
  91. const {success, message, data} = res.data;
  92. if (success) {
  93. setUserQuota(data.quota);
  94. } else {
  95. showError(message);
  96. }
  97. }
  98. useEffect(() => {
  99. let status = localStorage.getItem('status');
  100. if (status) {
  101. status = JSON.parse(status);
  102. if (status.top_up_link) {
  103. setTopUpLink(status.top_up_link);
  104. }
  105. }
  106. getUserQuota().then();
  107. }, []);
  108. const renderAmount = () => {
  109. console.log(amount);
  110. return amount + '元';
  111. }
  112. const getAmount = async (value) => {
  113. if (value === undefined) {
  114. value = topUpCount;
  115. }
  116. try {
  117. const res = await API.post('/api/user/amount', {
  118. amount: parseFloat(value),
  119. top_up_code: topUpCode
  120. });
  121. if (res !== undefined) {
  122. const {message, data} = res.data;
  123. // showInfo(message);
  124. if (message === 'success') {
  125. setAmount(parseInt(data));
  126. } else {
  127. showError(data);
  128. // setTopUpCount(parseInt(res.data.count));
  129. // setAmount(parseInt(data));
  130. }
  131. } else {
  132. showError(res);
  133. }
  134. } catch (err) {
  135. console.log(err);
  136. } finally {
  137. }
  138. }
  139. return (
  140. <div>
  141. <Segment>
  142. <Header as='h3'>充值额度</Header>
  143. <Grid columns={2} stackable>
  144. <Grid.Column>
  145. <Form>
  146. <Form.Input
  147. placeholder='兑换码'
  148. name='redemptionCode'
  149. value={redemptionCode}
  150. onChange={(e) => {
  151. setRedemptionCode(e.target.value);
  152. }}
  153. />
  154. <Button color='green' onClick={openTopUpLink}>
  155. 获取兑换码
  156. </Button>
  157. <Button color='yellow' onClick={topUp} disabled={isSubmitting}>
  158. {isSubmitting ? '兑换中...' : '兑换'}
  159. </Button>
  160. </Form>
  161. </Grid.Column>
  162. <Grid.Column>
  163. <Statistic.Group widths='one'>
  164. <Statistic>
  165. <Statistic.Value>{renderQuota(userQuota)}</Statistic.Value>
  166. <Statistic.Label>剩余额度</Statistic.Label>
  167. </Statistic>
  168. </Statistic.Group>
  169. </Grid.Column>
  170. </Grid>
  171. </Segment>
  172. <Segment>
  173. <Header as='h3'>在线充值,最低1</Header>
  174. <Grid columns={2} stackable>
  175. <Grid.Column>
  176. <Form>
  177. <Form.Input
  178. placeholder='充值金额,最低1'
  179. name='redemptionCount'
  180. type={'number'}
  181. value={topUpCount}
  182. autoComplete={'off'}
  183. onChange={async (e) => {
  184. setTopUpCount(e.target.value);
  185. await getAmount(e.target.value);
  186. }}
  187. />
  188. {/*<Form.Input*/}
  189. {/* placeholder='充值码,如果你没有充值码,可不填写'*/}
  190. {/* name='redemptionCount'*/}
  191. {/* value={topUpCode}*/}
  192. {/* onChange={(e) => {*/}
  193. {/* setTopUpCode(e.target.value);*/}
  194. {/* }}*/}
  195. {/*/>*/}
  196. <Button color='blue' onClick={
  197. async () => {
  198. onlineTopUp('zfb')
  199. }
  200. }>
  201. 支付宝
  202. </Button>
  203. <Button color='green' onClick={
  204. async () => {
  205. onlineTopUp('wx')
  206. }
  207. }>
  208. 微信
  209. </Button>
  210. </Form>
  211. </Grid.Column>
  212. <Grid.Column>
  213. <Statistic.Group widths='one'>
  214. <Statistic>
  215. <Statistic.Value>{renderAmount()}</Statistic.Value>
  216. <Statistic.Label>支付金额</Statistic.Label>
  217. </Statistic>
  218. </Statistic.Group>
  219. </Grid.Column>
  220. </Grid>
  221. </Segment>
  222. </div>
  223. );
  224. };
  225. export default TopUp;