From 4c1855d65b8cff099b4bc069748a2de2fb309448 Mon Sep 17 00:00:00 2001 From: Jimmy Liow Date: Thu, 5 Sep 2024 10:42:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8F=96=E6=B6=88=E4=BB=A4=E7=89=8C?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E8=BF=87=E6=9C=9F=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/stores/Auth.js | 27 +----------- src/views/App.jsx | 83 +++++++++++------------------------ src/views/account/Profile.jsx | 10 ++--- 3 files changed, 33 insertions(+), 87 deletions(-) diff --git a/src/stores/Auth.js b/src/stores/Auth.js index feab61c..07e0554 100644 --- a/src/stores/Auth.js +++ b/src/stores/Auth.js @@ -37,6 +37,7 @@ export const fetchPermissionListByUserId = async (userId) => { return errcode !== 0 ? {} : result } +// 取消令牌时间过期检测,待删除 async function fetchLastRequet() { const { errcode, result } = await fetchJSON(`${HT_HOST}/service-CooperateSOA/GetLastReqDate`) return errcode !== 0 ? {} : result @@ -44,7 +45,6 @@ async function fetchLastRequet() { const initialState = { tokenInterval: null, - tokenTimeout: import.meta.env.PROD ? true : false, loginStatus: 0, defaltRoute: '', currentUser: { @@ -62,7 +62,7 @@ const useAuthStore = create(devtools((set, get) => ({ ...initialState, initAuth: async () => { - const { startTokenInterval, loadUserPermission } = get() + const { loadUserPermission } = get() const { setStorage, loginToken } = usingStorage() // Dev 模式使用 localStorage,会有 token 失效情况,需要手动删除 @@ -87,7 +87,6 @@ const useAuthStore = create(devtools((set, get) => ({ } })) - startTokenInterval() loadPageSpy(userJson.real_name) }, @@ -102,7 +101,6 @@ const useAuthStore = create(devtools((set, get) => ({ await initAuth() set(() => ({ - tokenTimeout: false, loginStatus: 302 })) }, @@ -139,27 +137,6 @@ const useAuthStore = create(devtools((set, get) => ({ })) }, - startTokenInterval: () => { - const { logout } = get() - - async function checkTokenTimeout() { - // TODO:Token 失效后要跳转到登录页面 - const { LastReqDate } = await fetchLastRequet() - const lastReqDateTime = new Date(LastReqDate).getTime() - const now = new Date() - const diffTime = now.getTime() - lastReqDateTime - const diffHours = diffTime/1000/60/60 - if (diffHours > 1) { - logout() - } - } - - const interval = setInterval(() => checkTokenTimeout(), 1000*60*10) - set(() => ({ - tokenInterval: interval - })) - }, - // TODO: 迁移到 Account.js changeUserPassword: (password, newPassword) => { const { userId } = usingStorage() diff --git a/src/views/App.jsx b/src/views/App.jsx index 2ee9b45..cab3e4a 100644 --- a/src/views/App.jsx +++ b/src/views/App.jsx @@ -1,27 +1,26 @@ -import { Outlet, Link, useHref, useNavigate, 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 { 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 { useThemeContext } from '@/stores/ThemeContext'; -import { usingStorage } from '@/hooks/usingStorage'; -import { useDefaultLgc } from '@/i18n/LanguageSwitcher'; +import { Outlet, Link, useHref, useNavigate, NavLink } from 'react-router-dom' +import { useEffect, useState } from 'react' +import { Layout, Menu, ConfigProvider, theme, Dropdown, Space, Row, Col, Badge, 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 { 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 { useThemeContext } from '@/stores/ThemeContext' +import { usingStorage } from '@/hooks/usingStorage' +import { useDefaultLgc } from '@/i18n/LanguageSwitcher' import { appendRequestParams } from '@/utils/request' -import { PERM_ACCOUNT_MANAGEMENT, PERM_ROLE_NEW, PERM_OVERSEA, PERM_AIR_TICKET, PERM_PRODUCTS_MANAGEMENT } from '@/config'; +import { PERM_ACCOUNT_MANAGEMENT, PERM_ROLE_NEW, PERM_OVERSEA, PERM_AIR_TICKET, PERM_PRODUCTS_MANAGEMENT } from '@/config' -const { Header, Content, Footer } = Layout; -const { Title } = Typography; +const { Header, Content, Footer } = Layout function App() { @@ -29,10 +28,8 @@ function App() { const { colorPrimary } = useThemeContext() - const [password, setPassword] = useState('') - - const [authenticate, tokenTimeout, isPermitted, currentUser] = useAuthStore( - (state) => [state.authenticate, state.tokenTimeout, state.isPermitted, state.currentUser]) + const [isPermitted, currentUser] = useAuthStore( + (state) => [state.isPermitted, state.currentUser]) const { loginToken } = usingStorage() @@ -48,15 +45,6 @@ function App() { } }, [href]) - const onSubmit = () => { - authenticate(currentUser?.username, password) - .catch(ex => { - console.error(ex) - alert(t('Validation.LoginFailed')) - }) - setPassword('') - } - const splitPath = href.split('/') let defaultPath = 'notice' @@ -64,11 +52,11 @@ function App() { defaultPath = splitPath[1] } - const { language } = useDefaultLgc(); - const [antdLng, setAntdLng] = useState(enLocale); + const { language } = useDefaultLgc() + const [antdLng, setAntdLng] = useState(enLocale) useEffect(() => { - setAntdLng(i18n.language === 'en' ? enLocale : zhLocale); - appendRequestParams('lgc', language); + setAntdLng(i18n.language === 'en' ? enLocale : zhLocale) + appendRequestParams('lgc', language) }, [i18n.language]) return ( @@ -83,25 +71,6 @@ function App() { }}> - - {t('LoginTimeout')} -
{t('LoginTimeoutTip')}
- - setPassword(e.target.value)} - onPressEnter={onSubmit} - addonBefore={currentUser?.username} /> - - -
diff --git a/src/views/account/Profile.jsx b/src/views/account/Profile.jsx index d97b609..590b439 100644 --- a/src/views/account/Profile.jsx +++ b/src/views/account/Profile.jsx @@ -1,6 +1,6 @@ -import { Descriptions, Col, Row } from 'antd'; -import { useTranslation } from 'react-i18next'; -import useAuthStore from '@/stores/Auth'; +import { Descriptions, Col, Row } from 'antd' +import { useTranslation } from 'react-i18next' +import useAuthStore from '@/stores/Auth' function Profile() { @@ -12,7 +12,7 @@ function Profile() { {currentUser?.username} - {currentUser?.realname}({currentUser?.rolesName}) + {currentUser?.realname} {currentUser?.emailAddress} {currentUser?.travelAgencyName} @@ -21,4 +21,4 @@ function Profile() { ); } -export default Profile; +export default Profile