import React, { useState } from 'react'; import { Button, Form, Header, Segment } from 'semantic-ui-react'; import { API, showError, showSuccess, timestamp2string } from '../../helpers'; const AddToken = () => { const originInputs = { name: '', remain_times: -1, expired_time: -1 }; const [inputs, setInputs] = useState(originInputs); const { name, remain_times, expired_time } = inputs; const handleInputChange = (e, { name, value }) => { setInputs((inputs) => ({ ...inputs, [name]: value })); }; const setExpiredTime = (month, day, hour, minute) => { let now = new Date(); let timestamp = now.getTime() / 1000; let seconds = month * 30 * 24 * 60 * 60; seconds += day * 24 * 60 * 60; seconds += hour * 60 * 60; seconds += minute * 60; if (seconds !== 0) { timestamp += seconds; setInputs({ ...inputs, expired_time: timestamp2string(timestamp) }); } else { setInputs({ ...inputs, expired_time: -1 }); } }; const submit = async () => { if (inputs.name === '') return; let localInputs = inputs; localInputs.remain_times = parseInt(localInputs.remain_times); if (localInputs.expired_time !== -1) { let time = Date.parse(localInputs.expired_time); if (isNaN(time)) { showError('过期时间格式错误!'); return; } localInputs.expired_time = Math.ceil(time / 1000); } const res = await API.post(`/api/token/`, localInputs); const { success, message } = res.data; if (success) { showSuccess('令牌创建成功!'); setInputs(originInputs); } else { showError(message); } }; return ( <>
创建新的令牌
); }; export default AddToken;