import React, { useEffect, useState } from 'react'; import { Typography, Input, Form, Button, message, Avatar, Upload } from 'antd'; import { request } from '@/utils/http'; import config from '@/utils/config'; import { history } from '@umijs/max'; import QRCode from 'qrcode.react'; import { PageLoading } from '@ant-design/pro-layout'; import { UploadOutlined, UserOutlined } from '@ant-design/icons'; import ImgCrop from 'antd-img-crop'; import 'antd/es/slider/style'; const { Title, Link } = Typography; const SecuritySettings = ({ user, userChange }: any) => { const [loading, setLoading] = useState(false); const [twoFactorActivated, setTwoFactorActivated] = useState(); const [twoFactoring, setTwoFactoring] = useState(false); const [twoFactorInfo, setTwoFactorInfo] = useState(); const [code, setCode] = useState(); const [avatar, setAvatar] = useState(); const handleOk = (values: any) => { request .put(`${config.apiPrefix}user`, { data: { username: values.username, password: values.password, }, }) .then(({ code, data }) => { if (code === 200) { localStorage.removeItem(config.authKey); history.push('/login'); } }) .catch((error: any) => { console.log(error); }); }; const activeOrDeactiveTwoFactor = () => { if (twoFactorActivated) { deactiveTowFactor(); } else { getTwoFactorInfo(); setTwoFactoring(true); } }; const deactiveTowFactor = () => { request .put(`${config.apiPrefix}user/two-factor/deactive`) .then(({ code, data }) => { if (code === 200 && data) { setTwoFactorActivated(false); userChange(); } }) .catch((error: any) => { console.log(error); }); }; const completeTowFactor = () => { setLoading(true); request .put(`${config.apiPrefix}user/two-factor/active`, { data: { code } }) .then(({ code, data }) => { if (code === 200) { if (data) { message.success('激活成功'); setTwoFactoring(false); setTwoFactorActivated(true); userChange(); } else { message.success('验证失败'); } } }) .catch((error: any) => { console.log(error); }) .finally(() => setLoading(false)); }; const getTwoFactorInfo = () => { request .get(`${config.apiPrefix}user/two-factor/init`) .then(({ code, data }) => { if (code === 200) { setTwoFactorInfo(data); } }) .catch((error: any) => { console.log(error); }); }; const onChange = (e) => { if (e.file && e.file.response) { setAvatar(`/api/static/${e.file.response.data}`); userChange(); } }; useEffect(() => { setTwoFactorActivated(user && user.twoFactorActivated); setAvatar(user.avatar && `/api/static/${user.avatar}`); }, [user]); return twoFactoring ? ( <> {twoFactorInfo ? (
第一步 下载两步验证手机应用,比如 Google Authenticator 、 Microsoft Authenticator 、 Authy 、 1Password 、 LastPass Authenticator 第二步 使用手机应用扫描二维码,或者输入秘钥 {twoFactorInfo?.secret}
第三步 输入手机应用上的6位数字 setCode(e.target.value)} placeholder="123456" />
) : ( )} ) : ( <>
修改用户名密码
两步验证
头像
} src={avatar} /> ); }; export default SecuritySettings;