import 'package:dio/dio.dart'; import 'package:flutter_patcher/flutter_patcher.dart'; import 'package:logger/logger.dart'; import 'package:path_provider/path_provider.dart'; import 'dart:io'; /// 应用版本信息模型 class AppVersionInfo { final String version; final int versionCode; final String updateType; // "patch" 或 "full" final bool forceUpdate; final String updateDesc; final String? patchUrl; final String? patchMd5; final String? apkUrl; final String? apkMd5; AppVersionInfo({ required this.version, required this.versionCode, required this.updateType, required this.forceUpdate, required this.updateDesc, this.patchUrl, this.patchMd5, this.apkUrl, this.apkMd5, }); factory AppVersionInfo.fromJson(Map json) { // 兼容嵌套结构:如果存在 patch 对象,则从中提取 url 和 md5 final patchData = json['patch'] as Map?; final fullApkData = json['fullApk'] as Map?; return AppVersionInfo( version: json['version'] ?? '', versionCode: json['versionCode'] ?? 0, // 处理 null 情况 updateType: json['updateType'] ?? 'full', forceUpdate: json['forceUpdate'] ?? false, updateDesc: json['updateDesc'] ?? '', patchUrl: patchData?['patchUrl'] as String?, patchMd5: patchData?['md5'] as String?, apkUrl: fullApkData?['apkUrl'] as String?, apkMd5: fullApkData?['md5'] as String?, ); } } /// 版本检查服务 class VersionCheckService { static final VersionCheckService _instance = VersionCheckService._internal(); factory VersionCheckService() => _instance; VersionCheckService._internal(); final Logger _logger = Logger(); final Dio _dio = Dio(BaseOptions( connectTimeout: const Duration(seconds: 10), receiveTimeout: const Duration(seconds: 10), )); // 后端接口地址 static const String apiUrl = 'http://8.159.134.0:8012/api/update/check'; // 🔥 补丁版本号文件路径(持久化存储,不会被清理缓存删除) static const String _patchVersionFileName = '.patch_version'; /// 检查版本更新 Future checkUpdate({ required String currentVersion, required int currentVersionCode, }) async { try { _logger.i('🔍 检查版本更新'); _logger.i('📱 APK 自带版本: $currentVersion ($currentVersionCode)'); // 🔥 从文件读取已应用的补丁版本 final patchInfo = await _loadPatchVersionInfo(); final appliedPatchVersion = patchInfo['version'] as String?; final appliedPatchVersionCode = patchInfo['versionCode'] as int?; // 如果打过补丁,使用补丁的版本信息;否则使用 APK 自带的版本 final requestVersion = appliedPatchVersion ?? currentVersion; final requestVersionCode = appliedPatchVersionCode ?? currentVersionCode; _logger.i('🚀 请求后端版本: $requestVersion ($requestVersionCode)'); // 调用后端接口 final response = await _dio.get( apiUrl, queryParameters: { 'version': requestVersion, 'versionCode': requestVersionCode, }, ); if (response.statusCode == 200) { final data = response.data['data']; if (data['hasUpdate'] == false) { _logger.i('✅ 已是最新版本'); return null; } final versionInfo = AppVersionInfo.fromJson(data); // 如果已应用此版本补丁,跳过更新 if (appliedPatchVersion == versionInfo.version && versionInfo.updateType == 'patch') { _logger.i('✅ 已应用最新版本补丁: ${versionInfo.version}'); return null; } _logger.i('✅ 发现新版本: ${versionInfo.version}'); _logger.i('🔄 更新类型: ${versionInfo.updateType}'); _logger.i('⚠️ 强制更新: ${versionInfo.forceUpdate}'); return versionInfo; } return null; } catch (e, stack) { _logger.e('❌ 版本检查失败: $e'); _logger.e('❌ 堆栈信息: $stack'); throw Exception('网络请求失败: $e'); } } /// 下载并应用差量补丁 Future applyPatch({ required String patchUrl, required String version, required int targetVersionCode, String? md5, Function(double)? onProgress, }) async { try { _logger.i('⬇️ 开始下载补丁: $patchUrl'); // 1. 手动下载补丁文件 final tempDir = await getTemporaryDirectory(); final patchPath = '${tempDir.path}/update_patch.so'; await _dio.download( patchUrl, patchPath, onReceiveProgress: (received, total) { if (total != -1 && onProgress != null) { onProgress(received / total); } }, ); _logger.i('✅ 补丁下载完成,开始应用...'); // 2. 应用本地补丁文件 final result = await FlutterPatcher.applyPatch( PatchInfo( version: version, patchUrl: 'file://$patchPath', targetVersionCode: targetVersionCode, md5: md5 ?? '', ), ); if (result.ok) { _logger.i('✅ 补丁应用成功,需要冷启动生效'); // 🔥 保存补丁版本号到文件(持久化存储) await _savePatchVersionInfo(version, targetVersionCode); _logger.i('💾 已保存补丁版本: $version ($targetVersionCode)'); } else { _logger.e('❌ 补丁应用失败: ${result.error}'); } return result.ok; } catch (e) { _logger.e('❌ 应用补丁异常: $e'); return false; } } /// 下载完整 APK Future downloadApk(String apkUrl, Function(double) onProgress) async { try { _logger.i('⬇️ 开始下载 APK: $apkUrl'); final tempDir = await getTemporaryDirectory(); final apkPath = '${tempDir.path}/app-update.apk'; await _dio.download( apkUrl, apkPath, onReceiveProgress: (received, total) { if (total != -1) { onProgress(received / total); } }, ); _logger.i('✅ APK 下载完成: $apkPath'); return apkPath; } catch (e) { _logger.e('❌ 下载 APK 失败: $e'); return null; } } /// 回滚到内置版本 Future rollback() async { try { _logger.i('🔄 执行回滚操作'); await FlutterPatcher.rollback(); _logger.i('✅ 回滚成功,下次冷启动生效'); } catch (e) { _logger.e('❌ 回滚失败: $e'); } } // 🔥 清除补丁版本信息(整包更新后调用) Future clearPatchVersionInfo() async { try { final directory = await getApplicationSupportDirectory(); final file = File('${directory.path}/$_patchVersionFileName'); if (await file.exists()) { await file.delete(); _logger.i('🗑️ 已清除补丁版本记录'); } } catch (e) { _logger.e('❌ 清除补丁版本信息失败: $e'); } } // 🔥 保存补丁版本信息到文件(持久化,不会被清理缓存删除) Future _savePatchVersionInfo(String version, int versionCode) async { try { final directory = await getApplicationSupportDirectory(); final file = File('${directory.path}/$_patchVersionFileName'); await file.writeAsString('$version|$versionCode'); _logger.i('💾 补丁版本已保存到: ${file.path}'); } catch (e) { _logger.e('❌ 保存补丁版本信息失败: $e'); } } // 🔥 从文件加载补丁版本信息 Future> _loadPatchVersionInfo() async { try { final directory = await getApplicationSupportDirectory(); final file = File('${directory.path}/$_patchVersionFileName'); if (await file.exists()) { final content = await file.readAsString(); 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)'); return {'version': version, 'versionCode': versionCode}; } } } return {'version': null, 'versionCode': null}; } catch (e) { _logger.e('❌ 加载补丁版本信息失败: $e'); return {'version': null, 'versionCode': null}; } } }