修复版本管理整包更新无法弹出的问题

完成集成新的登录接口的更换使用
完成集成用户的场站列表接口
完成开发设计选择场站项为全局属性
完成我的页面的个人信息的集成和详情页面的开发
完成设备页面的中无人机机场的列表接口对接入和个项页面的布局的优化
完成设备页面的中无人机机场的详情接口的对接和使用和页面的更新布局
This commit is contained in:
2026-05-29 14:13:34 +08:00
parent 938aac2db9
commit 335c468aac
23 changed files with 1294 additions and 705 deletions

View File

@@ -8,7 +8,7 @@ import 'dart:io';
class AppVersionInfo {
final String version;
final int versionCode;
final String updateType; // "patch" 或 "full"
final String updateType;
final bool forceUpdate;
final String updateDesc;
final String? patchUrl;
@@ -29,13 +29,12 @@ class AppVersionInfo {
});
factory AppVersionInfo.fromJson(Map<String, dynamic> json) {
// 兼容嵌套结构:如果存在 patch 对象,则从中提取 url 和 md5
final patchData = json['patch'] as Map<String, dynamic>?;
final fullApkData = json['fullApk'] as Map<String, dynamic>?;
return AppVersionInfo(
version: json['version'] ?? '',
versionCode: json['versionCode'] ?? 0, // 处理 null 情况
versionCode: json['versionCode'] ?? 0,
updateType: json['updateType'] ?? 'full',
forceUpdate: json['forceUpdate'] ?? false,
updateDesc: json['updateDesc'] ?? '',
@@ -59,10 +58,7 @@ class VersionCheckService {
receiveTimeout: const Duration(seconds: 10),
));
// 后端接口地址
static const String apiUrl = 'http://8.159.134.0:8012/api/update/check';
// 🔥 补丁版本号文件路径(持久化存储,不会被清理缓存删除)
static const String _patchVersionFileName = '.patch_version';
/// 检查版本更新
@@ -74,18 +70,19 @@ class VersionCheckService {
_logger.i('🔍 检查版本更新');
_logger.i('📱 APK 自带版本: $currentVersion ($currentVersionCode)');
// 🔥 从文件读取已应用的补丁版本
// 从文件读取已应用的补丁版本
final patchInfo = await _loadPatchVersionInfo();
final appliedPatchVersion = patchInfo['version'] as String?;
final appliedPatchVersionCode = patchInfo['versionCode'] as int?;
_logger.i('📂 读取到的补丁版本: $appliedPatchVersion ($appliedPatchVersionCode)');
// 如果打过补丁,使用补丁的版本信息;否则使用 APK 自带的版本
// 如果打过补丁,使用补丁的版本;否则使用 APK 自带版本
final requestVersion = appliedPatchVersion ?? currentVersion;
final requestVersionCode = appliedPatchVersionCode ?? currentVersionCode;
_logger.i('🚀 请求后端版本: $requestVersion ($requestVersionCode)');
// 调用后端接口
final response = await _dio.get(
apiUrl,
queryParameters: {
@@ -104,9 +101,15 @@ class VersionCheckService {
final versionInfo = AppVersionInfo.fromJson(data);
// 如果已应用此版本补丁,跳过更新
if (appliedPatchVersion == versionInfo.version && versionInfo.updateType == 'patch') {
_logger.i('✅ 已应用最新版本补丁: ${versionInfo.version}');
// 🔥 关键修复:无论更新类型是什么,只要本地已应用此版本,就跳过
if (appliedPatchVersion != null && appliedPatchVersion == versionInfo.version) {
_logger.i('✅ 本地已应用版本 ${versionInfo.version},跳过更新(updateType: ${versionInfo.updateType})');
return null;
}
// 🔥 额外检查:如果本地补丁版本code >= 服务端返回的versionCode,也跳过
if (appliedPatchVersionCode != null && appliedPatchVersionCode >= versionInfo.versionCode) {
_logger.i('✅ 本地补丁版本code ($appliedPatchVersionCode) >= 服务端versionCode (${versionInfo.versionCode}),跳过更新');
return null;
}
@@ -135,8 +138,8 @@ class VersionCheckService {
}) async {
try {
_logger.i('⬇️ 开始下载补丁: $patchUrl');
_logger.i('🎯 目标版本: $version ($targetVersionCode)');
// 1. 手动下载补丁文件
final tempDir = await getTemporaryDirectory();
final patchPath = '${tempDir.path}/update_patch.so';
@@ -152,7 +155,6 @@ class VersionCheckService {
_logger.i('✅ 补丁下载完成,开始应用...');
// 2. 应用本地补丁文件
final result = await FlutterPatcher.applyPatch(
PatchInfo(
version: version,
@@ -163,10 +165,13 @@ class VersionCheckService {
);
if (result.ok) {
_logger.i('✅ 补丁应用成功,需要冷启动生效');
// 🔥 保存补丁版本号到文件(持久化存储)
_logger.i('✅ 补丁应用成功');
// 保存补丁版本号到文件
await _savePatchVersionInfo(version, targetVersionCode);
_logger.i('💾 已保存补丁版本: $version ($targetVersionCode)');
// 验证保存
final verifyInfo = await _loadPatchVersionInfo();
_logger.i('💾 已保存并验证: ${verifyInfo['version']} (${verifyInfo['versionCode']})');
} else {
_logger.e('❌ 补丁应用失败: ${result.error}');
}
@@ -183,8 +188,10 @@ class VersionCheckService {
try {
_logger.i('⬇️ 开始下载 APK: $apkUrl');
final tempDir = await getTemporaryDirectory();
final apkPath = '${tempDir.path}/app-update.apk';
final cacheDir = await getApplicationCacheDirectory();
final apkPath = '${cacheDir.path}/app-update.apk';
_logger.i('📁 APK 下载路径: $apkPath');
await _dio.download(
apkUrl,
@@ -209,13 +216,13 @@ class VersionCheckService {
try {
_logger.i('🔄 执行回滚操作');
await FlutterPatcher.rollback();
_logger.i('✅ 回滚成功,下次冷启动生效');
_logger.i('✅ 回滚成功');
} catch (e) {
_logger.e('❌ 回滚失败: $e');
}
}
// 🔥 清除补丁版本信息(整包更新后调用)
/// 清除补丁版本信息(整包更新后调用)
Future<void> clearPatchVersionInfo() async {
try {
final directory = await getApplicationSupportDirectory();
@@ -229,36 +236,54 @@ class VersionCheckService {
}
}
// 🔥 保存补丁版本信息到文件(持久化,不会被清理缓存删除)
/// 保存补丁版本信息到文件
Future<void> _savePatchVersionInfo(String version, int versionCode) async {
try {
final directory = await getApplicationSupportDirectory();
final file = File('${directory.path}/$_patchVersionFileName');
await file.writeAsString('$version|$versionCode');
final content = '$version|$versionCode';
await file.writeAsString(content);
// 🔥 立即验证写入
final verifyContent = await file.readAsString();
_logger.i('💾 补丁版本已保存到: ${file.path}');
_logger.i('💾 写入内容: $content');
_logger.i('💾 验证读取: $verifyContent');
_logger.i('💾 文件存在: ${await file.exists()}');
} catch (e) {
_logger.e('❌ 保存补丁版本信息失败: $e');
}
}
// 🔥 从文件加载补丁版本信息
/// 从文件加载补丁版本信息
Future<Map<String, dynamic>> _loadPatchVersionInfo() async {
try {
final directory = await getApplicationSupportDirectory();
final file = File('${directory.path}/$_patchVersionFileName');
_logger.i('📂 尝试加载补丁版本文件: ${file.path}');
_logger.i('📂 文件存在: ${await file.exists()}');
if (await file.exists()) {
final content = await file.readAsString();
_logger.i('📂 文件内容: $content');
final parts = content.split('|');
if (parts.length == 2) {
final version = parts[0];
final versionCode = int.tryParse(parts[1]);
if (versionCode != null) {
_logger.i('📂 从文件读取补丁版本: $version ($versionCode)');
_logger.i('📂 解析成功: version=$version, versionCode=$versionCode');
return {'version': version, 'versionCode': versionCode};
} else {
_logger.e('📂 versionCode 解析失败: ${parts[1]}');
}
} else {
_logger.e('📂 文件格式错误,parts.length=${parts.length}');
}
}
_logger.i('📂 未找到有效的补丁版本信息');
return {'version': null, 'versionCode': null};
} catch (e) {
_logger.e('❌ 加载补丁版本信息失败: $e');