import { useState, useEffect } from 'react';
import UserCard from 'ui-component/cards/UserCard';
import {
Card,
Button,
InputLabel,
FormControl,
OutlinedInput,
Stack,
Alert,
Dialog,
DialogTitle,
DialogContent,
DialogActions,
Divider,
SvgIcon
} from '@mui/material';
import Grid from '@mui/material/Unstable_Grid2';
import SubCard from 'ui-component/cards/SubCard';
import { IconBrandWechat, IconBrandGithub, IconMail } from '@tabler/icons-react';
import Label from 'ui-component/Label';
import { API } from 'utils/api';
import { onOidcClicked, showError, showSuccess } from 'utils/common';
import { onGitHubOAuthClicked, onLarkOAuthClicked, copy } from 'utils/common';
import * as Yup from 'yup';
import WechatModal from 'views/Authentication/AuthForms/WechatModal';
import { useSelector } from 'react-redux';
import EmailModal from './component/EmailModal';
import Turnstile from 'react-turnstile';
import { ReactComponent as Lark } from 'assets/images/icons/lark.svg';
import { ReactComponent as OIDC } from 'assets/images/icons/oidc.svg';
const validationSchema = Yup.object().shape({
username: Yup.string().required('用户名 不能为空').min(3, '用户名 不能小于 3 个字符'),
display_name: Yup.string(),
password: Yup.string().test('password', '密码不能小于 8 个字符', (val) => {
return !val || val.length >= 8;
})
});
export default function Profile() {
const [inputs, setInputs] = useState([]);
const [showAccountDeleteModal, setShowAccountDeleteModal] = useState(false);
const [turnstileEnabled, setTurnstileEnabled] = useState(false);
const [turnstileSiteKey, setTurnstileSiteKey] = useState('');
const [turnstileToken, setTurnstileToken] = useState('');
const [openWechat, setOpenWechat] = useState(false);
const [openEmail, setOpenEmail] = useState(false);
const status = useSelector((state) => state.siteInfo);
const handleWechatOpen = () => {
setOpenWechat(true);
};
const handleWechatClose = () => {
setOpenWechat(false);
};
const handleInputChange = (event) => {
let { name, value } = event.target;
setInputs((inputs) => ({ ...inputs, [name]: value }));
};
const loadUser = async () => {
let res = await API.get(`/api/user/self`);
const { success, message, data } = res.data;
if (success) {
setInputs(data);
} else {
showError(message);
}
};
const bindWeChat = async (code) => {
if (code === '') return;
try {
const res = await API.get(`/api/oauth/wechat/bind?code=${code}`);
const { success, message } = res.data;
if (success) {
showSuccess('微信账户绑定成功!');
}
return { success, message };
} catch (err) {
// 请求失败,设置错误信息
return { success: false, message: '' };
}
};
const generateAccessToken = async () => {
const res = await API.get('/api/user/token');
const { success, message, data } = res.data;
if (success) {
setInputs((inputs) => ({ ...inputs, access_token: data }));
copy(data, '访问令牌');
} else {
showError(message);
}
console.log(turnstileEnabled, turnstileSiteKey, status);
};
const submit = async () => {
try {
await validationSchema.validate(inputs);
const res = await API.put(`/api/user/self`, inputs);
const { success, message } = res.data;
if (success) {
showSuccess('用户信息更新成功!');
} else {
showError(message);
}
} catch (err) {
showError(err.message);
}
};
useEffect(() => {
if (status) {
if (status.turnstile_check) {
setTurnstileEnabled(true);
setTurnstileSiteKey(status.turnstile_site_key);
}
}
loadUser().then();
}, [status]);
function getOidcId(){
if (!inputs.oidc_id) return '';
let oidc_id = inputs.oidc_id;
if (inputs.oidc_id.length > 8) {
oidc_id = inputs.oidc_id.slice(0, 6) + '...' + inputs.oidc_id.slice(-6);
}
return oidc_id;
}
return (
<>
用户名
密码
显示名称
{status.wechat_login && !inputs.wechat_id && (
)}
{status.github_oauth && !inputs.github_id && (
)}
{status.lark_client_id && !inputs.lark_id && (
)}
{status.oidc && !inputs.oidc_id && (
)}
{turnstileEnabled ? (
{
setTurnstileToken(token);
}}
/>
) : (
<>>
)}
注意,此处生成的令牌用于系统管理,而非用于请求 OpenAI 相关的服务,请知悉。
{inputs.access_token && (
你的访问令牌是: {inputs.access_token}
请妥善保管。如有泄漏,请立即重置。
)}
{
setOpenEmail(false);
}}
/>
>
);
}