EditToken.js 5.0 KB

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