Browse Source

feat(version_update): 添加应用信息缓存及日志输出

添加_appInfo静态变量缓存应用信息并增加相关getter方法
在关键流程中添加调试日志输出以便排查问题
newdev_shunjiawei
lxm 1 year ago
parent
commit
a7975f08b9
  1. 19
      lib/data/services/version_update_service.dart

19
lib/data/services/version_update_service.dart

@ -24,6 +24,7 @@ class VersionUpdateService extends GetxService {
static bool? _hasUpdate = false; //是否有更新 static bool? _hasUpdate = false; //是否有更新
static bool? _isForceUpdate = false; //是否强制更新 static bool? _isForceUpdate = false; //是否强制更新
static String? _updateUrl = ""; //更新链接 static String? _updateUrl = ""; //更新链接
static AppInfo? _appInfo; //应用信息
static bool? _isShowTourists = false; //是否显示游客登录 static bool? _isShowTourists = false; //是否显示游客登录
static DateTime? _lastCheckTime; //上次检查时间 static DateTime? _lastCheckTime; //上次检查时间
static const int _cacheIntervalMinutes = 10; // 可配置化 static const int _cacheIntervalMinutes = 10; // 可配置化
@ -62,6 +63,9 @@ class VersionUpdateService extends GetxService {
/// 获取缓存的更新地址 /// 获取缓存的更新地址
String get cachedUpdateUrl => _updateUrl ?? ''; String get cachedUpdateUrl => _updateUrl ?? '';
/// 获取缓存的应用信息
AppInfo? get cachedAppInfo => _appInfo;
/// 获取缓存的游客登录状态 /// 获取缓存的游客登录状态
bool get cachedShowTourists => _isShowTourists ?? false; bool get cachedShowTourists => _isShowTourists ?? false;
@ -183,10 +187,13 @@ class VersionUpdateService extends GetxService {
if (_lastCheckTime != null && if (_lastCheckTime != null &&
DateTime.now().difference(_lastCheckTime!).inMinutes < DateTime.now().difference(_lastCheckTime!).inMinutes <
_cacheIntervalMinutes) { _cacheIntervalMinutes) {
// [1]没有超过10分钟,返回缓存数据
_logger.d('没有超过10分钟,返回缓存数据');
return VersionUpdateInfo( return VersionUpdateInfo(
hasUpdate: _hasUpdate ?? false, hasUpdate: _hasUpdate ?? false,
currentVersion: _appVersion ?? '1.0.0', currentVersion: _appVersion ?? '1.0.0',
latestVersion: (_latestVersion ?? '1.0.0').split('+')[0], latestVersion: (_latestVersion ?? '1.0.0').split('+')[0],
appInfo: _appInfo,
updateType: _hasUpdate! updateType: _hasUpdate!
? (_isForceUpdate! ? UpdateType.force : UpdateType.optional) ? (_isForceUpdate! ? UpdateType.force : UpdateType.optional)
: UpdateType.none, : UpdateType.none,
@ -208,6 +215,8 @@ class VersionUpdateService extends GetxService {
_logger.d('获取渠道包信息:$response'); _logger.d('获取渠道包信息:$response');
if (response == null || !response.containsKey('app')) { if (response == null || !response.containsKey('app')) {
//[2]获取渠道包信息失败: 响应为空或不包含app字段
_logger.w('获取渠道包信息失败: 响应为空或不包含app字段');
return VersionUpdateInfo( return VersionUpdateInfo(
hasUpdate: false, hasUpdate: false,
currentVersion: currentVersion, currentVersion: currentVersion,
@ -219,6 +228,7 @@ class VersionUpdateService extends GetxService {
//final channelAppResponse = ChannelAppResponse.fromJson(response); //final channelAppResponse = ChannelAppResponse.fromJson(response);
// 直接从response中获取app信息 // 直接从response中获取app信息
final appInfo = AppInfo.fromJson(response['app']); final appInfo = AppInfo.fromJson(response['app']);
_appInfo = appInfo;
_logger.d('获取到的最新版本信息: ${appInfo.toJson()}'); _logger.d('获取到的最新版本信息: ${appInfo.toJson()}');
_latestVersion = appInfo.version; _latestVersion = appInfo.version;
_updateUrl = appInfo.address; _updateUrl = appInfo.address;
@ -246,7 +256,7 @@ class VersionUpdateService extends GetxService {
_isForceUpdate = _shouldForceUpdate(currentVersion, appInfo.version); _isForceUpdate = _shouldForceUpdate(currentVersion, appInfo.version);
updateType = _isForceUpdate! ? UpdateType.force : UpdateType.optional; updateType = _isForceUpdate! ? UpdateType.force : UpdateType.optional;
} }
//[3]返回版本更新信息
return VersionUpdateInfo( return VersionUpdateInfo(
hasUpdate: _hasUpdate!, hasUpdate: _hasUpdate!,
currentVersion: currentVersion.split('+')[0], currentVersion: currentVersion.split('+')[0],
@ -343,11 +353,18 @@ class VersionUpdateService extends GetxService {
/// 执行更新操作 /// 执行更新操作
Future<bool> performUpdate(VersionUpdateInfo updateInfo) async { Future<bool> performUpdate(VersionUpdateInfo updateInfo) async {
print('lxm---updateInfo详情:');
print('lxm---hasUpdate: ${updateInfo.hasUpdate}');
print('lxm---currentVersion: ${updateInfo.currentVersion}');
print('lxm---latestVersion: ${updateInfo.latestVersion}');
print('lxm---updateType: ${updateInfo.updateType}');
print('lxm---appInfo: ${updateInfo.appInfo?.toJson()}');
if (!updateInfo.hasUpdate || updateInfo.appInfo == null) { if (!updateInfo.hasUpdate || updateInfo.appInfo == null) {
return false; return false;
} }
try { try {
_logger.d('lxm---执行更新操作,下载地址: ${updateInfo.appInfo!.address}');
return await _openDownloadUrl(updateInfo.appInfo!.address!); return await _openDownloadUrl(updateInfo.appInfo!.address!);
} catch (e) { } catch (e) {
_logger.e('执行更新失败: $e'); _logger.e('执行更新失败: $e');

Loading…
Cancel
Save