import {Button, Space, Spin, Typography} from "@douyinfe/semi-ui"; import {useTranslation} from 'react-i18next'; import {useEffect} from "react"; import {useSetState} from "ahooks"; import styled from "@emotion/styled"; import {useNavigate, useSearchParams} from "react-router-dom"; import useConfig from "@/hooks/useConfig.ts"; import AppLogo from "@/assets/AppLogo"; import useAuth from "@/hooks/useAuth.ts"; import {getAppUrl} from "@/hooks/useAppUrl"; const LoginContainer = styled.div({ backgroundColor: '#fff', borderRadius: 10, padding: 40 }) const LogoContainer = styled.div({ display: "flex", alignItems: 'center', justifyContent: 'center' }) const SubmitContainer = styled.div({ textAlign: 'center', width: 300, margin: '70px auto 20px' }) const LoginURL = getAppUrl() + '/login?action=auth' const AuthLogin = () => { const {login, mockLogin} = useAuth(); const navigate = useNavigate(); const [state, setState] = useSetState({ showLogin: false, loading: false }) const {t} = useTranslation(); const {appName} = useConfig() const [query] = useSearchParams() useEffect(() => { const code = query.get('code'), state = query.get('state'); if (query.get('action') == 'auth' && state && code) { setState({loading: true}) // 授权回调 login(code, state).then(() => { navigate('/dashboard') }).catch(() => { // 获取登录凭证失败 重新显示登录按钮 setTimeout(() => { setState({showLogin: true}) }, 2000) }).finally(() => { setTimeout(() => { setState({loading: false}) }, 2000) }) } else { // 正常登录 - 显示登录按钮 setState({ showLogin: true }) } }, []) const handleMockLogin = () => { mockLogin().then(() => { navigate('/dashboard') }) } return ( {appName} {state.loading && Authing ... } {state.showLogin &&
{t('login.submit')}
}
) } export default AuthLogin