diff --git a/frontend/mobile-app/lib/core/config/app_config.dart b/frontend/mobile-app/lib/core/config/app_config.dart new file mode 100644 index 00000000..d36de39e --- /dev/null +++ b/frontend/mobile-app/lib/core/config/app_config.dart @@ -0,0 +1,95 @@ +import 'package:flutter/foundation.dart'; + +/// 应用环境 +enum AppEnvironment { + development, + staging, + production, +} + +/// 应用配置 +/// +/// 根据环境变量或编译配置决定 API 地址等配置 +class AppConfig { + static AppConfig? _instance; + + final AppEnvironment environment; + final String apiBaseUrl; + final bool enableLogging; + + AppConfig._({ + required this.environment, + required this.apiBaseUrl, + required this.enableLogging, + }); + + /// 初始化配置 + /// + /// 可通过以下方式指定环境: + /// 1. flutter run --dart-define=ENV=dev + /// 2. 编译时: flutter build apk --dart-define=ENV=prod + static void initialize() { + const envString = String.fromEnvironment('ENV', defaultValue: 'prod'); + + final environment = _parseEnvironment(envString); + + _instance = AppConfig._( + environment: environment, + apiBaseUrl: _getApiBaseUrl(environment), + enableLogging: environment != AppEnvironment.production, + ); + + if (kDebugMode) { + debugPrint('🔧 [AppConfig] Environment: ${_instance!.environment}'); + debugPrint('🔧 [AppConfig] API Base URL: ${_instance!.apiBaseUrl}'); + } + } + + /// 获取配置实例 + static AppConfig get instance { + if (_instance == null) { + initialize(); + } + return _instance!; + } + + /// 解析环境字符串 + static AppEnvironment _parseEnvironment(String env) { + switch (env.toLowerCase()) { + case 'dev': + case 'development': + return AppEnvironment.development; + case 'staging': + case 'stage': + return AppEnvironment.staging; + case 'prod': + case 'production': + default: + return AppEnvironment.production; + } + } + + /// 获取 API 基础地址 + static String _getApiBaseUrl(AppEnvironment env) { + switch (env) { + case AppEnvironment.development: + // Android 模拟器访问本机: 10.0.2.2 + // iOS 模拟器访问本机: localhost + // 真机测试: 使用局域网 IP + return const String.fromEnvironment( + 'API_URL', + defaultValue: 'http://10.0.2.2:3000/api/v1', + ); + case AppEnvironment.staging: + return 'https://rwaapi-dev.szaiai.com/api/v1'; + case AppEnvironment.production: + return 'https://rwaapi.szaiai.com/api/v1'; + } + } + + /// 是否为开发环境 + bool get isDevelopment => environment == AppEnvironment.development; + + /// 是否为生产环境 + bool get isProduction => environment == AppEnvironment.production; +} diff --git a/frontend/mobile-app/lib/core/network/api_client.dart b/frontend/mobile-app/lib/core/network/api_client.dart index 45ff940e..a28c7f03 100644 --- a/frontend/mobile-app/lib/core/network/api_client.dart +++ b/frontend/mobile-app/lib/core/network/api_client.dart @@ -3,6 +3,7 @@ import 'package:flutter/foundation.dart'; import '../storage/secure_storage.dart'; import '../storage/storage_keys.dart'; import '../errors/exceptions.dart'; +import '../config/app_config.dart'; /// API 客户端 /// @@ -30,10 +31,8 @@ class ApiClient { _setupInterceptors(); } - // 生产环境 API 地址 - static const String _defaultBaseUrl = 'https://rwaapi.szaiai.com'; - // 开发环境 (Android 模拟器访问本机): 'http://10.0.2.2:3000' - // 开发环境 (iOS 模拟器访问本机): 'http://localhost:3000' + // 从配置获取 API 地址 + static String get _defaultBaseUrl => AppConfig.instance.apiBaseUrl; /// 设置拦截器 void _setupInterceptors() {