| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643 |
- import 'dart:async';
- import 'dart:convert';
- import 'dart:io';
- import 'package:device_info_plus/device_info_plus.dart';
- import 'package:dio/dio.dart';
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- import 'package:nomo/app/api/router/api_router.dart';
- import 'package:nomo/app/data/sp/ix_sp.dart';
- import 'package:nomo/app/constants/sp_keys.dart';
- import 'package:package_info_plus/package_info_plus.dart';
- import 'package:path_provider/path_provider.dart';
- import 'package:play_install_referrer/play_install_referrer.dart';
- import 'package:system_clock/system_clock.dart';
- import 'package:uuid/uuid.dart';
- import '../../config/translations/localization_service.dart';
- import '../../config/translations/strings_enum.dart';
- import '../../utils/boost_report_manager.dart';
- import '../../utils/gzip_manager.dart';
- import '../api/file/api_file.dart';
- import '../data/models/banner/banner_list.dart';
- import '../data/models/launch/upgrade.dart';
- import 'base_core_api.dart';
- import '../../utils/api_statistics.dart';
- import '../../utils/device_manager.dart';
- import '../../utils/geo_downloader.dart';
- import '../../utils/log/logger.dart';
- import '../../utils/network_helper.dart';
- import '../../utils/ntp_time_service.dart';
- import '../api/core/api_core.dart';
- import '../api/log/api_log.dart';
- import '../components/country_restricted_overlay.dart';
- import '../components/ix_snackbar.dart';
- import '../constants/api_domains.dart';
- import '../constants/configs.dart';
- import '../constants/enums.dart';
- import '../constants/errors.dart';
- import '../constants/platforms.dart';
- import '../data/models/api_exception.dart';
- import '../data/models/channelplan/channel_plan_list.dart';
- import '../data/models/failure.dart';
- import '../data/models/fingerprint.dart';
- import '../data/models/launch/groups.dart';
- import '../data/models/launch/launch.dart';
- import '../dialog/all_dialog.dart';
- class ApiController extends GetxService with WidgetsBindingObserver {
- final TAG = 'ApiController';
- // 记录是否已经显示禁用弹窗
- bool isShowDisabled = false;
- // 上次调用 uploadBoostLog 的时间(用于节流)
- DateTime? _lastUploadBoostLogTime;
- //是否是游客
- final _isGuest = false.obs;
- bool get isGuest => _isGuest.value;
- set isGuest(bool value) => _isGuest.value = value;
- //是否是会员
- final _isPremium = false.obs;
- bool get isPremium => _isPremium.value;
- set isPremium(bool value) => _isPremium.value = value;
- //用户等级
- final _userLevel = 1.obs;
- int get userLevel => _userLevel.value;
- set userLevel(int value) => _userLevel.value = value;
- // 过期时间文本
- final _expireTimeText = ''.obs;
- String get expireTimeText => _expireTimeText.value;
- set expireTimeText(String value) => _expireTimeText.value = value;
- // 有效期文本
- final _validTermText = ''.obs;
- String get validTermText => _validTermText.value;
- set validTermText(String value) => _validTermText.value = value;
- //全部节点列表
- final _nodesList = <LocationList>[].obs;
- List<LocationList> get nodesList => _nodesList.value;
- set nodesList(List<LocationList> value) => _nodesList.value = value;
- //初始化fingerprint
- Fingerprint fp = Fingerprint.empty();
- // 全局剩余时间倒计时(秒)
- int _remainTimeSeconds = 0;
- int get remainTimeSeconds => _remainTimeSeconds;
- // 格式化后的剩余时间字符串(响应式,只有文案变化时才更新 UI)
- final _remainTimeFormatted = ''.obs;
- String get remainTimeFormatted => _remainTimeFormatted.value;
- // 是否应该显示倒计时(响应式,只有状态变化时才更新 UI)
- final _shouldShowCountdown = false.obs;
- bool get shouldShowCountdown => _shouldShowCountdown.value;
- // 倒计时定时器
- Timer? _remainTimeTimer;
- // 是否在后台
- bool isBackground = false;
- // 切换到后台时的系统时钟时间戳(毫秒,不受系统时间修改影响)
- int _backgroundElapsedRealtime = 0;
- @override
- void onInit() {
- super.onInit();
- WidgetsBinding.instance.addObserver(this);
- }
- @override
- void onClose() {
- WidgetsBinding.instance.removeObserver(this);
- super.onClose();
- }
- @override
- void didChangeAppLifecycleState(AppLifecycleState state) {
- log(TAG, "App state: $state");
- if (state == AppLifecycleState.paused) {
- isBackground = true;
- // 记录切换到后台的系统时钟时间戳(不受系统时间修改影响)
- _backgroundElapsedRealtime = SystemClock.elapsedRealtime().inMilliseconds;
- ApiStatistics.instance.onAppPaused();
- stopRemainTimeCountdown();
- } else if (state == AppLifecycleState.resumed) {
- if (isBackground) {
- isBackground = false;
- asyncHandleLaunch(isRefreshLaunch: true);
- ApiStatistics.instance.onAppResumed();
- // 计算后台经过的时间,继续倒计时
- _resumeRemainTimeCountdown();
- }
- }
- }
- /// 恢复倒计时(从后台切换到前台时调用)
- void _resumeRemainTimeCountdown() {
- if (_backgroundElapsedRealtime > 0 && _remainTimeSeconds > 0) {
- // 计算后台经过的秒数(使用系统时钟,不受系统时间修改影响)
- final currentElapsedRealtime =
- SystemClock.elapsedRealtime().inMilliseconds;
- final elapsedSeconds =
- (currentElapsedRealtime - _backgroundElapsedRealtime) ~/ 1000;
- // 更新剩余时间
- final newRemainTime = _remainTimeSeconds - elapsedSeconds;
- log(
- TAG,
- 'Resume countdown: elapsed=${elapsedSeconds}s, '
- 'old=$_remainTimeSeconds, new=$newRemainTime',
- );
- // 重新启动倒计时
- if (newRemainTime > 0) {
- startRemainTimeCountdown(newRemainTime);
- } else {
- // 时间已耗尽
- _remainTimeSeconds = 0;
- _updateRemainTimeFormatted();
- _onRemainTimeExpired();
- }
- }
- _backgroundElapsedRealtime = 0;
- }
- Future<Fingerprint> initFingerprint() async {
- // 读取app发布渠道
- if (Platform.isIOS) {
- fp.channel = 'apple';
- } else if (Platform.isAndroid) {
- try {
- final channel = await BaseCoreApi().getChannel();
- fp.channel = channel ?? 'unknown';
- } catch (e) {
- log(TAG, 'read app channel error: $e');
- fp.channel = '';
- }
- try {
- final advertisingId = await BaseCoreApi().getAdvertisingId();
- fp.googleId = advertisingId ?? '';
- } catch (e) {
- log(TAG, 'read app googleId error: $e');
- fp.googleId = '';
- }
- try {
- ReferrerDetails referrerDetails =
- await PlayInstallReferrer.installReferrer;
- fp.refer = referrerDetails.installReferrer ?? '';
- } catch (e) {
- log(TAG, 'get install referrer error: $e');
- fp.refer = '';
- }
- } else if (Platform.isWindows) {
- fp.channel = 'universal';
- }
- // 读取应用信息
- final info = await PackageInfo.fromPlatform();
- fp.appVersionCode = int.tryParse(info.buildNumber) ?? 0;
- fp.appVersionName = info.version;
- // 读取设备信息
- final deviceInfo = DeviceInfoPlugin();
- if (Platform.isIOS) {
- fp.platform = Platforms.iOS;
- final iosOsInfo = await deviceInfo.iosInfo;
- fp.deviceModel = iosOsInfo.model;
- fp.deviceOs = iosOsInfo.systemVersion;
- fp.deviceBrand = iosOsInfo.utsname.machine;
- } else if (Platform.isAndroid) {
- fp.platform = Platforms.android;
- final androidOsInfo = await deviceInfo.androidInfo;
- fp.deviceModel = androidOsInfo.model;
- fp.deviceOs = androidOsInfo.version.release;
- fp.deviceBrand = androidOsInfo.brand;
- fp.androidId = androidOsInfo.id;
- } else if (Platform.isWindows) {
- fp.platform = Platforms.windows;
- final windowsInfo = await deviceInfo.windowsInfo;
- fp.deviceModel = windowsInfo.productName;
- fp.deviceOs = windowsInfo.csdVersion;
- fp.deviceBrand = windowsInfo.computerName;
- }
- //获取设备尺寸
- fp.deviceHeight = Get.height.toInt();
- fp.deviceWidth = Get.width.toInt();
- // 读取设备ID
- fp.deviceId = DeviceManager.getCacheDeviceId();
- fp.isNewInstall = IXSP.getIsNewInstall();
- await updateFingerprintData();
- return fp;
- }
- // 更新部分数据
- Future<void> updateFingerprintData() async {
- fp.lang = IXSP.getCurrentLocal().languageCode;
- fp.phoneCountryIso = LocalizationService.getSystemCountry();
- fp.isVpn = await BaseCoreApi().isConnected() ?? false;
- if (!fp.isVpn) {
- fp.isConnectedVpn = false;
- }
- try {
- final simInfo = await BaseCoreApi().getSimInfo();
- // 解析sim
- final sim = jsonDecode(simInfo ?? '{}');
- fp.simReady = sim['simReady'];
- fp.carrierName = sim['carrierName'];
- fp.mcc = sim['mcc'];
- fp.mnc = sim['mnc'];
- fp.countryIso = sim['countryIso'];
- fp.networkCarrierName = sim['networkCarrierName'];
- fp.networkMcc = sim['networkMcc'];
- fp.networkMnc = sim['networkMnc'];
- fp.networkCountryIso = sim['networkCountryIso'];
- } catch (e) {
- log(TAG, 'read app sim error: $e');
- }
- }
- Future<void> initData(Launch? launch) async {
- // 初始化是否第一次安装
- IXSP.setIsNewInstall(false);
- fp.userUuid = '';
- fp.isNewInstall = false;
- await initLaunch(launch);
- }
- Future<void> initLaunch(Launch? launch) async {
- try {
- if (launch != null) {
- // 初始化用户状态
- isGuest = launch.userConfig?.memberLevel == MemberLevel.guest.level;
- userLevel = launch.userConfig?.userLevel ?? 1;
- isPremium = userLevel == 3 || userLevel == 9999;
- expireTimeText = _getExpireTimeText();
- validTermText = _getValidTermText();
- updateRemainTime(launch.userConfig?.remainTime ?? 0);
- NtpTimeService().initLaunchInitialTime();
- // 设置路由和节点
- nodesList = launch.groups?.normal?.list ?? [];
- // 设置资源url
- if (launch.appConfig?.assetUrls != null &&
- launch.appConfig!.assetUrls!.isNotEmpty) {
- Configs.assetUrl = launch.appConfig!.assetUrls![0];
- }
- // 设置官网url
- if (launch.appConfig?.websiteUrl != null &&
- launch.appConfig!.websiteUrl!.isNotEmpty) {
- Configs.websiteUrl = launch.appConfig!.websiteUrl!;
- }
- }
- } catch (e) {
- log(TAG, 'initLaunch error: $e');
- }
- }
- // 发送分析事件, 后续可以发送到firebase
- Future<void> sendAnalytics(FirebaseEvent event) async {
- try {} catch (e) {
- log('sendAnalytics error: $e');
- }
- }
- Future<Launch> launch({bool isCache = false}) async {
- sendAnalytics(isCache ? FirebaseEvent.launchCache : FirebaseEvent.launch);
- while (true) {
- try {
- ApiCore().setbaseUrl(ApiDomains.instance.getApiUrl());
- final request = fp.toJson();
- final result = await ApiCore().launch(request);
- if (!result.success) {
- throw Failure(
- code: result.errorCode ?? '',
- message: result.errorMessage ?? '',
- );
- }
- sendAnalytics(
- isCache
- ? FirebaseEvent.launchCacheSuccess
- : FirebaseEvent.launchSuccess,
- );
- // 重置禁用状态
- IXSP.setLastIsRegionDisabled(false);
- IXSP.setLastIsUserDisabled(false);
- final launchData = Launch.fromJson(result.data);
- // 设置扩展数据
- fp.exData = launchData.exData;
- // 更新URL列表
- await ApiDomains.instance.updateFromLaunch(launchData);
- // 保存Launch数据
- await IXSP.saveLaunch(launchData);
- // 初始化Launch
- await initData(launchData);
- // 缓存日志上传
- processCachedLogs();
- // 缓存文件日志上传
- processCachedFileLogs();
- return launchData;
- } on ApiException catch (e) {
- final url = await ApiDomains.instance.getNextApiUrl();
- log(TAG, 'Launch request failed for URL $url: $e');
- if (url.isEmpty) {
- rethrow;
- }
- ApiCore().setbaseUrl(url);
- } on Failure catch (_) {
- rethrow;
- } on DioException catch (e) {
- if (e.response?.statusCode == Errors.eRegionNotAvailable ||
- e.response?.statusCode == Errors.eUserDisabled ||
- e.response?.statusCode == Errors.eTokenExpired) {
- rethrow;
- } else {
- if (await NetworkHelper.instance.isNetworkAvailable()) {
- final url = await ApiDomains.instance.getNextApiUrl();
- log(TAG, 'Launch request failed for URL $url: $e');
- if (url.isEmpty) {
- rethrow;
- }
- ApiCore().setbaseUrl(url);
- } else {
- rethrow;
- }
- }
- } catch (e) {
- final url = await ApiDomains.instance.getNextApiUrl();
- log(TAG, 'Launch request failed for URL $url: $e');
- if (url.isEmpty) {
- rethrow;
- }
- ApiCore().setbaseUrl(url);
- }
- }
- }
- Future<Launch> refreshLaunch() async {
- while (true) {
- try {
- ApiCore().setbaseUrl(ApiDomains.instance.getApiUrl());
- final request = fp.toJson();
- final result = await ApiCore().refreshLaunch(request);
- if (!result.success) {
- throw Failure(
- code: result.errorCode ?? '',
- message: result.errorMessage ?? '',
- );
- }
- // 重置禁用状态
- IXSP.setLastIsRegionDisabled(false);
- IXSP.setLastIsUserDisabled(false);
- final launchData = Launch.fromJson(result.data);
- // 设置扩展数据
- fp.exData = launchData.exData;
- // 更新URL列表
- await ApiDomains.instance.updateFromLaunch(launchData);
- // 保存Launch数据
- await IXSP.saveLaunch(launchData);
- // 初始化Launch
- await initData(launchData);
- // 缓存日志上传
- processCachedLogs();
- // 缓存文件日志上传
- processCachedFileLogs();
- return launchData;
- } on ApiException catch (e) {
- final url = await ApiDomains.instance.getNextApiUrl();
- log(TAG, 'refresh launch request failed for URL $url: $e');
- if (url.isEmpty) {
- rethrow;
- }
- ApiCore().setbaseUrl(url);
- } on Failure catch (_) {
- rethrow;
- } on DioException catch (e) {
- if (e.response?.statusCode == Errors.eRegionNotAvailable ||
- e.response?.statusCode == Errors.eUserDisabled ||
- e.response?.statusCode == Errors.eTokenExpired) {
- rethrow;
- } else {
- if (await NetworkHelper.instance.isNetworkAvailable()) {
- final url = await ApiDomains.instance.getNextApiUrl();
- log(TAG, 'refresh launch request failed for URL $url: $e');
- if (url.isEmpty) {
- rethrow;
- }
- ApiCore().setbaseUrl(url);
- } else {
- rethrow;
- }
- }
- } catch (e) {
- final url = await ApiDomains.instance.getNextApiUrl();
- log(TAG, 'refresh launch request failed for URL $url: $e');
- if (url.isEmpty) {
- rethrow;
- }
- ApiCore().setbaseUrl(url);
- }
- }
- }
- Future<void> asyncHandleLaunch({bool isRefreshLaunch = false}) async {
- try {
- final data = isRefreshLaunch
- ? await refreshLaunch()
- : await launch(isCache: true);
- final isVpnRunning = await BaseCoreApi().isConnected() ?? false;
- if (!isVpnRunning) {
- await checkUpdate();
- // 下载smartgeo文件
- GeoDownloader().downloadSmartGeo(smartGeo: data.appConfig!.smartGeo!);
- }
- } catch (e, s) {
- if (IXSP.getLastIsUserDisabled()) {
- if (!isShowDisabled) {
- Get.offAll(
- () => CountryRestrictedOverlay(
- type: RestrictedType.user,
- onPressed: () async {
- // 清除LaunchData
- await IXSP.clearLaunchData();
- // 清除禁用状态
- IXSP.setLastIsUserDisabled(false);
- // 发送事件
- },
- ),
- transition: Transition.fadeIn,
- );
- }
- return;
- } else if (IXSP.getLastIsRegionDisabled()) {
- if (!isShowDisabled) {
- Get.offAll(
- () => const CountryRestrictedOverlay(type: RestrictedType.region),
- transition: Transition.fadeIn,
- );
- }
- return;
- } else if (IXSP.getLastIsDeviceDisabled()) {
- if (!isShowDisabled) {
- Get.offAll(
- () => const CountryRestrictedOverlay(type: RestrictedType.device),
- transition: Transition.fadeIn,
- );
- }
- return;
- }
- final isVpnRunning = await BaseCoreApi().isConnected() ?? false;
- if (!isVpnRunning) {
- await checkUpdate();
- }
- handleSnackBarError(e, s);
- }
- }
- void handleSnackBarError(dynamic error, StackTrace stackTrace) {
- if (error is ApiException) {
- IXSnackBar.showIXErrorSnackBar(
- title: Strings.error.tr,
- message: error.message,
- );
- } else if (error is Failure) {
- IXSnackBar.showIXErrorSnackBar(
- title: Strings.error.tr,
- message: error.message ?? Strings.unknownError.tr,
- );
- } else if (error is DioException) {
- switch (error.type) {
- case DioExceptionType.connectionError:
- case DioExceptionType.connectionTimeout:
- case DioExceptionType.receiveTimeout:
- case DioExceptionType.sendTimeout:
- IXSnackBar.showIXErrorSnackBar(
- title: Strings.error.tr,
- message: Strings.unableToConnectNetwork.tr,
- );
- break;
- default:
- IXSnackBar.showIXErrorSnackBar(
- title: Strings.error.tr,
- message: Strings.unableToConnectServer.tr,
- );
- }
- } else {
- IXSnackBar.showIXErrorSnackBar(
- title: Strings.error.tr,
- message: error.toString(),
- );
- }
- }
- // 更新检查 - 智能时间控制版本
- Future<bool> checkUpdate({bool isClickCheck = false}) async {
- try {
- final upgrade = IXSP.getUpgrade();
- // 如果当前versionCode等于升级的versionCode,则没有更新
- if (upgrade?.versionCode == fp.appVersionCode) {
- return false;
- }
- var hasUpdate = false;
- var hasForceUpdate = false;
- if (upgrade != null) {
- if (upgrade.upgradeType == 1) {
- hasUpdate = true;
- }
- if (upgrade.forced == true) {
- hasForceUpdate = true;
- }
- }
- if (hasUpdate) {
- // 强制更新或用户主动点击检查时,直接显示更新弹窗
- if (hasForceUpdate || isClickCheck) {
- _showUpgradeDialog(upgrade!, hasForceUpdate);
- return hasUpdate;
- }
- // 检查版本是否变化(新版本则重置时间逻辑)
- final currentVersion = upgrade?.versionCode?.toString() ?? '';
- final lastNoticeVersion =
- IXSP.getString(SPKeys.lastUpgradeNoticeVersion) ?? '';
- final isNewVersion = currentVersion != lastNoticeVersion;
- if (isNewVersion) {
- // 新版本,直接显示弹窗
- log(
- TAG,
- 'New version detected: $currentVersion (last: $lastNoticeVersion)',
- );
- _showUpgradeDialog(upgrade!, hasForceUpdate);
- return hasUpdate;
- }
- // 检查是否超过 upgradeNoticeTime 分钟
- final appConfig = IXSP.getAppConfig();
- final upgradeNoticeMinutes =
- appConfig?.upgradeNoticeTime ?? 1440; // 默认1天
- final lastNoticeTimeStr =
- IXSP.getString(SPKeys.lastUpgradeNoticeTime) ?? '0';
- final lastNoticeTime = int.tryParse(lastNoticeTimeStr) ?? 0;
- final now = NtpTimeService().getCurrentTimestamp();
- final elapsedMinutes = (now - lastNoticeTime) / (1000 * 60);
- if (elapsedMinutes >= upgradeNoticeMinutes) {
- _showUpgradeDialog(upgrade!, hasForceUpdate);
- } else {
- log(
- TAG,
- 'Upgrade notice skipped: ${elapsedMinutes.toStringAsFixed(1)}min '
- 'elapsed, need ${upgradeNoticeMinutes}min',
- );
- }
- }
- return hasUpdate;
- } catch (e) {
- log(TAG, 'checkUpdate error: $e');
- }
- return false;
- }
- /// 显示更新弹窗并记录时间和版本
- void _showUpgradeDialog(Upgrade upgrade, bool hasForceUpdate) {
- AllDialog.showUpdate(upgrade, hasForceUpdate: hasForceUpdate);
- // 记录本次提醒时间
- IXSP.setString(
- SPKeys.lastUpgradeNoticeTime,
- NtpTimeService().getCurrentTimestamp().toString(),
- );
- // 记录本次提醒的版本号
- IXSP.setString(
- SPKeys.lastUpgradeNoticeVersion,
- upgrade.versionCode?.toString() ?? '',
- );
- }
- Future<Launch> getDispatchInfo(
- int locationId,
- String locationCode, {
- CancelToken? cancelToken,
- }) async {
- while (true) {
- try {
- ApiRouter().setbaseUrl(ApiDomains.instance.getRouterUrl());
- final request = fp.toJson();
- request['locationId'] = locationId;
- request['locationCode'] = locationCode;
- // 获取选中的路由模式
- final routingMode =
- IXSP.getString(SPKeys.routingModeSelected) ?? "smart";
- request['routingMode'] = routingMode;
- final result = await ApiRouter().getDispatchInfo(
- request,
- cancelToken: cancelToken,
- );
- if (!result.success) {
- throw Failure(
- code: result.errorCode ?? '',
- message: result.errorMessage ?? '',
- );
- }
- // 重置禁用状态
- IXSP.setLastIsRegionDisabled(false);
- IXSP.setLastIsUserDisabled(false);
- final launchData = Launch.fromJson(result.data);
- // 更新URL列表
- await ApiDomains.instance.updateFromLaunch(launchData);
- // 保存app配置
- await IXSP.saveAppConfig(launchData.appConfig!);
- return launchData;
- } on ApiException catch (_) {
- rethrow;
- } on Failure catch (_) {
- rethrow;
- } on DioException catch (e) {
- if (e.response?.statusCode == Errors.eRegionNotAvailable ||
- e.response?.statusCode == Errors.eUserDisabled ||
- e.response?.statusCode == Errors.eTokenExpired) {
- rethrow;
- } else {
- if (await NetworkHelper.instance.isNetworkAvailable()) {
- final url = await ApiDomains.instance.getNextRouterUrl();
- log(TAG, 'getDispatchInfo request failed for URL $url: $e');
- if (url.isEmpty) {
- rethrow;
- }
- ApiRouter().setbaseUrl(url);
- } else {
- rethrow;
- }
- }
- } catch (e) {
- final url = await ApiDomains.instance.getNextRouterUrl();
- log(TAG, 'getDispatchInfo request failed for URL $url: $e');
- if (url.isEmpty) {
- rethrow;
- }
- ApiRouter().setbaseUrl(url);
- }
- }
- }
- Future<Launch> register(Map<String, dynamic> params) async {
- while (true) {
- try {
- ApiCore().setbaseUrl(ApiDomains.instance.getApiUrl());
- final request = fp.toJson();
- request.addAll(params);
- final result = await ApiCore().register(request);
- if (!result.success) {
- throw Failure(
- code: result.errorCode ?? '',
- message: result.errorMessage ?? '',
- );
- }
- final launchData = Launch.fromJson(result.data);
- // 注册成功后上报firebase注册事件
- sendAnalytics(FirebaseEvent.register);
- // 保存 Launch 数据
- await IXSP.saveLaunch(launchData);
- // 初始化Launch
- await initData(launchData);
- return launchData;
- } on ApiException catch (_) {
- rethrow;
- } on Failure catch (_) {
- rethrow;
- } on DioException catch (e) {
- if (e.response?.statusCode == Errors.eRegionNotAvailable ||
- e.response?.statusCode == Errors.eUserDisabled ||
- e.response?.statusCode == Errors.eTokenExpired) {
- rethrow;
- } else {
- if (await NetworkHelper.instance.isNetworkAvailable()) {
- final url = await ApiDomains.instance.getNextApiUrl();
- log(TAG, 'Register request failed for URL $url: $e');
- if (url.isEmpty) {
- rethrow;
- }
- ApiCore().setbaseUrl(url);
- } else {
- rethrow;
- }
- }
- } catch (e) {
- final url = await ApiDomains.instance.getNextApiUrl();
- log(TAG, 'Register request failed for URL $url: $e');
- if (url.isEmpty) {
- rethrow;
- }
- ApiCore().setbaseUrl(url);
- }
- }
- }
- Future<Launch> login(Map<String, dynamic> params) async {
- while (true) {
- try {
- ApiCore().setbaseUrl(ApiDomains.instance.getApiUrl());
- final request = fp.toJson();
- request.addAll(params);
- final result = await ApiCore().login(request);
- if (!result.success) {
- throw Failure(
- code: result.errorCode ?? '',
- message: result.errorMessage ?? '',
- );
- }
- final launchData = Launch.fromJson(result.data);
- // 注册成功后上报firebase注册事件
- sendAnalytics(FirebaseEvent.login);
- // 保存 Launch 数据
- await IXSP.saveLaunch(launchData);
- // 初始化Launch
- await initData(launchData);
- return launchData;
- } on ApiException catch (_) {
- rethrow;
- } on Failure catch (_) {
- rethrow;
- } on DioException catch (e) {
- if (e.response?.statusCode == Errors.eRegionNotAvailable ||
- e.response?.statusCode == Errors.eUserDisabled ||
- e.response?.statusCode == Errors.eTokenExpired) {
- rethrow;
- } else {
- if (await NetworkHelper.instance.isNetworkAvailable()) {
- final url = await ApiDomains.instance.getNextApiUrl();
- log(TAG, 'Login request failed for URL $url: $e');
- if (url.isEmpty) {
- rethrow;
- }
- ApiCore().setbaseUrl(url);
- } else {
- rethrow;
- }
- }
- } catch (e) {
- final url = await ApiDomains.instance.getNextApiUrl();
- log(TAG, 'Login request failed for URL $url: $e');
- if (url.isEmpty) {
- rethrow;
- }
- ApiCore().setbaseUrl(url);
- }
- }
- }
- Future<Launch> logout() async {
- try {
- final request = fp.toJson();
- final result = await ApiCore().logout(request);
- if (!result.success) {
- throw Failure(
- code: result.errorCode ?? '',
- message: result.errorMessage ?? '',
- );
- }
- final launchData = Launch.fromJson(result.data);
- // 登出成功后上报firebase登出事件
- sendAnalytics(FirebaseEvent.logout);
- // 保存 Launch 数据
- await IXSP.saveLaunch(launchData);
- await initData(launchData);
- return launchData;
- } catch (e) {
- rethrow;
- }
- }
- Future<Launch> deleteAccount() async {
- try {
- final request = fp.toJson();
- final result = await ApiCore().deleteAccount(request);
- if (!result.success) {
- throw Failure(
- code: result.errorCode ?? '',
- message: result.errorMessage ?? '',
- );
- }
- final launchData = Launch.fromJson(result.data);
- // 登出成功后上报firebase登出事件
- sendAnalytics(FirebaseEvent.deleteAccount);
- // 保存 Launch 数据
- await IXSP.saveLaunch(launchData);
- await initData(launchData);
- return launchData;
- } catch (e) {
- rethrow;
- }
- }
- Future<String> changePassword(Map<String, dynamic> params) async {
- try {
- final request = fp.toJson();
- request.addAll(params);
- final result = await ApiCore().changePassword(request);
- if (!result.success) {
- throw Failure(
- code: result.errorCode ?? '',
- message: result.errorMessage ?? '',
- );
- }
- return result.errorMessage ?? '';
- } catch (e) {
- rethrow;
- }
- }
- Future<Groups> getLocations() async {
- while (true) {
- try {
- ApiCore().setbaseUrl(ApiDomains.instance.getApiUrl());
- final request = fp.toJson();
- final result = await ApiCore().getLocations(request);
- if (!result.success) {
- throw Failure(
- code: result.errorCode ?? '',
- message: result.errorMessage ?? '',
- );
- }
- final groups = Groups.fromJson(result.data);
- await IXSP.saveGroups(groups);
- return groups;
- } on ApiException catch (_) {
- rethrow;
- } on Failure catch (_) {
- rethrow;
- } on DioException catch (e) {
- if (e.response?.statusCode == Errors.eRegionNotAvailable ||
- e.response?.statusCode == Errors.eUserDisabled ||
- e.response?.statusCode == Errors.eTokenExpired) {
- rethrow;
- } else {
- if (await NetworkHelper.instance.isNetworkAvailable()) {
- final url = await ApiDomains.instance.getNextApiUrl();
- log(TAG, 'getLocations request failed for URL $url: $e');
- if (url.isEmpty) {
- rethrow;
- }
- ApiCore().setbaseUrl(url);
- } else {
- rethrow;
- }
- }
- } catch (e) {
- final url = await ApiDomains.instance.getNextApiUrl();
- log(TAG, 'getLocations request failed for URL $url: $e');
- if (url.isEmpty) {
- rethrow;
- }
- ApiCore().setbaseUrl(url);
- }
- }
- }
- Future<List<ChannelPlan>> getChannelPlanList() async {
- try {
- final request = fp.toJson();
- final result = await ApiCore().getChannelPlanList(request);
- if (!result.success) {
- throw Failure(
- code: result.errorCode ?? '',
- message: result.errorMessage ?? '',
- );
- }
- final channelPlanList = ChannelPlanList.fromJson(result.data);
- return channelPlanList.list ?? [];
- } catch (e) {
- rethrow;
- }
- }
- Future<Launch> subscribe(Map<String, dynamic> params) async {
- try {
- final request = fp.toJson();
- request.addAll(params);
- final result = await ApiCore().subscribe(request);
- if (!result.success) {
- throw Failure(
- code: result.errorCode ?? '',
- message: result.errorMessage ?? '',
- );
- }
- final launchData = Launch.fromJson(result.data);
- // 登出成功后上报firebase登出事件
- sendAnalytics(FirebaseEvent.subscribe);
- // 保存 Launch 数据
- await IXSP.saveLaunch(launchData);
- // 初始化Launch
- await initData(launchData);
- return launchData;
- } catch (e) {
- rethrow;
- }
- }
- Future<Launch> restore() async {
- try {
- final request = fp.toJson();
- final result = await ApiCore().restore(request);
- if (!result.success) {
- throw Failure(
- code: result.errorCode ?? '',
- message: result.errorMessage ?? '',
- );
- }
- final launchData = Launch.fromJson(result.data);
- // 登出成功后上报firebase登出事件
- sendAnalytics(FirebaseEvent.restore);
- // 保存 Launch 数据
- await IXSP.saveLaunch(launchData);
- // 初始化Launch
- await initData(launchData);
- return launchData;
- } catch (e) {
- rethrow;
- }
- }
- Future<void> connected(Map<String, dynamic> params) async {
- try {
- final request = fp.toJson();
- request.addAll(params);
- final result = await ApiRouter().connected(request);
- if (!result.success) {
- throw Failure(
- code: result.errorCode ?? '',
- message: result.errorMessage ?? '',
- );
- }
- } catch (e) {
- rethrow;
- }
- }
- Future<BannerList> getBanner({String position = "banner"}) async {
- try {
- final request = fp.toJson();
- request["position"] = position;
- final result = await ApiCore().getBanner(request);
- if (!result.success) {
- throw Failure(
- code: result.errorCode ?? '',
- message: result.errorMessage ?? '',
- );
- }
- final bannerList = BannerList.fromJson(result.data);
- return bannerList;
- } catch (e) {
- rethrow;
- }
- }
- /// 上传 Boost 日志(读取 app.json 和 core.json 并组合上传)
- /// 1秒内只允许调用一次
- Future<void> uploadBoostLog() async {
- // 节流检查:1秒内只允许调用一次
- final now = DateTime.now();
- if (_lastUploadBoostLogTime != null &&
- now.difference(_lastUploadBoostLogTime!).inMilliseconds < 1000) {
- log(TAG, 'uploadBoostLog throttled, skip');
- return;
- }
- _lastUploadBoostLogTime = now;
- try {
- // 读取 app.json 内容
- Map<String, dynamic> appLog = {};
- final appLogPath = await BoostReportManager().getAppLogFilePath();
- if (appLogPath != null) {
- final appFile = File(appLogPath);
- if (await appFile.exists()) {
- final content = await appFile.readAsString();
- if (content.isNotEmpty) {
- appLog = jsonDecode(content) as Map<String, dynamic>;
- }
- }
- }
- // 读取 core.json 内容
- Map<String, dynamic> coreLog = {};
- final coreLogPath = await BoostReportManager().getCoreLogFilePath();
- if (coreLogPath != null) {
- final coreFile = File(coreLogPath);
- if (await coreFile.exists()) {
- final content = await coreFile.readAsString();
- if (content.isNotEmpty) {
- coreLog = jsonDecode(content) as Map<String, dynamic>;
- }
- }
- }
- // 如果两个日志都为空,则不上传
- if (appLog.isEmpty && coreLog.isEmpty) {
- log(TAG, 'Both app log and core log are empty, skip upload');
- return;
- }
- // 组装日志数据
- final logItem = {
- 'id': const Uuid().v4(),
- 'time': DateTime.now().millisecondsSinceEpoch,
- 'level': LogLevel.info.name,
- 'module': LogModule.NM_Metrics.name,
- 'category': Configs.productCode,
- 'fields': {'appLog': appLog, 'coreLog': coreLog},
- };
- // 上传日志
- await uploadMetricsLog([logItem]);
- await uploadLocalLog(appLog['sessionInfo']?['boostSessionId'] ?? '');
- log(TAG, 'Boost log uploaded successfully');
- } catch (e) {
- log(TAG, 'uploadBoostLog error: $e');
- }
- }
- Future<void> uploadMetricsLog(List<Map<String, dynamic>> logs) async {
- try {
- await uploadLogs(logs, isCache: true);
- } catch (e) {
- log(TAG, 'uploadMetricsLog error: $e');
- }
- }
- Future<void> uploadLocalLog(String boostSessionId) async {
- try {
- final appDir = await getApplicationSupportDirectory();
- final logDir = Directory('${appDir.path}/logs');
- if (await logDir.exists()) {
- final filePaths = <String>[];
- // 遍历 logDir 目录,查找 client_ 和 service_ 开头的文件
- await for (final entity in logDir.list()) {
- if (entity is File) {
- final fileName = entity.path.split('/').last;
- if (fileName.startsWith('client_') ||
- fileName.startsWith('service_')) {
- filePaths.add(entity.path);
- }
- }
- }
- if (filePaths.isEmpty) {
- log(TAG, 'No client_ or service_ log files found');
- return;
- }
- final gzipFilePath = await GzipManager().generateAndShareGzip(
- filePaths: filePaths,
- gzipFileName: '$boostSessionId.tar.gz',
- );
- if (gzipFilePath == null) {
- return;
- }
- await uploadLogFile(
- gzipFilePath,
- boostSessionId,
- LogModule.NM_Log.name,
- );
- }
- } catch (e) {
- rethrow;
- }
- }
- Future<String> uploadLogs(List<dynamic> items, {bool isCache = false}) async {
- await updateFingerprintData();
- Map<String, dynamic> request = fp.toJson();
- request['items'] = items;
- while (true) {
- try {
- ApiLog().setbaseUrl(ApiDomains.instance.getLogUrl());
- final result = await ApiLog().uploadLogs(request);
- if (!result.success) {
- throw Failure(
- code: result.errorCode ?? '',
- message: result.errorMessage ?? '',
- );
- }
- return result.errorMessage ?? '';
- } on ApiException catch (_) {
- rethrow;
- } on Failure catch (_) {
- rethrow;
- } on DioException catch (e) {
- if (e.response?.statusCode == Errors.eRegionNotAvailable ||
- e.response?.statusCode == Errors.eUserDisabled ||
- e.response?.statusCode == Errors.eTokenExpired) {
- if (isCache) {
- await _cacheLogRequest(items);
- }
- rethrow;
- } else {
- if (await NetworkHelper.instance.isNetworkAvailable()) {
- final url = await ApiDomains.instance.getNextLogUrl();
- if (url.isEmpty) {
- if (isCache) {
- await _cacheLogRequest(items);
- }
- rethrow;
- }
- log(
- TAG,
- 'uploadLogs request failed for URL ${ApiLog().baseUrl}: $e',
- );
- ApiLog().setbaseUrl(url);
- } else {
- if (isCache) {
- await _cacheLogRequest(items);
- }
- rethrow;
- }
- }
- } catch (e) {
- final url = await ApiDomains.instance.getNextLogUrl();
- if (url.isEmpty) {
- if (isCache) {
- await _cacheLogRequest(items);
- }
- rethrow;
- }
- log(TAG, 'uploadLogs request failed for URL ${ApiLog().baseUrl}: $e');
- ApiLog().setbaseUrl(url);
- }
- }
- }
- // 上传文件
- Future<String> uploadLogFile(
- String filePath,
- String fileName,
- String logType, {
- bool isCache = false,
- }) async {
- try {
- Map<String, dynamic> request = fp.toJson();
- final data = jsonEncode(request);
- while (true) {
- try {
- ApiFile().setbaseUrl(ApiDomains.instance.getFileUrl());
- final result = await ApiFile().uploadLogFile(
- filePath,
- fileName,
- logType,
- data,
- );
- if (!result.success) {
- throw Failure(
- code: result.errorCode ?? '',
- message: result.errorMessage ?? '',
- );
- }
- return result.errorMessage ?? '';
- } on ApiException catch (_) {
- rethrow;
- } on Failure catch (_) {
- rethrow;
- } on DioException catch (e) {
- if (e.response?.statusCode == Errors.eRegionNotAvailable ||
- e.response?.statusCode == Errors.eUserDisabled ||
- e.response?.statusCode == Errors.eTokenExpired) {
- if (isCache) {
- await _cacheFileLogRequest(filePath, fileName);
- }
- rethrow;
- } else {
- if (await NetworkHelper.instance.isNetworkAvailable()) {
- final url = await ApiDomains.instance.getNextFileUrl();
- if (url.isEmpty) {
- if (isCache) {
- await _cacheFileLogRequest(filePath, fileName);
- }
- rethrow;
- }
- log('uploadLogs request failed for URL ${ApiLog().baseUrl}: $e');
- ApiLog().setbaseUrl(url);
- } else {
- if (isCache) {
- await _cacheFileLogRequest(filePath, fileName);
- }
- rethrow;
- }
- }
- } catch (e) {
- final url = await ApiDomains.instance.getNextFileUrl();
- if (url.isEmpty) {
- if (isCache) {
- await _cacheFileLogRequest(filePath, fileName);
- }
- rethrow;
- }
- log('uploadLogs request failed for URL ${ApiLog().baseUrl}: $e');
- ApiLog().setbaseUrl(url);
- }
- }
- } catch (e) {
- rethrow;
- }
- }
- // 缓存文件日志请求数据
- Future<void> _cacheFileLogRequest(String filePath, String fileName) async {
- try {
- final dir = await getApplicationSupportDirectory();
- final cacheDir = Directory('${dir.path}/file_log_cache');
- if (!await cacheDir.exists()) {
- await cacheDir.create(recursive: true);
- }
- // 复制文件到缓存目录
- final file = File(filePath);
- await file.copy('${cacheDir.path}/${file.path.split('/').last}');
- // 清理缓存目录
- await _cleanupFileCacheDirectory(cacheDir);
- } catch (e) {
- log('Failed to cache file log request: $e');
- }
- }
- // 清理文件缓存目录
- Future<void> _cleanupFileCacheDirectory(Directory cacheDir) async {
- try {
- const maxFiles = 10; // 最多保留10个文件
- final files = await cacheDir.list().toList();
- // 按修改时间排序
- files.sort(
- (a, b) => a.statSync().modified.compareTo(b.statSync().modified),
- );
- // 如果超过文件数量或大小限制,删除最旧的文件
- while (files.length > maxFiles && files.isNotEmpty) {
- final oldestFile = files.removeAt(0);
- await oldestFile.delete();
- }
- } catch (e) {
- log('Failed to cleanup cache directory: $e');
- }
- }
- // 处理缓存的文件日志数据
- Future<void> processCachedFileLogs() async {
- try {
- final dir = await getApplicationSupportDirectory();
- final cacheDir = Directory('${dir.path}/file_log_cache');
- if (!await cacheDir.exists()) {
- return;
- }
- final files = await cacheDir.list().toList();
- if (files.isEmpty) {
- return;
- }
- // 按修改时间排序,先处理最旧的文件
- files.sort(
- (a, b) => a.statSync().modified.compareTo(b.statSync().modified),
- );
- for (var fileEntity in files) {
- try {
- if (fileEntity is! File) continue;
- final filePath = fileEntity.path;
- final fileName = fileEntity.path.split('/').last.split('.').first;
- // 尝试上传缓存的日志
- await uploadLogFile(filePath, fileName, LogModule.NM_Log.name);
- // 上传成功后删除缓存文件
- await fileEntity.delete();
- } catch (e) {
- log('Failed to process cached log file ${fileEntity.path}: $e');
- // 如果上传失败,保留文件等待下次尝试
- continue;
- }
- }
- } catch (e) {
- log('Failed to process cached file logs: $e');
- }
- }
- // 缓存日志请求数据
- Future<void> _cacheLogRequest(dynamic items) async {
- try {
- final dir = await getApplicationSupportDirectory();
- final cacheDir = Directory('${dir.path}/log_cache');
- if (!await cacheDir.exists()) {
- await cacheDir.create(recursive: true);
- }
- // 生成唯一的文件名
- final timestamp = DateTime.now().millisecondsSinceEpoch;
- final fileName = 'log_$timestamp.json';
- final file = File('${cacheDir.path}/$fileName');
- // 将请求数据写入文件
- await file.writeAsString(jsonEncode(items));
- // 清理缓存目录
- await _cleanupCacheDirectory(cacheDir);
- } catch (e) {
- log(TAG, 'Failed to cache log request: $e');
- }
- }
- // 清理缓存目录
- Future<void> _cleanupCacheDirectory(Directory cacheDir) async {
- try {
- const maxFiles = 10; // 最多保留10个文件
- const maxCacheSize = 5 * 1024 * 1024; // 5MB
- final files = await cacheDir.list().toList();
- // 按修改时间排序
- files.sort(
- (a, b) => a.statSync().modified.compareTo(b.statSync().modified),
- );
- // 计算当前缓存大小
- int totalSize = 0;
- for (var file in files) {
- totalSize += file.statSync().size;
- }
- // 如果超过文件数量或大小限制,删除最旧的文件
- while ((files.length > maxFiles || totalSize > maxCacheSize) &&
- files.isNotEmpty) {
- final oldestFile = files.removeAt(0);
- totalSize -= oldestFile.statSync().size;
- await oldestFile.delete();
- }
- } catch (e) {
- log(TAG, 'Failed to cleanup cache directory: $e');
- }
- }
- // 处理缓存的日志数据
- Future<void> processCachedLogs() async {
- try {
- final dir = await getApplicationSupportDirectory();
- final cacheDir = Directory('${dir.path}/log_cache');
- if (!await cacheDir.exists()) {
- return;
- }
- final files = await cacheDir.list().toList();
- if (files.isEmpty) {
- return;
- }
- // 按修改时间排序,先处理最旧的文件
- files.sort(
- (a, b) => a.statSync().modified.compareTo(b.statSync().modified),
- );
- for (var fileEntity in files) {
- try {
- if (fileEntity is! File) continue;
- final content = await fileEntity.readAsString();
- final items = jsonDecode(content);
- // 尝试上传缓存的日志
- await uploadLogs(items, isCache: false);
- // 上传成功后删除缓存文件
- await fileEntity.delete();
- } catch (e) {
- log('Failed to process cached log file ${fileEntity.path}: $e');
- // 如果上传失败,保留文件等待下次尝试
- continue;
- }
- }
- } catch (e) {
- log('Failed to process cached logs: $e');
- }
- }
- Future<void> uploadApiStatisticsLog(
- List<Map<String, dynamic>> logs, {
- LogModule module = LogModule.NM_ApiLaunchLog,
- }) async {
- if (isNeedUploadLogs(module)) {
- await uploadLogs(logs);
- }
- }
- // 判断是否需要上传日志
- bool isNeedUploadLogs(LogModule module) {
- final launch = IXSP.getLaunch();
- if (launch == null) {
- return false;
- }
- if (launch.appConfig?.disabledLogModules?.contains(module.name) ?? false) {
- return false;
- }
- return true;
- }
- /// 获取订阅周期类型文本
- /// subscribeType: 1Day 2Week 3Month 4Year
- String _getSubscribeTypeText() {
- final user = IXSP.getUser();
- final planInfo = user?.planInfo;
- // 仅当 isSubscribe=true 时有效
- if (planInfo?.isSubscribe != true) {
- return planInfo?.subTitle ?? '';
- }
- switch (planInfo?.subscribeType) {
- case 1:
- return 'Day';
- case 2:
- return 'Week';
- case 3:
- return 'Month';
- case 4:
- return 'Year';
- default:
- return '';
- }
- }
- /// 获取过期时间文本
- String _getExpireTimeText() {
- final user = IXSP.getUser();
- final expireTime = user?.expireTime;
- if (expireTime == null || expireTime == 0) {
- return '';
- }
- // 时间戳转日期(秒级时间戳)
- final date = DateTime.fromMillisecondsSinceEpoch(expireTime * 1000);
- final formatted =
- "${date.year.toString().padLeft(4, '0')}-"
- "${date.month.toString().padLeft(2, '0')}-"
- "${date.day.toString().padLeft(2, '0')}";
- return formatted;
- }
- /// 获取有效期显示文本
- String _getValidTermText() {
- final subscribeType = _getSubscribeTypeText();
- final expireTime = _getExpireTimeText();
- if (subscribeType.isNotEmpty && expireTime.isNotEmpty) {
- return '$subscribeType / $expireTime';
- } else if (expireTime.isNotEmpty) {
- return expireTime;
- }
- return '';
- }
- /// 启动剩余时间倒计时
- /// [seconds] 剩余时间(秒),例如:3600 表示 1 小时
- void startRemainTimeCountdown(int seconds) {
- // 取消之前的定时器
- _remainTimeTimer?.cancel();
- // 设置初始剩余时间
- _remainTimeSeconds = seconds;
- // 立即更新格式化文案
- _updateRemainTimeFormatted();
- // 启动每秒倒计时
- _remainTimeTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
- if (_remainTimeSeconds > 0) {
- _remainTimeSeconds--;
- // 只有当格式化文案变化时才更新 UI
- _updateRemainTimeFormatted();
- } else {
- // 倒计时结束
- timer.cancel();
- _remainTimeTimer = null;
- _onRemainTimeExpired();
- }
- });
- }
- /// 更新格式化后的剩余时间(只有文案变化时才触发 UI 更新)
- void _updateRemainTimeFormatted() {
- final newFormatted = _formatRemainTime(_remainTimeSeconds);
- if (_remainTimeFormatted.value != newFormatted) {
- _remainTimeFormatted.value = newFormatted;
- }
- // 更新是否显示倒计时的状态
- final vipRemainNoticeSeconds =
- (IXSP.getAppConfig()?.vipRemainNotice ?? 600) * 60;
- final newShouldShow =
- _remainTimeSeconds > 0 && _remainTimeSeconds < vipRemainNoticeSeconds;
- if (_shouldShowCountdown.value != newShouldShow) {
- _shouldShowCountdown.value = newShouldShow;
- }
- }
- /// 停止剩余时间倒计时
- void stopRemainTimeCountdown() {
- _remainTimeTimer?.cancel();
- _remainTimeTimer = null;
- }
- /// 更新剩余时间(从服务器获取新的时间后调用)
- void updateRemainTime(int seconds) {
- stopRemainTimeCountdown();
- if (seconds > 0) {
- startRemainTimeCountdown(seconds);
- } else {
- _remainTimeSeconds = 0;
- _updateRemainTimeFormatted();
- }
- }
- /// 剩余时间到期处理
- void _onRemainTimeExpired() {
- log(TAG, 'VIP剩余时间已到期');
- // 可以在这里添加到期后的处理逻辑,例如:
- // - 显示续费提示
- // - 断开VPN连接
- // - 刷新用户信息
- refreshLaunch();
- }
- /// 格式化剩余时间显示
- String _formatRemainTime(int totalSeconds) {
- if (totalSeconds <= 0) {
- return '--';
- }
- final days = totalSeconds ~/ 86400;
- final hours = (totalSeconds % 86400) ~/ 3600;
- final minutes = (totalSeconds % 3600) ~/ 60;
- final seconds = totalSeconds % 60;
- // 大于1天
- if (days > 1) {
- return '$days days';
- }
- // 等于1天
- if (days == 1) {
- return '1 day';
- }
- // 大于1小时
- if (hours >= 1) {
- return '$hours h';
- }
- // 小于1小时,显示 mm:ss
- return '${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
- }
- }
|