2023-04-22 12:39:27 +00:00
|
|
|
|
import React, { useContext, useEffect, useState } from 'react';
|
2023-07-23 05:25:28 +00:00
|
|
|
|
import { Button, Divider, Form, Grid, Header, Image, Message, Modal, Segment } from 'semantic-ui-react';
|
2023-04-26 03:42:56 +00:00
|
|
|
|
import { Link, useNavigate, useSearchParams } from 'react-router-dom';
|
2023-04-22 12:39:27 +00:00
|
|
|
|
import { UserContext } from '../context/User';
|
2023-09-29 10:07:20 +00:00
|
|
|
|
import { API, getLogo, showError, showSuccess, showWarning } from '../helpers';
|
2024-04-05 04:10:43 +00:00
|
|
|
|
import { onGitHubOAuthClicked, onLarkOAuthClicked } from './utils';
|
2024-08-06 15:54:40 +00:00
|
|
|
|
import { BASE_URL } from '../config';
|
2024-04-05 04:10:43 +00:00
|
|
|
|
import larkIcon from '../images/lark.svg';
|
2023-04-22 12:39:27 +00:00
|
|
|
|
|
|
|
|
|
const LoginForm = () => {
|
|
|
|
|
const [inputs, setInputs] = useState({
|
|
|
|
|
username: '',
|
|
|
|
|
password: '',
|
2023-07-23 05:25:28 +00:00
|
|
|
|
wechat_verification_code: ''
|
2023-04-22 12:39:27 +00:00
|
|
|
|
});
|
2023-04-26 03:42:56 +00:00
|
|
|
|
const [searchParams, setSearchParams] = useSearchParams();
|
2023-04-22 12:39:27 +00:00
|
|
|
|
const [submitted, setSubmitted] = useState(false);
|
|
|
|
|
const { username, password } = inputs;
|
|
|
|
|
const [userState, userDispatch] = useContext(UserContext);
|
|
|
|
|
let navigate = useNavigate();
|
|
|
|
|
const [status, setStatus] = useState({});
|
2023-05-14 11:29:02 +00:00
|
|
|
|
const logo = getLogo();
|
2023-04-22 12:39:27 +00:00
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-07-23 05:25:28 +00:00
|
|
|
|
if (searchParams.get('expired')) {
|
2023-04-26 03:42:56 +00:00
|
|
|
|
showError('未登录或登录已过期,请重新登录!');
|
|
|
|
|
}
|
2023-04-22 12:39:27 +00:00
|
|
|
|
let status = localStorage.getItem('status');
|
|
|
|
|
if (status) {
|
|
|
|
|
status = JSON.parse(status);
|
|
|
|
|
setStatus(status);
|
|
|
|
|
}
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const [showWeChatLoginModal, setShowWeChatLoginModal] = useState(false);
|
|
|
|
|
|
|
|
|
|
const onWeChatLoginClicked = () => {
|
|
|
|
|
setShowWeChatLoginModal(true);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onSubmitWeChatVerificationCode = async () => {
|
|
|
|
|
const res = await API.get(
|
|
|
|
|
`/api/oauth/wechat?code=${inputs.wechat_verification_code}`
|
|
|
|
|
);
|
|
|
|
|
const { success, message, data } = res.data;
|
|
|
|
|
if (success) {
|
|
|
|
|
userDispatch({ type: 'login', payload: data });
|
|
|
|
|
localStorage.setItem('user', JSON.stringify(data));
|
|
|
|
|
navigate('/');
|
|
|
|
|
showSuccess('登录成功!');
|
|
|
|
|
setShowWeChatLoginModal(false);
|
|
|
|
|
} else {
|
|
|
|
|
showError(message);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function handleChange(e) {
|
|
|
|
|
const { name, value } = e.target;
|
|
|
|
|
setInputs((inputs) => ({ ...inputs, [name]: value }));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function handleSubmit(e) {
|
|
|
|
|
setSubmitted(true);
|
|
|
|
|
if (username && password) {
|
2023-07-15 08:06:01 +00:00
|
|
|
|
const res = await API.post(`/api/user/login`, {
|
2023-04-22 12:39:27 +00:00
|
|
|
|
username,
|
2023-07-23 05:25:28 +00:00
|
|
|
|
password
|
2023-04-22 12:39:27 +00:00
|
|
|
|
});
|
|
|
|
|
const { success, message, data } = res.data;
|
|
|
|
|
if (success) {
|
|
|
|
|
userDispatch({ type: 'login', payload: data });
|
|
|
|
|
localStorage.setItem('user', JSON.stringify(data));
|
2023-09-29 10:07:20 +00:00
|
|
|
|
if (username === 'root' && password === '123456') {
|
|
|
|
|
navigate('/user/edit');
|
|
|
|
|
showSuccess('登录成功!');
|
|
|
|
|
showWarning('请立刻修改默认密码!');
|
|
|
|
|
} else {
|
|
|
|
|
navigate('/token');
|
|
|
|
|
showSuccess('登录成功!');
|
|
|
|
|
}
|
2023-04-22 12:39:27 +00:00
|
|
|
|
} else {
|
|
|
|
|
showError(message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2023-07-23 05:25:28 +00:00
|
|
|
|
<Grid textAlign='center' style={{ marginTop: '48px' }}>
|
2023-04-22 12:39:27 +00:00
|
|
|
|
<Grid.Column style={{ maxWidth: 450 }}>
|
2023-07-23 05:25:28 +00:00
|
|
|
|
<Header as='h2' color='' textAlign='center'>
|
2024-08-06 15:54:40 +00:00
|
|
|
|
<Image src={ BASE_URL + logo} /> 用户登录
|
2023-04-22 12:39:27 +00:00
|
|
|
|
</Header>
|
2023-07-23 05:25:28 +00:00
|
|
|
|
<Form size='large'>
|
2023-04-22 12:39:27 +00:00
|
|
|
|
<Segment>
|
|
|
|
|
<Form.Input
|
|
|
|
|
fluid
|
2023-07-23 05:25:28 +00:00
|
|
|
|
icon='user'
|
|
|
|
|
iconPosition='left'
|
2024-03-17 11:39:00 +00:00
|
|
|
|
placeholder='用户名 / 邮箱地址'
|
2023-07-23 05:25:28 +00:00
|
|
|
|
name='username'
|
2023-04-22 12:39:27 +00:00
|
|
|
|
value={username}
|
|
|
|
|
onChange={handleChange}
|
|
|
|
|
/>
|
|
|
|
|
<Form.Input
|
|
|
|
|
fluid
|
2023-07-23 05:25:28 +00:00
|
|
|
|
icon='lock'
|
|
|
|
|
iconPosition='left'
|
|
|
|
|
placeholder='密码'
|
|
|
|
|
name='password'
|
|
|
|
|
type='password'
|
2023-04-22 12:39:27 +00:00
|
|
|
|
value={password}
|
|
|
|
|
onChange={handleChange}
|
|
|
|
|
/>
|
2023-07-23 05:25:28 +00:00
|
|
|
|
<Button color='green' fluid size='large' onClick={handleSubmit}>
|
2023-04-22 12:39:27 +00:00
|
|
|
|
登录
|
|
|
|
|
</Button>
|
|
|
|
|
</Segment>
|
|
|
|
|
</Form>
|
|
|
|
|
<Message>
|
|
|
|
|
忘记密码?
|
2023-07-23 05:25:28 +00:00
|
|
|
|
<Link to='/reset' className='btn btn-link'>
|
2023-04-22 12:39:27 +00:00
|
|
|
|
点击重置
|
|
|
|
|
</Link>
|
|
|
|
|
; 没有账户?
|
2023-07-23 05:25:28 +00:00
|
|
|
|
<Link to='/register' className='btn btn-link'>
|
2023-04-22 12:39:27 +00:00
|
|
|
|
点击注册
|
|
|
|
|
</Link>
|
|
|
|
|
</Message>
|
2024-04-05 04:10:43 +00:00
|
|
|
|
{status.github_oauth || status.wechat_login || status.lark_client_id ? (
|
2023-04-22 12:39:27 +00:00
|
|
|
|
<>
|
|
|
|
|
<Divider horizontal>Or</Divider>
|
2024-04-06 02:18:59 +00:00
|
|
|
|
<div style={{ display: "flex", justifyContent: "center" }}>
|
|
|
|
|
{status.github_oauth ? (
|
|
|
|
|
<Button
|
|
|
|
|
circular
|
|
|
|
|
color='black'
|
|
|
|
|
icon='github'
|
|
|
|
|
onClick={() => onGitHubOAuthClicked(status.github_client_id)}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<></>
|
|
|
|
|
)}
|
|
|
|
|
{status.wechat_login ? (
|
|
|
|
|
<Button
|
|
|
|
|
circular
|
|
|
|
|
color='green'
|
|
|
|
|
icon='wechat'
|
|
|
|
|
onClick={onWeChatLoginClicked}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<></>
|
|
|
|
|
)}
|
|
|
|
|
{status.lark_client_id ? (
|
|
|
|
|
<div style={{
|
2024-08-06 15:54:40 +00:00
|
|
|
|
background: "radial-gradient(circle, #FFFFFF, #FFFFFF, #00D6B9, #2F73FF, #0a3A9C)",
|
|
|
|
|
width: "36px",
|
|
|
|
|
height: "36px",
|
|
|
|
|
borderRadius: "10em",
|
|
|
|
|
display: "flex",
|
|
|
|
|
cursor: "pointer"
|
|
|
|
|
}}
|
2024-04-06 12:08:05 +00:00
|
|
|
|
onClick={() => onLarkOAuthClicked(status.lark_client_id)}
|
|
|
|
|
>
|
2024-04-06 02:18:59 +00:00
|
|
|
|
<Image
|
|
|
|
|
src={larkIcon}
|
|
|
|
|
avatar
|
|
|
|
|
style={{ width: "16px", height: "16px", cursor: "pointer", margin: "auto" }}
|
|
|
|
|
onClick={() => onLarkOAuthClicked(status.lark_client_id)}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<></>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2023-04-22 12:39:27 +00:00
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<></>
|
|
|
|
|
)}
|
|
|
|
|
<Modal
|
|
|
|
|
onClose={() => setShowWeChatLoginModal(false)}
|
|
|
|
|
onOpen={() => setShowWeChatLoginModal(true)}
|
|
|
|
|
open={showWeChatLoginModal}
|
|
|
|
|
size={'mini'}
|
|
|
|
|
>
|
|
|
|
|
<Modal.Content>
|
|
|
|
|
<Modal.Description>
|
|
|
|
|
<Image src={status.wechat_qrcode} fluid />
|
|
|
|
|
<div style={{ textAlign: 'center' }}>
|
|
|
|
|
<p>
|
|
|
|
|
微信扫码关注公众号,输入「验证码」获取验证码(三分钟内有效)
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
2023-07-23 05:25:28 +00:00
|
|
|
|
<Form size='large'>
|
2023-04-22 12:39:27 +00:00
|
|
|
|
<Form.Input
|
|
|
|
|
fluid
|
2023-07-23 05:25:28 +00:00
|
|
|
|
placeholder='验证码'
|
|
|
|
|
name='wechat_verification_code'
|
2023-04-22 12:39:27 +00:00
|
|
|
|
value={inputs.wechat_verification_code}
|
|
|
|
|
onChange={handleChange}
|
|
|
|
|
/>
|
|
|
|
|
<Button
|
2023-07-23 05:25:28 +00:00
|
|
|
|
color=''
|
2023-04-22 12:39:27 +00:00
|
|
|
|
fluid
|
2023-07-23 05:25:28 +00:00
|
|
|
|
size='large'
|
2023-04-22 12:39:27 +00:00
|
|
|
|
onClick={onSubmitWeChatVerificationCode}
|
|
|
|
|
>
|
|
|
|
|
登录
|
|
|
|
|
</Button>
|
|
|
|
|
</Form>
|
|
|
|
|
</Modal.Description>
|
|
|
|
|
</Modal.Content>
|
|
|
|
|
</Modal>
|
|
|
|
|
</Grid.Column>
|
|
|
|
|
</Grid>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default LoginForm;
|