PasswordResetForm.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import React, { useEffect, useState } from 'react';
  2. import { Button, Form, Grid, Header, Image, Segment } from 'semantic-ui-react';
  3. import { API, showError, showInfo, showSuccess } from '../helpers';
  4. import Turnstile from 'react-turnstile';
  5. const PasswordResetForm = () => {
  6. const [inputs, setInputs] = useState({
  7. email: '',
  8. });
  9. const { email } = inputs;
  10. const [loading, setLoading] = useState(false);
  11. const [turnstileEnabled, setTurnstileEnabled] = useState(false);
  12. const [turnstileSiteKey, setTurnstileSiteKey] = useState('');
  13. const [turnstileToken, setTurnstileToken] = useState('');
  14. useEffect(() => {
  15. let status = localStorage.getItem('status');
  16. if (status) {
  17. status = JSON.parse(status);
  18. if (status.turnstile_check) {
  19. setTurnstileEnabled(true);
  20. setTurnstileSiteKey(status.turnstile_site_key);
  21. }
  22. }
  23. }, []);
  24. function handleChange(e) {
  25. const { name, value } = e.target;
  26. setInputs((inputs) => ({ ...inputs, [name]: value }));
  27. }
  28. async function handleSubmit(e) {
  29. if (!email) return;
  30. if (turnstileEnabled && turnstileToken === '') {
  31. showInfo('请稍后几秒重试,Turnstile 正在检查用户环境!');
  32. return;
  33. }
  34. setLoading(true);
  35. const res = await API.get(
  36. `/api/reset_password?email=${email}&turnstile=${turnstileToken}`
  37. );
  38. const { success, message } = res.data;
  39. if (success) {
  40. showSuccess('重置邮件发送成功,请检查邮箱!');
  41. setInputs({ ...inputs, email: '' });
  42. } else {
  43. showError(message);
  44. }
  45. setLoading(false);
  46. }
  47. return (
  48. <Grid textAlign='center' style={{ marginTop: '48px' }}>
  49. <Grid.Column style={{ maxWidth: 450 }}>
  50. <Header as='h2' color='teal' textAlign='center'>
  51. <Image src='/logo.png' /> 密码重置
  52. </Header>
  53. <Form size='large'>
  54. <Segment>
  55. <Form.Input
  56. fluid
  57. icon='mail'
  58. iconPosition='left'
  59. placeholder='邮箱地址'
  60. name='email'
  61. value={email}
  62. onChange={handleChange}
  63. />
  64. {turnstileEnabled ? (
  65. <Turnstile
  66. sitekey={turnstileSiteKey}
  67. onVerify={(token) => {
  68. setTurnstileToken(token);
  69. }}
  70. />
  71. ) : (
  72. <></>
  73. )}
  74. <Button
  75. color='teal'
  76. fluid
  77. size='large'
  78. onClick={handleSubmit}
  79. loading={loading}
  80. >
  81. 提交
  82. </Button>
  83. </Segment>
  84. </Form>
  85. </Grid.Column>
  86. </Grid>
  87. );
  88. };
  89. export default PasswordResetForm;