提交
This commit is contained in:
1 parent
c3be9d1dce
commit
299609706c
328 files changed
+144899
-13873
No files matched your search
@@ -6,7 +6,6 @@ import axios, { type AxiosRequestConfig, type InternalAxiosRequestConfig } from
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { useUserStore } from '@/store/modules/user'
|
||||
import { fetchRefreshToken } from '@/api/auth'
|
||||
import { applyPortalAuthHeader } from '@/utils/http/portal-auth'
|
||||
|
||||
type RetryableConfig = InternalAxiosRequestConfig & {
|
||||
_retry?: boolean
|
||||
@@ -51,11 +50,40 @@ function onRefreshFailed(error: unknown) {
|
||||
pendingRequests = []
|
||||
}
|
||||
|
||||
dbAxios.interceptors.request.use((config: InternalAxiosRequestConfig) => {
|
||||
/** 从 localStorage 兜底读取 accessToken(兼容 Pinia persist 的版本化 key) */
|
||||
function readTokenFallback(): string {
|
||||
if (typeof localStorage === 'undefined') return ''
|
||||
try {
|
||||
for (const k of Object.keys(localStorage)) {
|
||||
if (!k.startsWith('sys-') || !k.includes('-user')) continue
|
||||
const raw = localStorage.getItem(k)
|
||||
if (!raw) continue
|
||||
const parsed = JSON.parse(raw) as { accessToken?: unknown }
|
||||
const token = parsed?.accessToken
|
||||
if (typeof token === 'string' && token.trim()) return token.trim()
|
||||
}
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
dbAxios.interceptors.request.use(async (config: InternalAxiosRequestConfig) => {
|
||||
if (config.url) {
|
||||
config.url = normalizeDbUrl(config.url)
|
||||
}
|
||||
applyPortalAuthHeader(config)
|
||||
const store = useUserStore()
|
||||
let token = store.accessToken as unknown as string
|
||||
if (!token) {
|
||||
token = readTokenFallback()
|
||||
}
|
||||
if (token) {
|
||||
if (typeof config.headers.set === 'function') {
|
||||
config.headers.set('Authorization', `Bearer ${token}`)
|
||||
} else {
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
}
|
||||
return config
|
||||
})
|
||||
|
||||
@@ -74,7 +102,11 @@ dbAxios.interceptors.response.use(
|
||||
return new Promise((resolve, reject) => {
|
||||
subscribeTokenRefresh(
|
||||
(newToken: string) => {
|
||||
applyPortalAuthHeader(config)
|
||||
if (typeof config.headers.set === 'function') {
|
||||
config.headers.set('Authorization', `Bearer ${newToken}`)
|
||||
} else {
|
||||
config.headers.Authorization = `Bearer ${newToken}`
|
||||
}
|
||||
config._retry = true
|
||||
resolve(dbAxios.request(config))
|
||||
},
|
||||
@@ -89,7 +121,11 @@ dbAxios.interceptors.response.use(
|
||||
const result = await fetchRefreshToken(storedRefreshToken)
|
||||
userStore.setToken(result.token, result.refreshToken)
|
||||
onRefreshed(result.token)
|
||||
applyPortalAuthHeader(config)
|
||||
if (typeof config.headers.set === 'function') {
|
||||
config.headers.set('Authorization', `Bearer ${result.token}`)
|
||||
} else {
|
||||
config.headers.Authorization = `Bearer ${result.token}`
|
||||
}
|
||||
return dbAxios.request(config)
|
||||
} catch (refreshError) {
|
||||
onRefreshFailed(refreshError)
|
||||
|
||||
Reference in new issue
Block a user