flutter架 构项目基础
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
/// ============================================================
|
||||
/// 【AppEnv】环境配置 —— 编译期注入(对标 uni-app 的 envList)
|
||||
///
|
||||
/// 一、核心概念:
|
||||
/// 环境是"打包时定死"的维度:测试包连测试地址,正式包连正式地址。
|
||||
/// 所以用 `--dart-define` 在编译期注入,代码里是 const,零运行时开销。
|
||||
///
|
||||
/// 二、怎么用:
|
||||
/// flutter run --dart-define=ENV=dev
|
||||
/// flutter build apk --release --dart-define=ENV=prod
|
||||
/// 不传时默认 prod(日常 flutter run 不用带参数,行为不变)。
|
||||
///
|
||||
/// 三、对标 uni-app(app.config.js envList):
|
||||
/// dev = 开发/本地调试 test = 测试服 prod = 正式服
|
||||
/// 每个环境一套 {baseUrl / gameUrl / reportUrl}。
|
||||
/// ============================================================
|
||||
class AppEnv {
|
||||
AppEnv._();
|
||||
|
||||
/// 环境:dev / test / prod(默认 prod,日常运行零参数)
|
||||
static const String env = String.fromEnvironment('ENV', defaultValue: 'prod');
|
||||
|
||||
/// 品牌标识:aixue / chucai / yule / ...(默认 aixue=爱学蝶变)
|
||||
/// 品牌表见 app_brand.dart;主题/接口/App 名都随它走(AppConfig 聚合)
|
||||
static const String brand =
|
||||
String.fromEnvironment('BRAND', defaultValue: 'aixue');
|
||||
|
||||
/// 三套环境地址表(与 uni-app envList 完全对齐)
|
||||
static const Map<String, Map<String, String>> _envTable = {
|
||||
'dev': {
|
||||
'baseUrl': 'https://k12-student-api-test.23544.com:16400',
|
||||
'gameUrl': 'https://cocos-test.cnzhyx.cn',
|
||||
'reportUrl': 'https://report-test.cnzhyx.cn/',
|
||||
},
|
||||
'test': {
|
||||
'baseUrl': 'https://axdb-test.cnzhyx.cn',
|
||||
'gameUrl': 'https://cocos-test.cnzhyx.cn',
|
||||
'reportUrl': 'https://report-test.cnzhyx.cn/',
|
||||
},
|
||||
'prod': {
|
||||
// 正式环境的 baseUrl 在 AppConfig 里会被品牌专属地址覆盖(同 uni-app)
|
||||
'baseUrl': 'https://ylax-api.cqbzyx.com',
|
||||
'gameUrl': 'https://axdb-cocos.cnzhyx.cn/',
|
||||
'reportUrl': 'https://axdb-report.cnzhyx.cn/',
|
||||
},
|
||||
};
|
||||
|
||||
/// 取当前环境某个 key 的地址(key 不存在返回空串,避免线上崩)
|
||||
static String value(String key) =>
|
||||
_envTable[env]?[key] ?? _envTable['prod']?[key] ?? '';
|
||||
|
||||
/// 当前环境接口地址
|
||||
static String get baseUrl => value('baseUrl');
|
||||
|
||||
/// 当前环境小游戏地址(预留,对标 envList.gameUrl)
|
||||
static String get gameUrl => value('gameUrl');
|
||||
|
||||
/// 当前环境学习报告地址(预留,对标 envList.reportUrl)
|
||||
static String get reportUrl => value('reportUrl');
|
||||
|
||||
/// 是否正式环境(组合规则里判断用:prod 才走品牌专属地址)
|
||||
static bool get isProd => env == 'prod';
|
||||
}
|
||||
Reference in New Issue
Block a user