diff --git a/src/layouts/index.less b/src/layouts/index.less index 77dbd449..1cb23273 100644 --- a/src/layouts/index.less +++ b/src/layouts/index.less @@ -168,13 +168,8 @@ input:-webkit-autofill:active { .ant-layout-content.ant-pro-basicLayout-content.ant-pro-basicLayout-has-header { margin-bottom: 0 !important; -} - -@media (min-width: 768px) { - .ant-layout-content.ant-pro-basicLayout-content.ant-pro-basicLayout-has-header { - min-height: calc(100vh - 72px); - min-height: calc(100vh - var(--vh-offset, 0px) - 72px); - } + min-height: calc(100vh - 72px); + min-height: calc(100vh - var(--vh-offset, 0px) - 72px); } .Resizer { diff --git a/src/layouts/index.tsx b/src/layouts/index.tsx index 6faee78f..94a8a3b8 100644 --- a/src/layouts/index.tsx +++ b/src/layouts/index.tsx @@ -15,11 +15,13 @@ import './index.less'; import vhCheck from 'vh-check'; import { version, changeLog } from '../version'; import { useCtx, useTheme } from '@/utils/hooks'; +import { message } from 'antd'; export default function (props: any) { const ctx = useCtx(); const theme = useTheme(); const [user, setUser] = useState(); + const [loading, setLoading] = useState(true); const logout = () => { request.post(`${config.apiPrefix}logout`).then(() => { @@ -28,30 +30,25 @@ export default function (props: any) { }); }; - const getUser = () => { - request - .get(`${config.apiPrefix}user`) - .then((data) => { - if (data.data.username) { - setUser(data.data); - if (props.location.pathname === '/') { - history.push('/crontab'); - } + const getUser = (needLoading = true) => { + needLoading && setLoading(true); + request.get(`${config.apiPrefix}user`).then(({ code, data }) => { + if (code === 200 && data.username) { + setUser(data); + localStorage.setItem('isLogin', 'true'); + if (props.location.pathname === '/') { + history.push('/crontab'); } - }) - .catch((e) => { - if (e.response && e.response.status === 401) { - localStorage.removeItem(config.authKey); - history.push('/login'); - } - }); + } else { + message.error(data); + } + needLoading && setLoading(false); + }); }; - useEffect(() => { - if (!user) { - getUser(); - } - }, [props.location.pathname]); + const reloadUser = () => { + getUser(false); + }; useEffect(() => { const isAuth = localStorage.getItem(config.authKey); @@ -64,6 +61,12 @@ export default function (props: any) { document.title = '控制面板'; }, []); + useEffect(() => { + if (!user) { + getUser(); + } + }, [props.location.pathname]); + useEffect(() => { const _theme = localStorage.getItem('qinglong_dark_theme') || 'auto'; setFetchMethod(window.fetch); @@ -89,6 +92,7 @@ export default function (props: any) { return ( 控制面板 @@ -136,7 +140,7 @@ export default function (props: any) { ...ctx, ...theme, user, - reloadUser: getUser, + reloadUser, }); })} diff --git a/src/utils/http.ts b/src/utils/http.ts index 5befff04..84c30e6a 100644 --- a/src/utils/http.ts +++ b/src/utils/http.ts @@ -1,6 +1,7 @@ import { extend } from 'umi-request'; import { message } from 'antd'; import config from './config'; +import { history } from 'umi'; message.config({ duration: 1.5, @@ -12,10 +13,17 @@ const errorHandler = function (error: any) { const msg = error.data ? error.data.message || error.data : error.response.statusText; - if (error.response.status !== 401 && error.response.status !== 502) { - message.error(msg); + const responseStatus = error.response.status; + if (responseStatus === 502) { + message.error('服务异常,请手动执行ql check检查服务状态'); + } else if (responseStatus === 401) { + if (history.location.pathname !== '/login') { + message.error('登录已过期,请重新登录'); + localStorage.removeItem(config.authKey); + history.push('/login'); + } } else { - console.log(error.response); + message.error(msg); } } else { console.log(error.message);