服务器候选列表改为构建注入(API_FALLBACK_URLS):测试包只连本地零兜底,生产包域名+IP同后端;彻底隔离测试/生产;地址记忆改为会话内(冷启动永远从默认地址重试)

This commit is contained in:
2026-11-22 13:31:28 +08:00
committed by ceryuan
parent e497643bd4
commit 35ae9d384c
6 changed files with 195 additions and 159 deletions
-19
View File
@@ -8,7 +8,6 @@ import 'package:go_router/go_router.dart';
import '../../core/network/api_client.dart';
import '../../core/theme/app_colors.dart';
import '../auth/auth_controller.dart';
import '../misc/app_updater.dart';
class SplashPage extends ConsumerStatefulWidget {
const SplashPage({super.key});
@@ -32,23 +31,6 @@ class _SplashPageState extends ConsumerState<SplashPage> {
setState(() => _checkedConfig = true);
}
Future<void> _checkUpdateStable() async {
await Future<void>.delayed(const Duration(milliseconds: 800));
if (!mounted) return;
final upd = await checkUpdate(ref.read(apiClientProvider));
if (upd == null || !mounted) return;
await showDialog(
context: context,
barrierDismissible: upd['force'] != true,
builder: (_) => UpdateDialog(
latest: upd['latest'] as String,
notes: upd['notes'] as String,
url: upd['url'] as String,
sizeMb: ((upd['size'] as int) / 1048576).round(),
force: upd['force'] == true,
),
);
}
@override
Widget build(BuildContext context) {
final auth = ref.watch(authProvider);
@@ -60,7 +42,6 @@ class _SplashPageState extends ConsumerState<SplashPage> {
} else {
context.go('/login');
}
_checkUpdateStable();
});
}
return Scaffold(
+170 -119
View File
@@ -28,6 +28,176 @@ int compareVersion(String a, String b) {
return 0;
}
/// 全局升级蒙版状态:非 null 时在 App 最顶层盖住一切(登录页/首页均被覆盖),
/// 必须处理(立即更新 或 非强制时的稍后再说)。
final ValueNotifier<Map<String, dynamic>?> updateGate = ValueNotifier(null);
/// 蒙版式升级层:挂在 MaterialApp.builder 最顶层,路由跳转不影响。
class UpdateGateOverlay extends StatelessWidget {
const UpdateGateOverlay({super.key, required this.child});
final Widget child;
@override
Widget build(BuildContext context) {
return ValueListenableBuilder<Map<String, dynamic>?>(
valueListenable: updateGate,
builder: (context, info, _) {
if (info == null) return child;
return Stack(
children: [
child,
Positioned.fill(child: UpdateGateCard(info: info)),
],
);
},
);
}
}
class UpdateGateCard extends StatefulWidget {
const UpdateGateCard({super.key, required this.info});
final Map<String, dynamic> info;
@override
State<UpdateGateCard> createState() => _UpdateGateCardState();
}
class _UpdateGateCardState extends State<UpdateGateCard> {
double _progress = -1; // -1 未开始;0~1 下载中;2 已拉起安装器
String? _err;
bool get _force => widget.info['force'] == true;
bool get _downloading => _progress >= 0 && _progress < 2;
Future<void> _downloadAndInstall() async {
setState(() => _err = null);
try {
final dir = await getTemporaryDirectory();
final filePath =
'${dir.path}/xuanlong-${widget.info['latest']}.apk';
setState(() => _progress = 0);
await Dio().download(
'${activeBaseUrl.value}${widget.info['url']}',
filePath,
onReceiveProgress: (n, total) {
if (total > 0) setState(() => _progress = n / total);
},
);
setState(() => _progress = 2);
final r = await OpenFilex.open(filePath,
type: 'application/vnd.android.package-archive');
if (r.type != ResultType.done) {
setState(() => _err = '无法拉起安装器(${r.message}),请检查"安装未知应用"权限');
} else if (!_force) {
// 非强制:装起安装器后放行;强制版保持蒙版直至 App 重启为新版
Future<void>.delayed(const Duration(seconds: 2),
() => updateGate.value = null);
}
} catch (e) {
setState(() {
_progress = -1;
_err = '下载失败:$e';
});
}
}
@override
Widget build(BuildContext context) {
return Material(
color: Colors.black87,
child: GestureDetector(
onTap: () {}, // 吸收点击,防止穿透到底层页面
child: Center(
child: Padding(
padding: const EdgeInsets.all(24),
child: Container(
constraints: const BoxConstraints(maxWidth: 420),
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: XlColors.surface,
borderRadius: BorderRadius.circular(16),
border: Border.all(color: XlColors.borderGold, width: 1.2),
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(children: [
Container(width: 4, height: 16, color: XlColors.gold),
const SizedBox(width: 8),
Text('发现新版本 v${widget.info['latest']}',
style: const TextStyle(
fontSize: 17,
fontWeight: FontWeight.w700,
color: XlColors.porcelain)),
]),
if ((widget.info['notes'] ?? '') != '')
Padding(
padding: const EdgeInsets.only(top: 10),
child: Text(widget.info['notes'],
style: const TextStyle(
fontSize: 13, color: XlColors.clay, height: 1.6)),
),
if ((widget.info['size'] ?? 0) > 0)
Padding(
padding: const EdgeInsets.only(top: 6),
child: Text('大小 ${(widget.info['size'] as int) ~/ 1048576}MB',
style:
const TextStyle(fontSize: 12, color: XlColors.clay)),
),
const SizedBox(height: 14),
if (_downloading)
Column(children: [
LinearProgressIndicator(value: _progress, color: XlColors.gold, minHeight: 4),
const SizedBox(height: 6),
Text('下载中 ${(_progress * 100).toStringAsFixed(0)}%',
style: const TextStyle(fontSize: 12, color: XlColors.goldLight)),
]),
if (_progress == 2)
const Text('已下载,请在系统安装器中确认安装',
style: TextStyle(fontSize: 12, color: XlColors.goldLight)),
if (_err != null)
Padding(
padding: const EdgeInsets.only(top: 8),
child: Text(_err!, style: const TextStyle(fontSize: 12, color: XlColors.rise)),
),
const SizedBox(height: 16),
Row(children: [
if (!_force && _progress == -1)
Expanded(
child: OutlinedButton(
onPressed: () => updateGate.value = null,
style: OutlinedButton.styleFrom(
side: const BorderSide(color: XlColors.clay)),
child: const Text('稍后再说',
style: TextStyle(fontSize: 14, color: XlColors.clay)),
),
),
if (!_force && _progress == -1) const SizedBox(width: 12),
Expanded(
child: FilledButton(
onPressed: _downloading ? null : _downloadAndInstall,
style: FilledButton.styleFrom(
backgroundColor: XlColors.vermilion,
padding: const EdgeInsets.symmetric(vertical: 12)),
child: Text(
_downloading ? '下载中…' : (_progress == 2 ? '重新拉起安装' : '立即更新'),
style: const TextStyle(fontSize: 14, color: Colors.white)),
),
),
]),
],
),
),
),
),
),
);
}
}
/// 启动时调用:有新版返回对话框内容,否则返回 null。
Future<Map<String, dynamic>?> checkUpdate(ApiClient api) async {
try {
@@ -52,122 +222,3 @@ Future<Map<String, dynamic>?> checkUpdate(ApiClient api) async {
return null;
}
}
class UpdateDialog extends StatefulWidget {
const UpdateDialog({
super.key,
required this.latest,
required this.notes,
required this.url,
required this.sizeMb,
this.force = false,
});
final String latest;
final String notes;
final String url;
final int sizeMb;
final bool force; // 低于最低版本:不可取消
@override
State<UpdateDialog> createState() => _UpdateDialogState();
}
class _UpdateDialogState extends State<UpdateDialog> {
double _progress = -1; // -1 未开始;0~1 下载中;2 完成
String? _err;
Future<void> _downloadAndInstall() async {
setState(() => _err = null);
try {
final dir = await getTemporaryDirectory();
final filePath = '${dir.path}/xuanlong-${widget.latest}.apk';
setState(() => _progress = 0);
await Dio().download(
'${activeBaseUrl.value}${widget.url}',
filePath,
options: Options(responseType: ResponseType.bytes),
onReceiveProgress: (n, total) {
if (total > 0) setState(() => _progress = n / total);
},
);
setState(() => _progress = 2);
final r = await OpenFilex.open(filePath, type: 'application/vnd.android.package-archive');
if (r.type != ResultType.done) {
setState(() => _err = '无法拉起安装器(${r.message}),请检查"安装未知应用"权限');
}
} catch (e) {
setState(() {
_progress = -1;
_err = '下载失败:$e';
});
}
}
@override
Widget build(BuildContext context) {
final downloading = _progress >= 0 && _progress < 2;
return PopScope(
canPop: !widget.force && !downloading,
child: AlertDialog(
backgroundColor: XlColors.surface,
title: Text('发现新版本 v${widget.latest}',
style: const TextStyle(fontSize: 17, fontWeight: FontWeight.w700, color: XlColors.porcelain)),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (widget.notes.isNotEmpty)
Text(widget.notes, style: const TextStyle(fontSize: 13, color: XlColors.clay, height: 1.6)),
if (widget.sizeMb > 0)
Padding(
padding: const EdgeInsets.only(top: 6),
child: Text('大小 ${widget.sizeMb}MB', style: const TextStyle(fontSize: 12, color: XlColors.clay)),
),
if (downloading)
Padding(
padding: const EdgeInsets.only(top: 12),
child: Column(
children: [
LinearProgressIndicator(value: _progress, color: XlColors.gold, minHeight: 4),
const SizedBox(height: 4),
Text('下载中 ${(_progress * 100).toStringAsFixed(0)}%',
style: const TextStyle(fontSize: 12, color: XlColors.goldLight)),
],
),
),
if (_progress == 2)
const Padding(
padding: EdgeInsets.only(top: 12),
child: Text('已下载,请在系统安装器中确认安装', style: TextStyle(fontSize: 12, color: XlColors.goldLight)),
),
if (_err != null)
Padding(
padding: const EdgeInsets.only(top: 10),
child: Text(_err!, style: const TextStyle(fontSize: 12, color: XlColors.rise)),
),
],
),
actions: [
if (!widget.force && !downloading && _progress != 2)
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('稍后再说', style: TextStyle(fontSize: 14, color: XlColors.clay)),
),
if (_progress != 2)
FilledButton(
onPressed: downloading ? null : _downloadAndInstall,
style: FilledButton.styleFrom(backgroundColor: XlColors.vermilion),
child: Text(downloading ? '下载中…' : '立即更新', style: const TextStyle(fontSize: 14)),
),
if (_progress == 2)
FilledButton(
onPressed: _downloadAndInstall,
style: FilledButton.styleFrom(backgroundColor: XlColors.vermilion),
child: const Text('重新拉起安装', style: TextStyle(fontSize: 14)),
),
],
),
);
}
}
+2 -12
View File
@@ -10,8 +10,8 @@ import 'package:permission_handler/permission_handler.dart';
import '../../core/network/api_client.dart';
import '../../core/network/api_error.dart';
import '../../core/theme/app_colors.dart';
import '../misc/app_updater.dart';
import '../auth/auth_controller.dart';
import '../misc/app_updater.dart';
class SettingsState {
final bool pushEnabled, smsEnabled, dndEnabled;
@@ -219,17 +219,7 @@ class _SettingsPageState extends ConsumerState<SettingsPage> {
);
return;
}
await showDialog(
context: context,
barrierDismissible: upd['force'] != true,
builder: (_) => UpdateDialog(
latest: upd['latest'] as String,
notes: upd['notes'] as String,
url: upd['url'] as String,
sizeMb: ((upd['size'] as int) / 1048576).round(),
force: upd['force'] == true,
),
);
updateGate.value = upd; // 蒙版层固定弹出
},
style: FilledButton.styleFrom(backgroundColor: XlColors.vermilion),
child: checking
+17 -3
View File
@@ -14,6 +14,7 @@ import 'core/theme/app_theme.dart';
import 'features/auth/auth_controller.dart' show authStageProvider;
import 'features/home/home_page.dart';
import 'core/widgets/debug_overlay.dart' show DebugOverlay;
import 'features/misc/app_updater.dart' show UpdateGateOverlay, checkUpdate, updateGate;
import 'features/misc/system_pages.dart';
import 'features/settings/settings_page.dart';
@@ -21,6 +22,14 @@ Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await _devTokenInject();
runApp(const ProviderScope(child: XuanlongApp()));
// 冷启动全局版本检查:不挂在任何页面(页面会被路由销毁),蒙版层独立呈现
Future<void>.delayed(const Duration(seconds: 2), () async {
try {
final api = ApiClient(TokenStore());
final upd = await checkUpdate(api);
if (upd != null) updateGate.value = upd;
} catch (_) {}
});
}
/// 仅 Web 联调用:URL 带 devtoken=<jwt> 时注入登录态并清理地址栏。
@@ -88,9 +97,14 @@ class _XuanlongAppState extends ConsumerState<XuanlongApp> {
routerConfig: router,
debugShowCheckedModeBanner: false,
// 测试版:--dart-define=DEBUG_OVERLAY=true 打开日志悬浮窗
builder: (context, child) => const bool.fromEnvironment('DEBUG_OVERLAY')
? DebugOverlay(child: child ?? const SizedBox.shrink())
: (child ?? const SizedBox.shrink()),
builder: (context, child) {
final base = child ?? const SizedBox.shrink();
return UpdateGateOverlay(
child: const bool.fromEnvironment('DEBUG_OVERLAY')
? DebugOverlay(child: base)
: base,
);
},
);
}
}
+6 -6
View File
@@ -1,8 +1,8 @@
{
"latest_version": "1.2.0",
"min_version": "1.2.0",
"notes": "90m",
"filename": "xuanlong-1.2.0.apk",
"size": 58878770,
"uploaded_at": "2026-11-22 11:31:49"
"latest_version": "1.3.0",
"min_version": "",
"notes": "蒙版升级验证:三信号+90/120分钟",
"filename": "xuanlong-1.3.0.apk",
"size": 58878766,
"uploaded_at": "2026-11-22 12:14:15"
}
Binary file not shown.