From ad739a83f8fcc27fb52d23c0d327a9d9ddd38641 Mon Sep 17 00:00:00 2001 From: Developer Date: Wed, 3 Dec 2025 08:41:37 -0800 Subject: [PATCH] =?UTF-8?q?fix(mobile-app):=20=E4=BF=AE=E5=A4=8D=20API=20?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E8=B7=AF=E5=BE=84=E7=BC=BA=E5=B0=91=20/api/v?= =?UTF-8?q?1=20=E5=89=8D=E7=BC=80=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题原因: - 原 ApiClient baseUrl 为 'https://rwaapi.szaiai.com' - 请求路径为 '/user/auto-create' - 实际请求: https://rwaapi.szaiai.com/user/auto-create (错误) - 正确路径: https://rwaapi.szaiai.com/api/v1/user/auto-create 解决方案: - 新增 AppConfig 配置管理类 - 支持多环境配置 (dev/staging/prod) - apiBaseUrl 已包含 /api/v1 前缀 - 支持 --dart-define=ENV=dev 切换环境 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .../lib/core/config/app_config.dart | 95 +++++++++++++++++++ .../lib/core/network/api_client.dart | 7 +- 2 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 frontend/mobile-app/lib/core/config/app_config.dart 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() {