All checks were successful
Build and Deploy Front / deploy (push) Successful in 5m39s
生产用 .env.production 与 pnpm build;测试用 .env.nanafa 与 pnpm build:test;本地 pnpm dev / dev:ideship 可切换。 Co-authored-by: Cursor <cursoragent@cursor.com>
74 lines
2.1 KiB
TypeScript
74 lines
2.1 KiB
TypeScript
/**
|
||
* Admin API 地址(方案 A)
|
||
* - 优先 VITE_API_ADMIN_URL(.env.production / .env.nanafa / .env.ideship 切换)
|
||
* - 未配置且开发:相对路径走 Vite 代理
|
||
* - 未配置且生产构建:默认 https://api.ideship.com/admin
|
||
*/
|
||
|
||
function normalizeBase(base: string): string {
|
||
return base.trim().replace(/\/$/, '')
|
||
}
|
||
|
||
function ensureLeadingSlash(path: string): string {
|
||
return path.startsWith('/') ? path : `/${path}`
|
||
}
|
||
|
||
const DEFAULT_PROD_ADMIN = 'https://api.ideship.com/admin'
|
||
const DEFAULT_TEST_ADMIN = 'http://api.nanafa.com/admin'
|
||
|
||
function configuredAdminBase(): string | undefined {
|
||
const override = import.meta.env.VITE_API_ADMIN_URL as string | undefined
|
||
if (override?.trim()) return normalizeBase(override)
|
||
return undefined
|
||
}
|
||
|
||
function prodAdminBase(): string {
|
||
return configuredAdminBase() ?? DEFAULT_PROD_ADMIN
|
||
}
|
||
|
||
/** 浏览器请求 Admin API(OpenAPI.BASE 或 omsUrl/orderUrl 等) */
|
||
export function resolveAdminApiPath(path: string): string {
|
||
const p = ensureLeadingSlash(path)
|
||
const configured = configuredAdminBase()
|
||
if (configured) {
|
||
return `${configured}${p}`
|
||
}
|
||
if (!import.meta.env.DEV) {
|
||
return `${DEFAULT_PROD_ADMIN}${p}`
|
||
}
|
||
return p
|
||
}
|
||
|
||
export function resolveOpenApiBase(): string {
|
||
const configured = configuredAdminBase()
|
||
if (configured) return configured
|
||
if (!import.meta.env.DEV) {
|
||
return DEFAULT_PROD_ADMIN
|
||
}
|
||
return ''
|
||
}
|
||
|
||
/** 桌面 Agent 等直连 Admin 网关 */
|
||
export function resolveDirectAdminApiUrl(): string {
|
||
const configured = configuredAdminBase()
|
||
if (configured) return configured
|
||
if (import.meta.env.DEV) {
|
||
return DEFAULT_TEST_ADMIN
|
||
}
|
||
return DEFAULT_PROD_ADMIN
|
||
}
|
||
|
||
/** 静态资源(uploads)在 Admin wwwroot,须带 `/admin`。 */
|
||
export function resolveAdminAssetOrigin(): string {
|
||
const override = import.meta.env.VITE_API_ASSET_ORIGIN as string | undefined
|
||
if (override?.trim()) {
|
||
return normalizeBase(override)
|
||
}
|
||
const configured = configuredAdminBase()
|
||
if (configured) return configured
|
||
if (!import.meta.env.DEV) {
|
||
return DEFAULT_PROD_ADMIN
|
||
}
|
||
return ''
|
||
}
|