import { ApiError } from '@/types/api' const DEFAULT_TIMEOUT = 15000 // 允许外部注入 token 获取函数,避免与 store 循环依赖 let tokenGetter: (() => string | null) | null = null export function setTokenGetter(fn: () => string | null) { tokenGetter = fn } interface RequestOptions extends RequestInit { params?: Record timeout?: number } async function request(url: string, options: RequestOptions = {}): Promise { const token = tokenGetter?.() ?? localStorage.getItem('access_token') const headers: Record = { 'Content-Type': 'application/json', ...(options.headers as Record), } if (token) { headers['Authorization'] = `Bearer ${token}` } // 拼接查询参数 if (options.params) { const query = new URLSearchParams( Object.entries(options.params) .filter(([, v]) => v !== undefined && v !== null && v !== '') .map(([k, v]) => [k, String(v)]) ) const separator = url.includes('?') ? '&' : '?' url = `${url}${separator}${query}` } // eslint-disable-next-line @typescript-eslint/no-unused-vars const { params: _params, timeout: _timeout, ...fetchOptions } = options // 超时控制 const controller = new AbortController() const timeoutId = setTimeout(() => controller.abort(), options.timeout ?? DEFAULT_TIMEOUT) try { const response = await fetch(url, { ...fetchOptions, headers, signal: controller.signal, }) // 401 未授权:保留当前路径作为 redirect 参数 if (response.status === 401) { localStorage.removeItem('access_token') localStorage.removeItem('refresh_token') localStorage.removeItem('user') const redirect = encodeURIComponent( window.location.pathname + window.location.search, ) window.location.href = `/login?redirect=${redirect}` throw new ApiError('登录已过期,请重新登录', 401) } // 防护非 JSON 响应(如网关 HTML 错误页) let result try { result = await response.json() } catch { throw new ApiError('服务器返回了非预期的响应格式', response.status) } // 业务错误 if (result.code !== 0) { throw new ApiError(result.message || '请求失败', result.code) } return result.data } catch (error) { if (error instanceof ApiError) throw error if (error instanceof DOMException && error.name === 'AbortError') { throw new ApiError('请求超时,请检查网络连接', 0) } throw error } finally { clearTimeout(timeoutId) } } export const api = { get: (url: string, params?: Record) => request(url, { method: 'GET', params }), post: (url: string, data?: unknown) => request(url, { method: 'POST', body: JSON.stringify(data) }), put: (url: string, data?: unknown) => request(url, { method: 'PUT', body: JSON.stringify(data) }), patch: (url: string, data?: unknown) => request(url, { method: 'PATCH', body: JSON.stringify(data) }), delete: (url: string) => request(url, { method: 'DELETE' }), }