修复接单端过期会话重复请求
This commit is contained in:
@@ -5,6 +5,14 @@ const WORKER_USERNAME_KEY = 'order-site-worker-username'
|
||||
const WORKER_STATUS_KEY = 'order-site-worker-status'
|
||||
const WORKER_DEVICE_ID_KEY = 'order-site-worker-device-id'
|
||||
|
||||
const WORKER_SESSION_STORAGE_KEYS = new Set([
|
||||
WORKER_TOKEN_KEY,
|
||||
WORKER_EXPIRES_AT_KEY,
|
||||
WORKER_ID_KEY,
|
||||
WORKER_USERNAME_KEY,
|
||||
WORKER_STATUS_KEY,
|
||||
])
|
||||
|
||||
export function getWorkerToken() {
|
||||
return localStorage.getItem(WORKER_TOKEN_KEY) || ''
|
||||
}
|
||||
@@ -65,6 +73,27 @@ export function clearWorkerSession() {
|
||||
localStorage.removeItem(WORKER_STATUS_KEY)
|
||||
}
|
||||
|
||||
/** 清理失效会话并通知当前标签页,避免并发请求各自重复跳转。 */
|
||||
export function expireWorkerSession() {
|
||||
const hadSession = Boolean(getWorkerToken() || getWorkerTokenExpiresAt())
|
||||
clearWorkerSession()
|
||||
if (hadSession && typeof window !== 'undefined') {
|
||||
window.dispatchEvent(new Event('worker-session-expired'))
|
||||
}
|
||||
return hadSession
|
||||
}
|
||||
|
||||
export function isWorkerSessionStorageKey(key: string | null) {
|
||||
return Boolean(key && WORKER_SESSION_STORAGE_KEYS.has(key))
|
||||
}
|
||||
|
||||
export function getWorkerSessionExpiryDelay(expiresAt: string, now = Date.now()) {
|
||||
if (!expiresAt) return null
|
||||
const expiresAtMs = Date.parse(expiresAt)
|
||||
if (!Number.isFinite(expiresAtMs)) return null
|
||||
return Math.max(0, expiresAtMs - now)
|
||||
}
|
||||
|
||||
export function hasWorkerSession() {
|
||||
const token = getWorkerToken()
|
||||
const expiresAt = getWorkerTokenExpiresAt()
|
||||
@@ -77,7 +106,6 @@ export function hasWorkerSession() {
|
||||
}
|
||||
|
||||
function isExpired(expiresAt: string) {
|
||||
if (!expiresAt) return false
|
||||
const expiresAtMs = Date.parse(expiresAt)
|
||||
return Number.isFinite(expiresAtMs) && expiresAtMs <= Date.now()
|
||||
const delay = getWorkerSessionExpiryDelay(expiresAt)
|
||||
return delay !== null && delay <= 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user