Merge pull request #13425 from dataease/pr@dev-v2@perf_token_refresh

perf(X-Pack): Token 刷新机制弃用时间点比较采用相对时间
This commit is contained in:
fit2cloud-chenyw 2024-11-19 20:15:28 +08:00 committed by GitHub
commit c10de37d29
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 25 additions and 10 deletions

View File

@ -11,12 +11,18 @@ const refreshUrl = '/login/refresh'
const expConstants = 10000
const expTimeConstants = 90000
const isExpired = () => {
const exp = wsCache.get('user.exp')
if (!exp) {
return false
}
return exp - new Date().getTime() < expConstants
const time = wsCache.get('user.time')
if (!time) {
return exp - Date.now() < expConstants
}
return Date.now() - time > expTimeConstants
}
const delayExecute = (token: string) => {
@ -25,10 +31,6 @@ const delayExecute = (token: string) => {
cb(token)
})
requestStore.cleanCacheRequest()
/* cachedRequestList.forEach(cb => {
cb(token)
})
cachedRequestList = [] */
}
const getRefreshStatus = () => {
@ -40,7 +42,6 @@ const setRefreshStatus = (status: boolean) => {
const cacheRequest = cb => {
requestStore.addCacheRequest(cb)
// cachedRequestList.push(cb)
}
export const configHandler = config => {
@ -61,6 +62,7 @@ export const configHandler = config => {
.then(res => {
userStore.setToken(res.data.token)
userStore.setExp(res.data.exp)
userStore.setTime(Date.now())
config.headers['X-DE-TOKEN'] = res.data.token
delayExecute(res.data.token)
})

View File

@ -11,6 +11,7 @@ interface UserState {
oid: string
language: string
exp: number
time: number
}
export const userStore = defineStore('user', {
@ -21,7 +22,8 @@ export const userStore = defineStore('user', {
name: null,
oid: null,
language: 'zh-CN',
exp: null
exp: null,
time: null
}
},
getters: {
@ -42,6 +44,9 @@ export const userStore = defineStore('user', {
},
getExp(): number {
return this.exp
},
getTime(): number {
return this.time
}
},
actions: {
@ -57,7 +62,8 @@ export const userStore = defineStore('user', {
const data = res.data
data.token = wsCache.get('user.token')
data.exp = wsCache.get('user.exp')
const keys: string[] = ['token', 'uid', 'name', 'oid', 'language', 'exp']
data.time = wsCache.get('user.time')
const keys: string[] = ['token', 'uid', 'name', 'oid', 'language', 'exp', 'time']
keys.forEach(key => {
const dkey = key === 'uid' ? 'id' : key
@ -74,6 +80,10 @@ export const userStore = defineStore('user', {
wsCache.set('user.exp', exp)
this.exp = exp
},
setTime(time: number) {
wsCache.set('user.time', time)
this.time = time
},
setUid(uid: string) {
wsCache.set('user.uid', uid)
this.uid = uid
@ -95,7 +105,7 @@ export const userStore = defineStore('user', {
locale.setLang(language)
},
clear() {
const keys: string[] = ['token', 'uid', 'name', 'oid', 'language', 'exp']
const keys: string[] = ['token', 'uid', 'name', 'oid', 'language', 'exp', 'time']
keys.forEach(key => wsCache.delete('user.' + key))
}
}

View File

@ -123,6 +123,7 @@ const handleLogin = () => {
const { token, exp } = res.data
userStore.setToken(token)
userStore.setExp(exp)
userStore.setTime(Date.now())
if (!xpackLoadFail.value && xpackInvalidPwd.value?.invokeMethod) {
const param = {
methodName: 'init'

View File

@ -96,6 +96,7 @@ const onSubmit = async () => {
const { token, exp } = res.data
userStore.setToken(token)
userStore.setExp(exp)
userStore.setTime(Date.now())
router.push({ path: '/index' })
})
.catch(() => {

View File

@ -98,6 +98,7 @@ const switchHandler = (id: number | string) => {
const token = res.data.token
userStore.setToken(token)
userStore.setExp(res.data.exp)
userStore.setTime(Date.now())
window.location.reload()
})
}

@ -1 +1 @@
Subproject commit 4ade726502814434828dc4b6195c35dfeb4d3379
Subproject commit e26306dcac49eb95ba8dc92b4d5e4568f31f0581