import 'dart:io'; import 'package:get/get.dart'; import '../config/config.dart'; import '../hj_model/splash/domain_source_model.dart'; ///启动页 /ping/domain 下发的当前平台版本信息,冷启动必刷新,不落磁盘 CheckVersionInfo? _remoteVersion; ///启动页拿到配置后调一次,筛出当前平台那条存内存 void saveVersion(List versions) { final os = Platform.operatingSystem; _remoteVersion = versions.firstWhereOrNull((e) => e.platform?.toLowerCase() == os); } ///有新版本则返回版本信息,已是最新 / 没配置都返回 null;只读内存,不发请求 CheckVersionInfo? checkUpdate() { final info = _remoteVersion; if (info == null) return null; return compareVersion(Config.innerVersion, info.verName ?? '') ? info : null; } ///比较版本号:newVersion 比 version 新则返回 true(需更新) bool compareVersion(String version, String newVersion) { List localArr = version.split("."); List newArr = newVersion.split("."); //逐位比较,最多比较前 3 段(major.minor.patch),按两者较短的段数取,避免越界 int count = newArr.length < localArr.length ? newArr.length : localArr.length; if (count > 3) count = 3; for (int i = 0; i < count; i++) { int newCode = int.tryParse(newArr[i]) ?? 0; int localCode = int.tryParse(localArr[i]) ?? 0; if (newCode > localCode) return true; if (newCode < localCode) return false; } return false; }