134 lines
3.4 KiB
Dart
134 lines
3.4 KiB
Dart
/// 环境配置
|
||
enum Environment {
|
||
development,
|
||
staging,
|
||
production,
|
||
}
|
||
|
||
/// 环境配置管理
|
||
class EnvironmentConfig {
|
||
static Environment _currentEnvironment = Environment.development;
|
||
|
||
/// 获取当前环境
|
||
static Environment get current => _currentEnvironment;
|
||
|
||
/// 设置当前环境
|
||
static void setEnvironment(Environment env) {
|
||
_currentEnvironment = env;
|
||
}
|
||
|
||
/// 从字符串设置环境
|
||
static void setEnvironmentFromString(String? envString) {
|
||
switch (envString?.toLowerCase()) {
|
||
case 'production':
|
||
case 'prod':
|
||
_currentEnvironment = Environment.production;
|
||
break;
|
||
case 'staging':
|
||
case 'stage':
|
||
_currentEnvironment = Environment.staging;
|
||
break;
|
||
case 'development':
|
||
case 'dev':
|
||
default:
|
||
_currentEnvironment = Environment.development;
|
||
break;
|
||
}
|
||
}
|
||
|
||
/// 获取当前环境的API基础URL
|
||
static String get baseUrl {
|
||
switch (_currentEnvironment) {
|
||
case Environment.production:
|
||
return 'https://loukao.cn/api/v1';
|
||
case Environment.staging:
|
||
return 'http://localhost:8080/api/v1';
|
||
case Environment.development:
|
||
default:
|
||
// 开发环境:localhost 用于 Web,10.0.2.2 用于 Android 模拟器
|
||
return const String.fromEnvironment(
|
||
'API_BASE_URL',
|
||
defaultValue: 'http://localhost:8080/api/v1',
|
||
);
|
||
}
|
||
}
|
||
|
||
/// 获取环境名称
|
||
static String get environmentName {
|
||
switch (_currentEnvironment) {
|
||
case Environment.production:
|
||
return 'Production';
|
||
case Environment.staging:
|
||
return 'Staging';
|
||
case Environment.development:
|
||
return 'Development';
|
||
}
|
||
}
|
||
|
||
/// 是否为开发环境
|
||
static bool get isDevelopment => _currentEnvironment == Environment.development;
|
||
|
||
/// 是否为生产环境
|
||
static bool get isProduction => _currentEnvironment == Environment.production;
|
||
|
||
/// 是否为预发布环境
|
||
static bool get isStaging => _currentEnvironment == Environment.staging;
|
||
|
||
/// 开发环境配置
|
||
static const Map<String, String> developmentConfig = {
|
||
'baseUrl': 'http://localhost:8080/api/v1',
|
||
'baseUrlAndroid': 'http://10.0.2.2:8080/api/v1',
|
||
'wsUrl': 'ws://localhost:8080/ws',
|
||
};
|
||
|
||
/// 预发布环境配置
|
||
static const Map<String, String> stagingConfig = {
|
||
'baseUrl': 'https://loukao.cn/api/v1',
|
||
'wsUrl': 'ws://your-staging-domain.com/ws',
|
||
};
|
||
|
||
/// 生产环境配置
|
||
static const Map<String, String> productionConfig = {
|
||
'baseUrl': 'https://loukao.cn/api/v1',
|
||
'wsUrl': 'ws://your-production-domain.com/ws',
|
||
};
|
||
|
||
/// 获取当前环境配置
|
||
static Map<String, String> get config {
|
||
switch (_currentEnvironment) {
|
||
case Environment.production:
|
||
return productionConfig;
|
||
case Environment.staging:
|
||
return stagingConfig;
|
||
case Environment.development:
|
||
default:
|
||
return developmentConfig;
|
||
}
|
||
}
|
||
|
||
/// 获取WebSocket URL
|
||
static String get wsUrl {
|
||
return config['wsUrl'] ?? '';
|
||
}
|
||
|
||
/// 获取连接超时时间(毫秒)
|
||
static int get connectTimeout {
|
||
return isProduction ? 10000 : 30000;
|
||
}
|
||
|
||
/// 获取接收超时时间(毫秒)
|
||
static int get receiveTimeout {
|
||
return isProduction ? 10000 : 30000;
|
||
}
|
||
|
||
/// 是否启用日志
|
||
static bool get enableLogging {
|
||
return !isProduction;
|
||
}
|
||
|
||
/// 是否启用调试模式
|
||
static bool get debugMode {
|
||
return isDevelopment;
|
||
}
|
||
}
|