feat: 取消令牌时间过期检测

main
Jimmy Liow 10 months ago
parent 5a4a3f0214
commit 4c1855d65b

@ -37,6 +37,7 @@ export const fetchPermissionListByUserId = async (userId) => {
return errcode !== 0 ? {} : result return errcode !== 0 ? {} : result
} }
// 取消令牌时间过期检测,待删除
async function fetchLastRequet() { async function fetchLastRequet() {
const { errcode, result } = await fetchJSON(`${HT_HOST}/service-CooperateSOA/GetLastReqDate`) const { errcode, result } = await fetchJSON(`${HT_HOST}/service-CooperateSOA/GetLastReqDate`)
return errcode !== 0 ? {} : result return errcode !== 0 ? {} : result
@ -44,7 +45,6 @@ async function fetchLastRequet() {
const initialState = { const initialState = {
tokenInterval: null, tokenInterval: null,
tokenTimeout: import.meta.env.PROD ? true : false,
loginStatus: 0, loginStatus: 0,
defaltRoute: '', defaltRoute: '',
currentUser: { currentUser: {
@ -62,7 +62,7 @@ const useAuthStore = create(devtools((set, get) => ({
...initialState, ...initialState,
initAuth: async () => { initAuth: async () => {
const { startTokenInterval, loadUserPermission } = get() const { loadUserPermission } = get()
const { setStorage, loginToken } = usingStorage() const { setStorage, loginToken } = usingStorage()
// Dev 模式使用 localStorage会有 token 失效情况,需要手动删除 // Dev 模式使用 localStorage会有 token 失效情况,需要手动删除
@ -87,7 +87,6 @@ const useAuthStore = create(devtools((set, get) => ({
} }
})) }))
startTokenInterval()
loadPageSpy(userJson.real_name) loadPageSpy(userJson.real_name)
}, },
@ -102,7 +101,6 @@ const useAuthStore = create(devtools((set, get) => ({
await initAuth() await initAuth()
set(() => ({ set(() => ({
tokenTimeout: false,
loginStatus: 302 loginStatus: 302
})) }))
}, },
@ -139,27 +137,6 @@ const useAuthStore = create(devtools((set, get) => ({
})) }))
}, },
startTokenInterval: () => {
const { logout } = get()
async function checkTokenTimeout() {
// TODOToken 失效后要跳转到登录页面
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 // TODO: 迁移到 Account.js
changeUserPassword: (password, newPassword) => { changeUserPassword: (password, newPassword) => {
const { userId } = usingStorage() const { userId } = usingStorage()

@ -1,27 +1,26 @@
import { Outlet, Link, useHref, useNavigate, NavLink } from 'react-router-dom'; import { Outlet, Link, useHref, useNavigate, NavLink } from 'react-router-dom'
import { useEffect, useState } from 'react'; 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 { Layout, Menu, ConfigProvider, theme, Dropdown, Space, Row, Col, Badge, App as AntApp } from 'antd'
import { DownOutlined } from '@ant-design/icons'; import { DownOutlined } from '@ant-design/icons'
import 'antd/dist/reset.css'; import 'antd/dist/reset.css'
import AppLogo from '@/assets/logo-gh.png'; import AppLogo from '@/assets/logo-gh.png'
import { isEmpty } from '@/utils/commons'; import { isEmpty } from '@/utils/commons'
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next'
import zhLocale from 'antd/locale/zh_CN'; import zhLocale from 'antd/locale/zh_CN'
import enLocale from 'antd/locale/en_US'; import enLocale from 'antd/locale/en_US'
import 'dayjs/locale/zh-cn'; import 'dayjs/locale/zh-cn'
import ErrorBoundary from '@/components/ErrorBoundary'; import ErrorBoundary from '@/components/ErrorBoundary'
import { BUILD_VERSION, } from '@/config'; import { BUILD_VERSION, } from '@/config'
import useNoticeStore from '@/stores/Notice'; import useNoticeStore from '@/stores/Notice'
import useAuthStore from '@/stores/Auth'; import useAuthStore from '@/stores/Auth'
import { useThemeContext } from '@/stores/ThemeContext'; import { useThemeContext } from '@/stores/ThemeContext'
import { usingStorage } from '@/hooks/usingStorage'; import { usingStorage } from '@/hooks/usingStorage'
import { useDefaultLgc } from '@/i18n/LanguageSwitcher'; import { useDefaultLgc } from '@/i18n/LanguageSwitcher'
import { appendRequestParams } from '@/utils/request' 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 { Header, Content, Footer } = Layout
const { Title } = Typography;
function App() { function App() {
@ -29,10 +28,8 @@ function App() {
const { colorPrimary } = useThemeContext() const { colorPrimary } = useThemeContext()
const [password, setPassword] = useState('') const [isPermitted, currentUser] = useAuthStore(
(state) => [state.isPermitted, state.currentUser])
const [authenticate, tokenTimeout, isPermitted, currentUser] = useAuthStore(
(state) => [state.authenticate, state.tokenTimeout, state.isPermitted, state.currentUser])
const { loginToken } = usingStorage() const { loginToken } = usingStorage()
@ -48,15 +45,6 @@ function App() {
} }
}, [href]) }, [href])
const onSubmit = () => {
authenticate(currentUser?.username, password)
.catch(ex => {
console.error(ex)
alert(t('Validation.LoginFailed'))
})
setPassword('')
}
const splitPath = href.split('/') const splitPath = href.split('/')
let defaultPath = 'notice' let defaultPath = 'notice'
@ -64,11 +52,11 @@ function App() {
defaultPath = splitPath[1] defaultPath = splitPath[1]
} }
const { language } = useDefaultLgc(); const { language } = useDefaultLgc()
const [antdLng, setAntdLng] = useState(enLocale); const [antdLng, setAntdLng] = useState(enLocale)
useEffect(() => { useEffect(() => {
setAntdLng(i18n.language === 'en' ? enLocale : zhLocale); setAntdLng(i18n.language === 'en' ? enLocale : zhLocale)
appendRequestParams('lgc', language); appendRequestParams('lgc', language)
}, [i18n.language]) }, [i18n.language])
return ( return (
@ -83,25 +71,6 @@ function App() {
}}> }}>
<AntApp> <AntApp>
<ErrorBoundary> <ErrorBoundary>
<Modal
centered
closable={false}
maskClosable={false}
footer={null}
open={tokenTimeout}
>
<Title level={3}>{t('LoginTimeout')}</Title>
<div>{t('LoginTimeoutTip')}</div>
<Space direction='horizontal'>
<Input.Password value={password}
onChange={(e) => setPassword(e.target.value)}
onPressEnter={onSubmit}
addonBefore={currentUser?.username} />
<Button
onClick={onSubmit}
>{t('Submit')}</Button></Space>
</Modal>
<Layout className='min-h-screen'> <Layout className='min-h-screen'>
<Header className='sticky top-0 z-10 w-full'> <Header className='sticky top-0 z-10 w-full'>
<Row gutter={{ md: 24 }} justify='end' align='middle'> <Row gutter={{ md: 24 }} justify='end' align='middle'>

@ -1,6 +1,6 @@
import { Descriptions, Col, Row } from 'antd'; import { Descriptions, Col, Row } from 'antd'
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next'
import useAuthStore from '@/stores/Auth'; import useAuthStore from '@/stores/Auth'
function Profile() { function Profile() {
@ -12,7 +12,7 @@ function Profile() {
<Col span={12} offset={6}> <Col span={12} offset={6}>
<Descriptions title={t('userProfile')} layout="vertical" column={2}> <Descriptions title={t('userProfile')} layout="vertical" column={2}>
<Descriptions.Item label={t("Username")}>{currentUser?.username}</Descriptions.Item> <Descriptions.Item label={t("Username")}>{currentUser?.username}</Descriptions.Item>
<Descriptions.Item label={t("Realname")}>{currentUser?.realname}({currentUser?.rolesName})</Descriptions.Item> <Descriptions.Item label={t("Realname")}>{currentUser?.realname}</Descriptions.Item>
<Descriptions.Item label={t("Email")}>{currentUser?.emailAddress}</Descriptions.Item> <Descriptions.Item label={t("Email")}>{currentUser?.emailAddress}</Descriptions.Item>
<Descriptions.Item label={t("Company")}>{currentUser?.travelAgencyName}</Descriptions.Item> <Descriptions.Item label={t("Company")}>{currentUser?.travelAgencyName}</Descriptions.Item>
</Descriptions> </Descriptions>
@ -21,4 +21,4 @@ function Profile() {
); );
} }
export default Profile; export default Profile

Loading…
Cancel
Save