import { Outlet, Link, useHref, useNavigate, useLocation, NavLink } from 'react-router-dom'; import { useEffect, useState } from 'react'; import { Layout, Menu, ConfigProvider, theme, Dropdown, Space, Row, Col, Badge, Typography, Modal, Input, Button, App as AntApp } from 'antd'; import { DownOutlined } from '@ant-design/icons'; import 'antd/dist/reset.css'; import AppLogo from '@/assets/logo-gh.png'; import { isEmpty } from '@/utils/commons'; import { appendRequestParams } from '@/utils/request' import Language from '../i18n/LanguageSwitcher'; import { useTranslation } from 'react-i18next'; import zhLocale from 'antd/locale/zh_CN'; import enLocale from 'antd/locale/en_US'; import 'dayjs/locale/zh-cn'; import ErrorBoundary from '@/components/ErrorBoundary' import { BUILD_VERSION, } from '@/config'; import useNoticeStore from '@/stores/Notice'; import useAuthStore from '@/stores/Auth' import { usingStorage } from '@/hooks/usingStorage' const { Header, Content, Footer } = Layout; const { Title } = Typography; function App() { const { t, i18n } = useTranslation(); const [password, setPassword] = useState('') const [validateUserPassword, tokenTimeout] = useAuthStore( (state) => [state.validateUserPassword, state.tokenTimeout]) const { loginToken, userDetail } = usingStorage() const noticeUnRead = useNoticeStore((state) => state.noticeUnRead) const href = useHref() const navigate = useNavigate() const location = useLocation() // 除了路由 /p...以外都需要登陆系统 const needToLogin = href !== '/login' && isEmpty(loginToken) if (!needToLogin) { appendRequestParams('token', loginToken) } useEffect(() => { if (needToLogin) { navigate('/login') } }, [href]) useEffect(() => { window.gtag('event', 'page_view', { page_location: window.location.href }); }, [location]); const onSubmit = () => { validateUserPassword(userDetail?.username, password) .catch(ex => { console.error(ex) alert(t('Validation.LoginFailed')) }) setPassword('') }; const splitPath = href.split('/'); let defaultPath = 'reservation'; if (splitPath.length > 1) { defaultPath = splitPath[1]; } const { token: { colorBgContainer }, } = theme.useToken(); const [antdLng, setAntdLng] = useState(enLocale); useEffect(() => { setAntdLng(i18n.language === 'en' ? enLocale : zhLocale); }, [i18n.language]); return ( {t('LoginTimeout')}
{t('LoginTimeoutTip')}
setPassword(e.target.value)} onPressEnter={onSubmit} addonBefore={userDetail?.username} />
App logo {t('menu.Reservation')} }, { key: 'invoice', label: {t('menu.Invoice')} }, { key: 'feedback', label: {t('menu.Feedback')} }, { key: 'report', label: {t('menu.Report')} }, { key: 'airticket', label: {t('menu.Airticket')} }, { key: 'notice', label: ( {t('menu.Notice')} {noticeUnRead ? : ''} ), }, ]} /> {userDetail?.travelAgencyName} {t('ChangePassword')}, key: '0' }, { label: {t('Profile')}, key: '1' }, { label: {t('account:management.tile')}, key: '3' }, { type: 'divider' }, { label: {t('Logout')}, key: '4' }, ], { type: 'divider' }, { label: <>v{BUILD_VERSION}, key: 'BUILD_VERSION' }, ], }} trigger={['click']} > e.preventDefault()}> {userDetail?.username}
{needToLogin ? <>login... : }
); } export default App