Files
Flutter_Shell/lib/config/app_config.dart
2026-05-26 22:22:07 +07:00

99 lines
3.0 KiB
Dart

enum Environment {
production,
}
class AppConfig {
static const Environment currentEnvironment = Environment.production;
static const String appName = '本地打包';
static const String appLogo = '';
static const String canonicalWebHost = 'h5-im.imharry.work';
static const Set<String> legacyWebHosts = {
'h5-test.imharry.work',
};
static final Map<Environment, List<String>> _environmentHosts = {
Environment.production: [
canonicalWebHost,
],
};
static List<String> get environmentHosts {
return _environmentHosts[currentEnvironment] ?? const [];
}
static String homeUrl({String? appName, String? appLogo}) {
final host =
environmentHosts.isNotEmpty ? environmentHosts.first : canonicalWebHost;
final uri = Uri.parse(_normalizeHomeUrl(host));
return uri.toString();
}
static String withFreshShellParams(String url) {
return _removeShellParams(Uri.parse(url)).toString();
}
static bool shouldRewriteMainFrameUrl(String url) {
final uri = Uri.tryParse(url);
if (uri == null) {
return false;
}
final host = uri.host.toLowerCase();
return legacyWebHosts.contains(host) || _hasShellParams(uri);
}
static String canonicalizeMainFrameUrl(String url) {
final uri = _removeShellParams(Uri.parse(url));
final host = uri.host.toLowerCase();
if (!legacyWebHosts.contains(host)) {
return uri.toString();
}
return uri
.replace(
scheme: 'https',
host: canonicalWebHost,
)
.toString();
}
static Uri _removeShellParams(Uri uri) {
final queryParameters = Map<String, String>.from(uri.queryParameters);
queryParameters.remove('flutter_shell');
queryParameters.remove('shell_cache_bust');
queryParameters.remove('shell_app_name');
queryParameters.remove('shell_app_logo');
final fragmentParameters = Uri.splitQueryString(uri.fragment);
fragmentParameters.remove('shell_app_name');
fragmentParameters.remove('shell_app_logo');
return uri.replace(
queryParameters: queryParameters,
fragment: fragmentParameters.isEmpty
? ''
: Uri(queryParameters: fragmentParameters).query,
);
}
static bool _hasShellParams(Uri uri) {
final fragmentParameters = Uri.splitQueryString(uri.fragment);
return uri.queryParameters.containsKey('flutter_shell') ||
uri.queryParameters.containsKey('shell_cache_bust') ||
uri.queryParameters.containsKey('shell_app_name') ||
uri.queryParameters.containsKey('shell_app_logo') ||
fragmentParameters.containsKey('shell_app_name') ||
fragmentParameters.containsKey('shell_app_logo');
}
static String _normalizeHomeUrl(String host) {
final value = host.trim();
final normalized =
value.startsWith('http://') || value.startsWith('https://')
? value
: 'https://$value';
final uri = Uri.parse(normalized);
final path = uri.path.isEmpty ? '/' : uri.path;
return uri.replace(path: path).toString();
}
}