diff --git a/soul-admin/src/api/client.ts b/soul-admin/src/api/client.ts index d28af2fd..b975d111 100644 --- a/soul-admin/src/api/client.ts +++ b/soul-admin/src/api/client.ts @@ -9,6 +9,9 @@ import { getAdminToken } from './auth' /** 未设置环境变量时使用的默认 API 地址(零配置部署) */ const DEFAULT_API_BASE = 'https://soulapi.quwanzhi.com' +/** 请求超时(毫秒),避免接口无响应时一直卡在加载中 */ +const REQUEST_TIMEOUT = 15000 + const getBaseUrl = (): string => { const url = import.meta.env.VITE_API_BASE_URL if (typeof url === 'string' && url.length > 0) return url.replace(/\/$/, '') @@ -43,12 +46,15 @@ export async function request( headers.set('Content-Type', 'application/json') } const body = data !== undefined && data !== null ? JSON.stringify(data) : init.body + const controller = new AbortController() + const timeoutId = setTimeout(() => controller.abort(), REQUEST_TIMEOUT) const res = await fetch(url, { ...init, headers, body, credentials: 'include', - }) + signal: init.signal ?? controller.signal, + }).finally(() => clearTimeout(timeoutId)) const contentType = res.headers.get('Content-Type') || '' const json: T = contentType.includes('application/json') ? ((await res.json()) as T)