/** * 与 vite `server.proxy` 默认 target 一致;H5 开发无 VITE_US_API_BASE 时用于拼图片等静态资源全路径。 * 修改后端环境时请同步改 vite.config.ts 的 proxy.target。 */ export const US_BACKEND_ORIGIN_FALLBACK = 'http://flus-test.3ffoodsafety.com' /** * 美国版后端 API 根地址(不含末尾 /)。 * - H5 开发:可在 .env.development 留空,配合 vite proxy 走同源 /api * - App / 生产:在 .env 中设置 VITE_US_API_BASE,例如 http://192.168.1.10:19001 */ export function getApiBaseUrl(): string { const fromEnv = (import.meta.env.VITE_US_API_BASE as string | undefined)?.trim() if (fromEnv) return fromEnv.replace(/\/$/, '') if (import.meta.env.DEV && typeof window !== 'undefined') return '' return US_BACKEND_ORIGIN_FALLBACK } /** * 图片等静态资源根(与 API 同域)。H5 开发时 API 仍用相对路径走 Vite /api 代理, * 但 /picture/... 若不加域名会请求到 dev server 导致 404,故在此返回后端根域。 */ export function getStaticMediaOrigin(): string { const apiBase = getApiBaseUrl() if (apiBase) return apiBase if (import.meta.env.DEV && typeof window !== 'undefined') { return US_BACKEND_ORIGIN_FALLBACK } return '' } export function buildApiUrl(path: string): string { const base = getApiBaseUrl() const p = path.startsWith('/') ? path : `/${path}` return base ? `${base}${p}` : p }