|
1 | | -import React, { useEffect, useState } from 'react'; |
| 1 | +import React, { useContext, useEffect, useState } from 'react'; |
2 | 2 | import { Button, Divider, Form, Header, Image, Message, Modal } from 'semantic-ui-react'; |
3 | | -import { Link } from 'react-router-dom'; |
| 3 | +import { Link, useNavigate } from 'react-router-dom'; |
4 | 4 | import { API, copy, showError, showInfo, showNotice, showSuccess } from '../helpers'; |
5 | 5 | import Turnstile from 'react-turnstile'; |
| 6 | +import { UserContext } from '../context/User'; |
6 | 7 |
|
7 | 8 | const PersonalSetting = () => { |
| 9 | + const [userState, userDispatch] = useContext(UserContext); |
| 10 | + let navigate = useNavigate(); |
| 11 | + |
8 | 12 | const [inputs, setInputs] = useState({ |
9 | 13 | wechat_verification_code: '', |
10 | 14 | email_verification_code: '', |
11 | 15 | email: '', |
| 16 | + self_account_deletion_confirmation: '' |
12 | 17 | }); |
13 | 18 | const [status, setStatus] = useState({}); |
14 | 19 | const [showWeChatBindModal, setShowWeChatBindModal] = useState(false); |
15 | 20 | const [showEmailBindModal, setShowEmailBindModal] = useState(false); |
| 21 | + const [showAccountDeleteModal, setShowAccountDeleteModal] = useState(false); |
16 | 22 | const [turnstileEnabled, setTurnstileEnabled] = useState(false); |
17 | 23 | const [turnstileSiteKey, setTurnstileSiteKey] = useState(''); |
18 | 24 | const [turnstileToken, setTurnstileToken] = useState(''); |
@@ -72,6 +78,26 @@ const PersonalSetting = () => { |
72 | 78 | } |
73 | 79 | }; |
74 | 80 |
|
| 81 | + const deleteAccount = async () => { |
| 82 | + if (inputs.self_account_deletion_confirmation !== userState.user.username) { |
| 83 | + showError('请输入你的账户名以确认删除!'); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + const res = await API.delete('/api/user/self'); |
| 88 | + const { success, message } = res.data; |
| 89 | + |
| 90 | + if (success) { |
| 91 | + showSuccess('账户已删除!'); |
| 92 | + await API.get('/api/user/logout'); |
| 93 | + userDispatch({ type: 'logout' }); |
| 94 | + localStorage.removeItem('user'); |
| 95 | + navigate('/login'); |
| 96 | + } else { |
| 97 | + showError(message); |
| 98 | + } |
| 99 | + }; |
| 100 | + |
75 | 101 | const bindWeChat = async () => { |
76 | 102 | if (inputs.wechat_verification_code === '') return; |
77 | 103 | const res = await API.get( |
@@ -139,6 +165,9 @@ const PersonalSetting = () => { |
139 | 165 | </Button> |
140 | 166 | <Button onClick={generateAccessToken}>生成系统访问令牌</Button> |
141 | 167 | <Button onClick={getAffLink}>复制邀请链接</Button> |
| 168 | + <Button onClick={() => { |
| 169 | + setShowAccountDeleteModal(true); |
| 170 | + }}>删除个人账户</Button> |
142 | 171 | <Divider /> |
143 | 172 | <Header as='h3'>账号绑定</Header> |
144 | 173 | { |
@@ -246,6 +275,47 @@ const PersonalSetting = () => { |
246 | 275 | </Modal.Description> |
247 | 276 | </Modal.Content> |
248 | 277 | </Modal> |
| 278 | + <Modal |
| 279 | + onClose={() => setShowAccountDeleteModal(false)} |
| 280 | + onOpen={() => setShowAccountDeleteModal(true)} |
| 281 | + open={showAccountDeleteModal} |
| 282 | + size={'tiny'} |
| 283 | + style={{ maxWidth: '450px' }} |
| 284 | + > |
| 285 | + <Modal.Header>确认删除自己的帐户</Modal.Header> |
| 286 | + <Modal.Content> |
| 287 | + <Modal.Description> |
| 288 | + <Form size='large'> |
| 289 | + <Form.Input |
| 290 | + fluid |
| 291 | + placeholder={`输入你的账户名 ${userState.user.username} 以确认删除`} |
| 292 | + name='self_account_deletion_confirmation' |
| 293 | + value={inputs.self_account_deletion_confirmation} |
| 294 | + onChange={handleInputChange} |
| 295 | + /> |
| 296 | + {turnstileEnabled ? ( |
| 297 | + <Turnstile |
| 298 | + sitekey={turnstileSiteKey} |
| 299 | + onVerify={(token) => { |
| 300 | + setTurnstileToken(token); |
| 301 | + }} |
| 302 | + /> |
| 303 | + ) : ( |
| 304 | + <></> |
| 305 | + )} |
| 306 | + <Button |
| 307 | + color='red' |
| 308 | + fluid |
| 309 | + size='large' |
| 310 | + onClick={deleteAccount} |
| 311 | + loading={loading} |
| 312 | + > |
| 313 | + 删除 |
| 314 | + </Button> |
| 315 | + </Form> |
| 316 | + </Modal.Description> |
| 317 | + </Modal.Content> |
| 318 | + </Modal> |
249 | 319 | </div> |
250 | 320 | ); |
251 | 321 | }; |
|
0 commit comments