Files
react-ui/src/pages/Login.tsx

107 lines
4.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import React from 'react'
import { login } from '@/utils/auth.ts'
import { LOGIN_SUCCESS, LOGIN_USERNAME_PASSWORD_ERROR } from '@/constants/Common.constants.ts'
import { setToken } from '@/utils/common.ts'
import '@/assets/css/login.css'
const Login: React.FC = () => {
const [messageApi, contextHolder] = message.useMessage()
const navigate = useNavigate()
const [isLoggingIn, setIsLoggingIn] = useState(false)
const onFinish = (values: LoginForm) => {
setIsLoggingIn(true)
void login(values.username, values.password).then((value) => {
const res = value.data
const { code, data } = res
switch (code) {
case LOGIN_SUCCESS:
setToken(data?.token ?? '')
void messageApi.success('登录成功')
setTimeout(() => {
navigate('/')
}, 1500)
break
case LOGIN_USERNAME_PASSWORD_ERROR:
void messageApi.error(
<>
<strong></strong><strong></strong>
</>
)
setIsLoggingIn(false)
break
default:
void messageApi.error(
<>
<strong></strong>
</>
)
setIsLoggingIn(false)
}
})
}
return (
<>
{contextHolder}
<div className={'login-background'}>
<div className={'login-box'}>
<div className={'login-box-left'}>
<div className={'login-box-left-text'}>
<div></div>
<div>Welcome back</div>
</div>
</div>
<div className={'login-box-right'}>
<div className={'login-from-text'}>
<span>&ensp;</span>
</div>
<AntdForm
name="login-form"
autoComplete="on"
onFinish={onFinish}
className={'login-from'}
>
<AntdForm.Item
className={'login-from-item'}
name={'username'}
rules={[{ required: true, message: '用户名为空' }]}
>
<AntdInput
prefix={<UserOutlined />}
placeholder={'用户名'}
disabled={isLoggingIn}
/>
</AntdForm.Item>
<AntdForm.Item
className={'login-from-item'}
name={'password'}
rules={[{ required: true, message: '密码为空' }]}
>
<AntdInput.Password
prefix={<LockOutlined />}
placeholder={'密码'}
disabled={isLoggingIn}
/>
</AntdForm.Item>
<AntdForm.Item className={'login-from-item'}>
<AntdButton
style={{ width: '100%' }}
type={'primary'}
htmlType={'submit'}
disabled={isLoggingIn}
loading={isLoggingIn}
>
&ensp;&ensp;&ensp;&ensp;
</AntdButton>
</AntdForm.Item>
</AntdForm>
</div>
</div>
</div>
</>
)
}
export default Login