front/src/utils/adminApi.ts
zzh 1bc2838def
Some checks failed
Build and Deploy Front / deploy (push) Has been cancelled
fix: test 站点登录强制走 api.test,避免误用正式 API
Dockerfile 误用 production 构建会把 VITE_API_ORIGIN 打成 api.ideship.com;运行时按 hostname 纠正,并支持 FRONT_BUILD_SCRIPT=build:test。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-24 11:24:03 +08:00

80 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Admin API 地址(方案 A)
* - 开发:相对路径,Vite 代理到 Admin
* - 生产/测试:{apiOrigin}/admin(可用 VITE_API_ADMIN_URL 覆盖)
* - 浏览器在 test.ideship.com 时强制 api.test.ideship.com/admin
*/
import { isTestFrontHost, prodApiOrigin } from './rewriteBrowserApiPath'
const DEFAULT_TEST_ADMIN = 'https://api.test.ideship.com/admin'
function normalizeBase(base: string): string {
return base.trim().replace(/\/$/, '')
}
function ensureLeadingSlash(path: string): string {
return path.startsWith('/') ? path : `/${path}`
}
function isBrowserOnTestFront(): boolean {
return typeof window !== 'undefined' && isTestFrontHost(window.location.hostname)
}
function prodAdminBase(): string {
if (isBrowserOnTestFront()) {
return DEFAULT_TEST_ADMIN
}
const override = import.meta.env.VITE_API_ADMIN_URL as string | undefined
if (override?.trim()) return normalizeBase(override)
return `${prodApiOrigin()}/admin`
}
/** 浏览器请求 Admin API(OpenAPI.BASE 或 omsUrl/orderUrl 等) */
export function resolveAdminApiPath(path: string): string {
const p = ensureLeadingSlash(path)
if (!import.meta.env.DEV) {
return `${prodAdminBase()}${p}`
}
return p
}
export function resolveOpenApiBase(): string {
if (!import.meta.env.DEV) {
return prodAdminBase()
}
return ''
}
/** 桌面 Agent 等直连 Admin 网关 */
export function resolveDirectAdminApiUrl(): string {
if (isBrowserOnTestFront()) {
return DEFAULT_TEST_ADMIN
}
const override = import.meta.env.VITE_API_ADMIN_URL as string | undefined
if (override?.trim()) {
return normalizeBase(override)
}
if (import.meta.env.DEV) {
return DEFAULT_TEST_ADMIN
}
return prodAdminBase()
}
/** 静态资源(uploads)在 Admin wwwroot,须带 `/admin`。
* 生产/测试默认 api 域名;开发空串走 Vite `/uploads` 代理。
*/
export function resolveAdminAssetOrigin(): string {
if (isBrowserOnTestFront()) {
return DEFAULT_TEST_ADMIN
}
const override = import.meta.env.VITE_API_ASSET_ORIGIN as string | undefined
if (override?.trim()) {
return normalizeBase(override)
}
if (!import.meta.env.DEV) {
return prodAdminBase()
}
return ''
}