51 lines
1.9 KiB
TypeScript
51 lines
1.9 KiB
TypeScript
/**
|
||
* 客户端环境变量统一入口(Expo 推荐使用 EXPO_PUBLIC_ 前缀)
|
||
*
|
||
* 注意:
|
||
* - 这里读取的是构建时/运行时注入的环境变量(EXPO_PUBLIC_*)
|
||
* - 真机联调时不要用 localhost,改为电脑局域网 IP,例如:http://192.168.1.10:8000
|
||
*/
|
||
|
||
export type AppEnv = 'dev' | 'prod';
|
||
|
||
function getRequiredEnv(name: string): string {
|
||
const value = process.env[name];
|
||
if (!value) {
|
||
throw new Error(`缺少环境变量:${name}(请检查 .env 配置)`);
|
||
}
|
||
return value;
|
||
}
|
||
|
||
function getOptionalEnv(name: string, fallback: string): string {
|
||
return process.env[name] ?? fallback;
|
||
}
|
||
|
||
export type AppRuntimeEnv = 'local' | 'dev' | 'prod';
|
||
|
||
export const APP_ENV = (getOptionalEnv('EXPO_PUBLIC_ENV', 'local') as AppRuntimeEnv) ?? 'local';
|
||
|
||
function getApiBaseUrl(env: AppRuntimeEnv): string {
|
||
// 向后兼容:若直接提供了 EXPO_PUBLIC_API_BASE_URL,则优先使用(不再强制要求 *_DEV/_PROD)
|
||
const direct = process.env.EXPO_PUBLIC_API_BASE_URL;
|
||
if (direct && String(direct).trim()) return String(direct).trim();
|
||
|
||
// 约定:local/dev/prod 三套域名分别配置, 便于后续直接切环境而不改代码
|
||
if (env === 'local') {
|
||
return getOptionalEnv('EXPO_PUBLIC_API_BASE_URL_LOCAL', 'https://api.damer.fun');
|
||
}
|
||
if (env === 'dev') {
|
||
return getOptionalEnv('EXPO_PUBLIC_API_BASE_URL_DEV', getOptionalEnv('EXPO_PUBLIC_API_BASE_URL_LOCAL', 'https://api.damer.fun'));
|
||
}
|
||
return getOptionalEnv('EXPO_PUBLIC_API_BASE_URL_PROD', getOptionalEnv('EXPO_PUBLIC_API_BASE_URL_LOCAL', 'http://localhost:8000'));
|
||
}
|
||
|
||
export const API_BASE_URL = getApiBaseUrl(APP_ENV);
|
||
|
||
/**
|
||
* 默认语言策略:
|
||
* - auto:优先设备语言(支持列表内时),否则回退 en
|
||
* - en/zh-TW:固定默认语言(仍允许用户在设置中手动切换并持久化)
|
||
*/
|
||
export const DEFAULT_LANGUAGE = getOptionalEnv('EXPO_PUBLIC_DEFAULT_LANGUAGE', 'auto');
|
||
|