sync: soul-admin 前端 | 原因: 前端代码修改
This commit is contained in:
@@ -9,6 +9,9 @@ import { getAdminToken } from './auth'
|
|||||||
/** 未设置环境变量时使用的默认 API 地址(零配置部署) */
|
/** 未设置环境变量时使用的默认 API 地址(零配置部署) */
|
||||||
const DEFAULT_API_BASE = 'https://soulapi.quwanzhi.com'
|
const DEFAULT_API_BASE = 'https://soulapi.quwanzhi.com'
|
||||||
|
|
||||||
|
/** 请求超时(毫秒),避免接口无响应时一直卡在加载中 */
|
||||||
|
const REQUEST_TIMEOUT = 15000
|
||||||
|
|
||||||
const getBaseUrl = (): string => {
|
const getBaseUrl = (): string => {
|
||||||
const url = import.meta.env.VITE_API_BASE_URL
|
const url = import.meta.env.VITE_API_BASE_URL
|
||||||
if (typeof url === 'string' && url.length > 0) return url.replace(/\/$/, '')
|
if (typeof url === 'string' && url.length > 0) return url.replace(/\/$/, '')
|
||||||
@@ -43,12 +46,15 @@ export async function request<T = unknown>(
|
|||||||
headers.set('Content-Type', 'application/json')
|
headers.set('Content-Type', 'application/json')
|
||||||
}
|
}
|
||||||
const body = data !== undefined && data !== null ? JSON.stringify(data) : init.body
|
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, {
|
const res = await fetch(url, {
|
||||||
...init,
|
...init,
|
||||||
headers,
|
headers,
|
||||||
body,
|
body,
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
})
|
signal: init.signal ?? controller.signal,
|
||||||
|
}).finally(() => clearTimeout(timeoutId))
|
||||||
const contentType = res.headers.get('Content-Type') || ''
|
const contentType = res.headers.get('Content-Type') || ''
|
||||||
const json: T = contentType.includes('application/json')
|
const json: T = contentType.includes('application/json')
|
||||||
? ((await res.json()) as T)
|
? ((await res.json()) as T)
|
||||||
|
|||||||
Reference in New Issue
Block a user