feat: 更新 homeUrl 方法以支持传递应用 logo,增强 URL 生成逻辑
This commit is contained in:
@@ -17,13 +17,14 @@ class AppConfig {
|
|||||||
return _environmentHosts[currentEnvironment] ?? const [];
|
return _environmentHosts[currentEnvironment] ?? const [];
|
||||||
}
|
}
|
||||||
|
|
||||||
static String homeUrl({String? appName}) {
|
static String homeUrl({String? appName, String? appLogo}) {
|
||||||
final host = environmentHosts.isNotEmpty
|
final host = environmentHosts.isNotEmpty
|
||||||
? environmentHosts.first
|
? environmentHosts.first
|
||||||
: 'h5-im.imharry.work';
|
: 'h5-im.imharry.work';
|
||||||
return _withShellBranding(
|
return _withShellBranding(
|
||||||
_normalizeHomeUrl(host),
|
_normalizeHomeUrl(host),
|
||||||
appName: appName,
|
appName: appName,
|
||||||
|
appLogo: appLogo,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,17 +39,31 @@ class AppConfig {
|
|||||||
static String _withShellBranding(
|
static String _withShellBranding(
|
||||||
String url, {
|
String url, {
|
||||||
String? appName,
|
String? appName,
|
||||||
|
String? appLogo,
|
||||||
}) {
|
}) {
|
||||||
final uri = Uri.parse(url);
|
final uri = Uri.parse(url);
|
||||||
final queryParameters = Map<String, String>.from(uri.queryParameters)
|
final queryParameters = Map<String, String>.from(uri.queryParameters)
|
||||||
..['flutter_shell'] = '1'
|
..['flutter_shell'] = '1'
|
||||||
..['shell_cache_bust'] = DateTime.now().millisecondsSinceEpoch.toString();
|
..['shell_cache_bust'] = DateTime.now().millisecondsSinceEpoch.toString();
|
||||||
|
final fragmentParameters = Uri.splitQueryString(uri.fragment);
|
||||||
|
|
||||||
final trimmedName = (appName ?? AppConfig.appName).trim();
|
final trimmedName = (appName ?? AppConfig.appName).trim();
|
||||||
if (trimmedName.isNotEmpty) {
|
if (trimmedName.isNotEmpty) {
|
||||||
queryParameters['shell_app_name'] = trimmedName;
|
queryParameters['shell_app_name'] = trimmedName;
|
||||||
}
|
}
|
||||||
|
|
||||||
return uri.replace(queryParameters: queryParameters).toString();
|
final trimmedLogo = (appLogo ?? AppConfig.appLogo).trim();
|
||||||
|
if (trimmedLogo.isNotEmpty) {
|
||||||
|
fragmentParameters['shell_app_logo'] = trimmedLogo;
|
||||||
|
}
|
||||||
|
|
||||||
|
return uri
|
||||||
|
.replace(
|
||||||
|
queryParameters: queryParameters,
|
||||||
|
fragment: fragmentParameters.isEmpty
|
||||||
|
? null
|
||||||
|
: Uri(queryParameters: fragmentParameters).query,
|
||||||
|
)
|
||||||
|
.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -219,6 +219,7 @@ class _H5ShellPageState extends State<H5ShellPage> with WidgetsBindingObserver {
|
|||||||
WidgetsBinding.instance.addObserver(this);
|
WidgetsBinding.instance.addObserver(this);
|
||||||
_homeUrl = AppConfig.homeUrl(
|
_homeUrl = AppConfig.homeUrl(
|
||||||
appName: widget.shellBranding.appName,
|
appName: widget.shellBranding.appName,
|
||||||
|
appLogo: widget.shellBranding.appLogo,
|
||||||
);
|
);
|
||||||
_controller = _buildController()..loadRequest(Uri.parse(_homeUrl));
|
_controller = _buildController()..loadRequest(Uri.parse(_homeUrl));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import 'package:flutter/widgets.dart';
|
import 'package:flutter/widgets.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:im_webview_app/config/app_config.dart';
|
||||||
import 'package:im_webview_app/main.dart';
|
import 'package:im_webview_app/main.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
@@ -9,4 +10,18 @@ void main() {
|
|||||||
isA<Widget>(),
|
isA<Widget>(),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('passes shell branding name and logo to the H5 URL', () {
|
||||||
|
const logo = 'data:image/png;base64,abc+/=';
|
||||||
|
|
||||||
|
final uri = Uri.parse(
|
||||||
|
AppConfig.homeUrl(appName: 'Shell Test', appLogo: logo),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(uri.queryParameters['flutter_shell'], '1');
|
||||||
|
expect(uri.queryParameters['shell_app_name'], 'Shell Test');
|
||||||
|
expect(uri.queryParameters.containsKey('shell_app_logo'), isFalse);
|
||||||
|
expect(uri.toString(), isNot(contains('%25')));
|
||||||
|
expect(Uri.splitQueryString(uri.fragment)['shell_app_logo'], logo);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user