import React, { useState } from 'react'
import '@/assets/css/pages/sign.scss'
import FitFullscreen from '@/components/common/FitFullscreen'
import FitCenter from '@/components/common/FitCenter'
import FlexBox from '@/components/common/FlexBox'
import { useNavigate } from 'react-router'
import Icon from '@ant-design/icons'
import { getUserInfo, login, setToken } from '@/util/auth.tsx'
import {
PERMISSION_LOGIN_SUCCESS,
PERMISSION_LOGIN_USERNAME_PASSWORD_ERROR,
PERMISSION_USER_DISABLE,
PERMISSION_USERNAME_NOT_FOUND
} from '@/constants/common.constants.ts'
import { AppContext } from '@/App.tsx'
import { utcToLocalTime } from '@/util/datetime.tsx'
import { useUpdatedEffect } from '@/util/hooks.tsx'
const SignUp: React.FC = () => {
const navigate = useNavigate()
const [searchParams] = useSearchParams()
return (
Welcome join us
navigate(
`/login${
searchParams.toString() ? `?${searchParams.toString()}` : ''
}`
)
}
>
登录
)
}
const Forget: React.FC = () => {
const navigate = useNavigate()
const [searchParams] = useSearchParams()
const [isLoading, setIsLoading] = useState(false)
const [isFinish, setIsFinish] = useState(false)
const handleOnFinish = () => {
setIsFinish(true)
}
const handleOnRetry = () => {
setIsFinish(false)
}
return (
{!isFinish ? (
<>
}
placeholder={'邮箱'}
disabled={isLoading}
/>
确 定
>
) : (
我们发送了一封包含找回密码链接的邮件到您的邮箱里,如未收到,可能被归为垃圾邮件,请仔细检查。
重新发送
)}
)
}
const SignIn: React.FC = () => {
const { refreshRouter } = useContext(AppContext)
const navigate = useNavigate()
const [searchParams] = useSearchParams()
const [isSigningIn, setIsSigningIn] = useState(false)
const handleOnFinish = (loginForm: LoginForm) => {
if (isSigningIn) {
return
}
setIsSigningIn(true)
void login(loginForm.account, loginForm.password)
.then((res) => {
const response = res.data
const { code, data } = response
switch (code) {
case PERMISSION_LOGIN_SUCCESS:
setToken(data?.token ?? '')
void message.success('登录成功')
setTimeout(() => {
void getUserInfo().then((user) => {
refreshRouter()
if (searchParams.has('redirect')) {
navigate(searchParams.get('redirect') ?? '/')
} else {
navigate('/')
}
notification.success({
message: '欢迎回来',
description: (
<>
你好 {user.userInfo.nickname}
最近登录:
{user.lastLoginTime
? `${utcToLocalTime(user.lastLoginTime)}【${
user.lastLoginIp
}】`
: '无'}
>
),
placement: 'topRight'
})
})
}, 1500)
break
case PERMISSION_USERNAME_NOT_FOUND:
case PERMISSION_LOGIN_USERNAME_PASSWORD_ERROR:
void message.error(
<>
账号或密码错误,请重试
>
)
setIsSigningIn(false)
break
case PERMISSION_USER_DISABLE:
void message.error(
<>
该用户已被禁用
>
)
setIsSigningIn(false)
break
default:
void message.error(
<>
服务器出错了
>
)
setIsSigningIn(false)
}
})
.catch(() => {
setIsSigningIn(false)
})
}
return (
)
}
const Sign: React.FC = () => {
const lastPage = useRef('none')
const currentPage = useRef('none')
const match = useMatches().reduce((_, second) => second)
const [isSwitch, setIsSwitch] = useState(false)
const leftPage = ['register', 'forget']
useUpdatedEffect(() => {
lastPage.current = currentPage.current
currentPage.current = match.id
setIsSwitch(leftPage.includes(currentPage.current))
}, [match.id])
const leftComponent = () => {
switch (leftPage.includes(currentPage.current) ? currentPage.current : lastPage.current) {
case 'forget':
return
default:
return
}
}
const rightComponent = () => {
switch (leftPage.includes(currentPage.current) ? lastPage.current : currentPage.current) {
default:
return
}
}
return (
<>
{leftComponent()}
{rightComponent()}
>
)
}
export default Sign