Files
mindfulness/client/src/constants/env.ts
2026-02-03 02:04:48 +08:00

51 lines
1.9 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.
/**
* 客户端环境变量统一入口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');