import React from 'react'
import { notification } from 'antd'
import '@/assets/css/pages/login.scss'
import { getLocalTime, setToken } from '@/utils/common'
import { getUserInfo, login } from '@/utils/auth'
import {
PERMISSION_LOGIN_SUCCESS,
PERMISSION_LOGIN_USERNAME_PASSWORD_ERROR,
PERMISSION_USER_DISABLE,
PERMISSION_USERNAME_NOT_FOUND
} from '@/constants/common.constants'
const Login: React.FC = () => {
const [messageApi, contextHolder] = message.useMessage()
const navigate = useNavigate()
const [searchParams] = useSearchParams()
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 PERMISSION_LOGIN_SUCCESS:
setToken(data?.token ?? '')
void messageApi.success('登录成功')
setTimeout(() => {
if (searchParams.has('redirect')) {
navigate(searchParams.get('redirect') ?? '/')
} else {
navigate('/')
}
void getUserInfo().then((user) => {
notification.success({
message: '欢迎回来',
description: (
<>
你好 {user.userInfo.nickname}
上次登录:
{getLocalTime(user.lastLoginTime)}【
{user.lastLoginIp}】
>
),
placement: 'topRight'
})
})
}, 1500)
break
case PERMISSION_USERNAME_NOT_FOUND:
case PERMISSION_LOGIN_USERNAME_PASSWORD_ERROR:
void messageApi.error(
<>
用户名或密码错误,请重试
>
)
setIsLoggingIn(false)
break
case PERMISSION_USER_DISABLE:
void messageApi.error(
<>
该用户已被禁用
>
)
setIsLoggingIn(false)
break
default:
void messageApi.error(
<>
服务器出错了
>
)
setIsLoggingIn(false)
}
})
.catch(() => {
setIsLoggingIn(false)
})
}
return (
<>
{contextHolder}