| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221 |
- 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 '../../config/translations/localization_service.dart';
- import '../../config/translations/strings_enum.dart';
- import '../data/models/banner/banner_list.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;
- //是否是游客
- 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;
- @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;
- ApiStatistics.instance.onAppPaused();
- stopRemainTimeCountdown();
- } else if (state == AppLifecycleState.resumed) {
- if (isBackground) {
- isBackground = false;
- asyncHandleLaunch(isRefreshLaunch: true);
- ApiStatistics.instance.onAppResumed();
- updateRemainTime(IXSP.getUser()?.remainTime ?? 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);
- 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);
- 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();
- var hasUpdate = false;
- var hasForceUpdate = false;
- if (upgrade != null) {
- if (upgrade.upgradeType == 1) {
- hasUpdate = true;
- }
- if (upgrade.forced == true) {
- hasForceUpdate = true;
- }
- }
- if (hasUpdate) {
- AllDialog.showUpdate(hasForceUpdate: hasForceUpdate);
- }
- return hasUpdate;
- } catch (e) {
- log(TAG, 'checkUpdate error: $e');
- }
- return false;
- }
- 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;
- }
- }
- 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<void> _cacheLogRequest(dynamic items) async {
- try {
- final dir = await getApplicationDocumentsDirectory();
- 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> 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连接
- // - 刷新用户信息
- }
- /// 格式化剩余时间显示
- 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')}';
- }
- }
|