EditToken.js 5.5 KB

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