fix(app): 修复视频播放明文HTTP与双网络访问切换
This commit is contained in:
1 parent
b1c69e22af
commit
3d0e0804ca
7 files changed
+67
-64
No files matched your search
@@ -15,6 +15,9 @@ class AppConfig {
|
||||
/// `ipconfig getifaddr en0` 重新确认。默认值只改这一行。
|
||||
static const String devHostBaseUrl = 'http://192.168.31.187:8081';
|
||||
|
||||
/// 办公室内网(Wi-Fi)直连地址,双网络自动切换时的首选。
|
||||
static const String lanBaseUrl = 'http://192.168.31.250:8081';
|
||||
|
||||
static const String _loopbackBaseUrl = 'http://127.0.0.1:8081';
|
||||
|
||||
static const String _overrideBaseUrl = String.fromEnvironment('API_BASE_URL');
|
||||
@@ -59,6 +62,31 @@ class AppConfig {
|
||||
|
||||
static const Duration requestTimeout = Duration(seconds: 8);
|
||||
|
||||
/// 探测内网地址是否可达,可达则切换为内网直连,否则回落到公网地址。
|
||||
///
|
||||
/// Wi-Fi 下内网直连更快;移动数据下内网不可达,自动回落到公网端口映射。
|
||||
static Future<void> probeAndApplyBaseUrl() async {
|
||||
try {
|
||||
final client = HttpClient()
|
||||
..connectionTimeout = const Duration(seconds: 2);
|
||||
final request = await client.getUrl(
|
||||
Uri.parse('$lanBaseUrl/app/categories'),
|
||||
);
|
||||
final response = await request.close().timeout(const Duration(seconds: 2));
|
||||
await response.drain<void>();
|
||||
client.close(force: true);
|
||||
if (response.statusCode == 200) {
|
||||
applyRuntimeBaseUrl(lanBaseUrl);
|
||||
return;
|
||||
}
|
||||
} catch (_) {
|
||||
// 内网不可达,回落到公网。
|
||||
}
|
||||
applyRuntimeBaseUrl(
|
||||
compiledBaseUrl.isNotEmpty ? compiledBaseUrl : defaultBaseUrl,
|
||||
);
|
||||
}
|
||||
|
||||
/// 后端返回的是相对路径(/files/...),这里补上服务地址。
|
||||
static String mediaUrl(String path) {
|
||||
if (path.isEmpty) return '';
|
||||
|
||||
Reference in new issue
Block a user