2026-05-08 10:52:54 +08:00
|
|
|
const API_BASE_STORAGE_KEY = 'x-financial-api-base-url'
|
|
|
|
|
|
|
|
|
|
function normalizeApiBaseUrl(value) {
|
|
|
|
|
return String(value || '/api/v1').replace(/\/$/, '')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function readStoredApiBaseUrl() {
|
|
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
|
return ''
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return window.localStorage.getItem(API_BASE_STORAGE_KEY) || ''
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let runtimeApiBaseUrl = normalizeApiBaseUrl(readStoredApiBaseUrl() || import.meta.env.VITE_API_BASE_URL || '/api/v1')
|
|
|
|
|
|
|
|
|
|
export function setRuntimeApiBaseUrl(value) {
|
|
|
|
|
runtimeApiBaseUrl = normalizeApiBaseUrl(value)
|
|
|
|
|
|
|
|
|
|
if (typeof window !== 'undefined') {
|
|
|
|
|
window.localStorage.setItem(API_BASE_STORAGE_KEY, runtimeApiBaseUrl)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getRuntimeApiBaseUrl() {
|
|
|
|
|
return runtimeApiBaseUrl
|
|
|
|
|
}
|
2026-05-07 11:50:10 +08:00
|
|
|
|
|
|
|
|
function buildUrl(path) {
|
|
|
|
|
if (!path.startsWith('/')) {
|
2026-05-08 10:52:54 +08:00
|
|
|
return `${runtimeApiBaseUrl}/${path}`
|
2026-05-07 11:50:10 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-08 10:52:54 +08:00
|
|
|
return `${runtimeApiBaseUrl}${path}`
|
2026-05-07 11:50:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function apiRequest(path, options = {}) {
|
|
|
|
|
let response
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
response = await fetch(buildUrl(path), {
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
...(options.headers || {})
|
|
|
|
|
},
|
|
|
|
|
...options
|
|
|
|
|
})
|
|
|
|
|
} catch {
|
|
|
|
|
throw new Error('无法连接后端员工服务,请确认 FastAPI 已启动。')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let payload = null
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
payload = await response.json()
|
|
|
|
|
} catch {
|
|
|
|
|
payload = null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(payload?.detail || '接口请求失败,请稍后重试。')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return payload
|
|
|
|
|
}
|