EditToken.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import React, { useEffect, useState } from 'react';
  2. import { Button, Form, Header, Message, Segment } from 'semantic-ui-react';
  3. import { useParams } from 'react-router-dom';
  4. import { API, showError, showSuccess, timestamp2string } from '../../helpers';
  5. const EditToken = () => {
  6. const params = useParams();
  7. const tokenId = params.id;
  8. const isEdit = tokenId !== undefined;
  9. const [loading, setLoading] = useState(isEdit);
  10. const originInputs = {
  11. name: '',
  12. remain_quota: 0,
  13. expired_time: -1,
  14. unlimited_quota: false
  15. };
  16. const [inputs, setInputs] = useState(originInputs);
  17. const { name, remain_quota, expired_time, unlimited_quota } = inputs;
  18. const handleInputChange = (e, { name, value }) => {
  19. setInputs((inputs) => ({ ...inputs, [name]: value }));
  20. };
  21. const setExpiredTime = (month, day, hour, minute) => {
  22. let now = new Date();
  23. let timestamp = now.getTime() / 1000;
  24. let seconds = month * 30 * 24 * 60 * 60;
  25. seconds += day * 24 * 60 * 60;
  26. seconds += hour * 60 * 60;
  27. seconds += minute * 60;
  28. if (seconds !== 0) {
  29. timestamp += seconds;
  30. setInputs({ ...inputs, expired_time: timestamp2string(timestamp) });
  31. } else {
  32. setInputs({ ...inputs, expired_time: -1 });
  33. }
  34. };
  35. const setUnlimitedQuota = () => {
  36. setInputs({ ...inputs, unlimited_quota: !unlimited_quota });
  37. };
  38. const loadToken = async () => {
  39. let res = await API.get(`/api/token/${tokenId}`);
  40. const { success, message, data } = res.data;
  41. if (success) {
  42. if (data.expired_time !== -1) {
  43. data.expired_time = timestamp2string(data.expired_time);
  44. }
  45. setInputs(data);
  46. } else {
  47. showError(message);
  48. }
  49. setLoading(false);
  50. };
  51. useEffect(() => {
  52. if (isEdit) {
  53. loadToken().then();
  54. }
  55. }, []);
  56. const submit = async () => {
  57. if (!isEdit && inputs.name === '') return;
  58. let localInputs = inputs;
  59. localInputs.remain_quota = parseInt(localInputs.remain_quota);
  60. if (localInputs.expired_time !== -1) {
  61. let time = Date.parse(localInputs.expired_time);
  62. if (isNaN(time)) {
  63. showError('过期时间格式错误!');
  64. return;
  65. }
  66. localInputs.expired_time = Math.ceil(time / 1000);
  67. }
  68. let res;
  69. if (isEdit) {
  70. res = await API.put(`/api/token/`, { ...localInputs, id: parseInt(tokenId) });
  71. } else {
  72. res = await API.post(`/api/token/`, localInputs);
  73. }
  74. const { success, message } = res.data;
  75. if (success) {
  76. if (isEdit) {
  77. showSuccess('令牌更新成功!');
  78. } else {
  79. showSuccess('令牌创建成功!');
  80. setInputs(originInputs);
  81. }
  82. } else {
  83. showError(message);
  84. }
  85. };
  86. return (
  87. <>
  88. <Segment loading={loading}>
  89. <Header as='h3'>{isEdit ? '更新令牌信息' : '创建新的令牌'}</Header>
  90. <Form autoComplete='new-password'>
  91. <Form.Field>
  92. <Form.Input
  93. label='名称'
  94. name='name'
  95. placeholder={'请输入名称'}
  96. onChange={handleInputChange}
  97. value={name}
  98. autoComplete='new-password'
  99. required={!isEdit}
  100. />
  101. </Form.Field>
  102. <Message>注意,令牌的额度仅用于限制令牌本身的最大额度使用量,实际的使用受到账户的剩余额度限制。</Message>
  103. <Form.Field>
  104. <Form.Input
  105. label='额度'
  106. name='remain_quota'
  107. placeholder={'请输入额度'}
  108. onChange={handleInputChange}
  109. value={remain_quota}
  110. autoComplete='new-password'
  111. type='number'
  112. disabled={unlimited_quota}
  113. />
  114. </Form.Field>
  115. <Button type={'button'} style={{ marginBottom: '14px' }} onClick={() => {
  116. setUnlimitedQuota();
  117. }}>{unlimited_quota ? '取消无限额度' : '设置为无限额度'}</Button>
  118. <Form.Field>
  119. <Form.Input
  120. label='过期时间'
  121. name='expired_time'
  122. placeholder={'请输入过期时间,格式为 yyyy-MM-dd HH:mm:ss,-1 表示无限制'}
  123. onChange={handleInputChange}
  124. value={expired_time}
  125. autoComplete='new-password'
  126. type='datetime-local'
  127. />
  128. </Form.Field>
  129. <Button type={'button'} onClick={() => {
  130. setExpiredTime(0, 0, 0, 0);
  131. }}>永不过期</Button>
  132. <Button type={'button'} onClick={() => {
  133. setExpiredTime(1, 0, 0, 0);
  134. }}>一个月后过期</Button>
  135. <Button type={'button'} onClick={() => {
  136. setExpiredTime(0, 1, 0, 0);
  137. }}>一天后过期</Button>
  138. <Button type={'button'} onClick={() => {
  139. setExpiredTime(0, 0, 1, 0);
  140. }}>一小时后过期</Button>
  141. <Button type={'button'} onClick={() => {
  142. setExpiredTime(0, 0, 0, 1);
  143. }}>一分钟后过期</Button>
  144. <Button onClick={submit}>提交</Button>
  145. </Form>
  146. </Segment>
  147. </>
  148. );
  149. };
  150. export default EditToken;