feat: complete admin API integration

This commit is contained in:
2026-07-31 13:43:16 +08:00
parent bb09c7d6a0
commit d0f94d95d7
24 changed files with 1834 additions and 113 deletions
+16 -1
View File
@@ -43,14 +43,20 @@ export const http = axios.create({
type RetryableRequestConfig = InternalAxiosRequestConfig & { _authRetry?: boolean }
type AuthRefreshHandler = () => Promise<string>
type AuthSessionExpiredHandler = () => void | Promise<void>
let authRefreshHandler: AuthRefreshHandler | null = null
let authSessionExpiredHandler: AuthSessionExpiredHandler | null = null
let activeRefresh: Promise<string> | null = null
export function registerAuthRefreshHandler(handler: AuthRefreshHandler) {
authRefreshHandler = handler
}
export function registerAuthSessionExpiredHandler(handler: AuthSessionExpiredHandler) {
authSessionExpiredHandler = handler
}
http.interceptors.request.use((config) => {
const token = localStorage.getItem('cc-admin-token')
if (token) {
@@ -71,7 +77,16 @@ http.interceptors.response.use(
config._authRetry = true
try {
if (!activeRefresh) {
activeRefresh = authRefreshHandler().finally(() => { activeRefresh = null })
activeRefresh = authRefreshHandler()
.catch(async (refreshError) => {
try {
await authSessionExpiredHandler?.()
} catch {
// 会话已清理,跳转失败不应覆盖原始认证错误。
}
throw refreshError
})
.finally(() => { activeRefresh = null })
}
const nextToken = await activeRefresh
config.headers.Authorization = `Bearer ${nextToken}`