This commit is contained in:
root
2026-06-07 19:18:24 +08:00
parent a1daa040ef
commit 32b900547a
16 changed files with 127 additions and 427 deletions

View File

@@ -10,21 +10,21 @@
* - NEXT_PUBLIC_SITE_URL: 站点根地址
*/
const isDev = process.env.NODE_ENV === "development";
import { IS_DEVELOPMENT, IS_PRODUCTION } from "../runtime-env";
/** 根域名,用于推导默认值。如 nomadro.com、hackrobot.cn */
export const ROOT_DOMAIN =
process.env.ROOT_DOMAIN || process.env.NEXT_PUBLIC_ROOT_DOMAIN || "hackrobot.cn";
/** 支付 API 默认地址。生产环境固定为 api.${ROOT_DOMAIN},避免 .env 中 127.0.0.1 导致支付失败 */
export const DEFAULT_PAYMENT_API_URL = isDev
/** 支付 API 默认地址。运行环境由 APP_ENV / NEXT_PUBLIC_APP_ENV 显式控制 */
export const DEFAULT_PAYMENT_API_URL = IS_DEVELOPMENT
? "http://127.0.0.1:8007"
: `https://api.${ROOT_DOMAIN}`;
/** 生产环境若 PAYMENT_API_URL 指向 localhost则忽略并使用 api 域名 */
/** 线上环境若 PAYMENT_API_URL 指向 localhost则忽略并使用 api 域名 */
export function resolvePaymentApiUrl(): string {
const url = process.env.PAYMENT_API_URL || DEFAULT_PAYMENT_API_URL;
if (!isDev && /^(https?:\/\/)?(127\.0\.0\.1|localhost)(:\d+)?(\/|$)/i.test(url)) {
if (IS_PRODUCTION && /^(https?:\/\/)?(127\.0\.0\.1|localhost)(:\d+)?(\/|$)/i.test(url)) {
return `https://api.${ROOT_DOMAIN}`;
}
return url;
@@ -34,7 +34,7 @@ export function resolvePaymentApiUrl(): string {
export const DEFAULT_POCKETBASE_URL = `https://pocketbase.${ROOT_DOMAIN}`;
/** 站点根地址默认值 */
export const DEFAULT_SITE_URL = isDev
export const DEFAULT_SITE_URL = IS_DEVELOPMENT
? "http://localhost:3001"
: `https://${ROOT_DOMAIN}`;

View File

@@ -5,8 +5,8 @@
*/
import { getThemeConfig } from "./site.config";
import { IS_DEVELOPMENT } from "../runtime-env";
import {
DEFAULT_PAYMENT_API_URL,
resolvePaymentApiUrl,
DEFAULT_POCKETBASE_URL,
DEFAULT_MINIO_HOST,
@@ -64,7 +64,6 @@ export interface ServicesConfig {
minio: MinioConfig;
}
const isDev = process.env.NODE_ENV === "development";
const PAYJSAPI_PORT = process.env.PAYJSAPI_PORT || "8007";
/** 站点标识,用于 VIP 按站点隔离。digital 专题站ai.xx/android.xx各自独立如 digital_ai、digital_android */
@@ -75,7 +74,7 @@ export const VIP_MASTER_SITE_ID = "vip";
export function getApiUrlFromRequest(hostHeader: string | null): string | null {
if (!hostHeader) return null;
if (!isDev) return resolvePaymentApiUrl();
if (!IS_DEVELOPMENT) return resolvePaymentApiUrl();
const hostname = hostHeader.split(":")[0];
return `http://${hostname}:${PAYJSAPI_PORT}`;
}

17
config/runtime-env.ts Normal file
View File

@@ -0,0 +1,17 @@
export type RuntimeEnv = "development" | "production" | "test";
function normalizeRuntimeEnv(value?: string): RuntimeEnv {
const normalized = value?.trim().toLowerCase();
if (normalized === "production" || normalized === "prod") return "production";
if (normalized === "test") return "test";
return "development";
}
export const RUNTIME_ENV = normalizeRuntimeEnv(
process.env.APP_ENV ||
process.env.NEXT_PUBLIC_APP_ENV ||
process.env.NODE_ENV
);
export const IS_DEVELOPMENT = RUNTIME_ENV === "development";
export const IS_PRODUCTION = RUNTIME_ENV === "production";