splash_controller.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import 'package:nomo/app/base/base_controller.dart';
  4. import 'package:package_info_plus/package_info_plus.dart';
  5. import '../../../../utils/geo_downloader.dart';
  6. import '../../../../utils/log/logger.dart';
  7. import '../../../components/country_restricted_overlay.dart';
  8. import '../../../constants/enums.dart';
  9. import '../../../controllers/api_controller.dart';
  10. import '../../../data/models/launch/launch.dart';
  11. import '../../../data/sp/ix_sp.dart';
  12. import '../../../routes/app_pages.dart';
  13. class SplashController extends BaseController {
  14. static const String TAG = 'SplashController';
  15. final _apiController = Get.find<ApiController>();
  16. final _showLoading = false.obs;
  17. bool get showLoading => _showLoading.value;
  18. set showLoading(bool value) => _showLoading.value = value;
  19. // 是否已经登录
  20. final _hasLogin = true.obs;
  21. bool get hasLogin => _hasLogin.value;
  22. set hasLogin(bool value) => _hasLogin.value = value;
  23. final _versionName = ''.obs;
  24. String get versionName => _versionName.value;
  25. set versionName(String value) => _versionName.value = value;
  26. @override
  27. void onInit() {
  28. super.onInit();
  29. getVersionInfo();
  30. initApiInfo();
  31. // Get.offAllNamed(Routes.HOME);
  32. // Get.to(
  33. // () => CountryRestrictedOverlay(type: RestrictedType.network),
  34. // transition: Transition.fadeIn,
  35. // );
  36. // Get.to(
  37. // () => const ProtocolOverlay(),
  38. // transition: Transition.native,
  39. // curve: Curves.easeInOut,
  40. // );
  41. }
  42. void getVersionInfo() async {
  43. // versionCode =
  44. // await PackageInfo.fromPlatform().then((value) => value.buildNumber);
  45. versionName = await PackageInfo.fromPlatform().then(
  46. (value) => value.version,
  47. );
  48. }
  49. Future<void> initApiInfo() async {
  50. try {
  51. await _apiController.initFingerprint();
  52. final launch = IXSP.getLaunch();
  53. if (launch != null) {
  54. _apiController.fp.exData = launch.exData;
  55. _apiController.initLaunch(launch);
  56. handleLaunch(launch, isFirstRequest: false);
  57. _apiController.asyncHandleLaunch();
  58. } else {
  59. showLoading = true;
  60. try {
  61. final launch = await _apiController.launch();
  62. handleLaunch(launch);
  63. // 下载smartgeo文件
  64. GeoDownloader().downloadSmartGeo(
  65. smartGeo: launch.appConfig!.smartGeo!,
  66. );
  67. } catch (e, st) {
  68. handleLaunchError(e, st);
  69. } finally {
  70. showLoading = false;
  71. }
  72. }
  73. } catch (e, s) {
  74. handleLaunchError(e, s);
  75. }
  76. }
  77. void handleLaunch(Launch launch, {bool isFirstRequest = true}) {
  78. if (launch.userConfig != null && launch.appConfig != null) {
  79. final user = launch.userConfig!;
  80. final appConfig = launch.appConfig!;
  81. switch (MemberLevel.fromLevel(user.memberLevel)) {
  82. case MemberLevel.guest:
  83. //游客禁用,必须登录
  84. if (appConfig.visitorDisabled ?? true) {
  85. // 延迟1秒后设置为未登录
  86. Future.delayed(const Duration(seconds: 1), () {
  87. hasLogin = false;
  88. });
  89. } else {
  90. //允许游客访问,直接进入主页
  91. Get.offAllNamed(Routes.HOME);
  92. }
  93. break;
  94. default:
  95. //登录用户,直接进入主页
  96. Get.offAllNamed(Routes.HOME);
  97. break;
  98. }
  99. }
  100. }
  101. Future<void> handleLaunchError(dynamic e, StackTrace s) async {
  102. if (IXSP.getLastIsUserDisabled()) {
  103. if (!_apiController.isShowDisabled) {
  104. Get.offAll(
  105. () => CountryRestrictedOverlay(
  106. type: RestrictedType.user,
  107. onPressed: () async {
  108. // 清除LaunchData
  109. await IXSP.clearLaunchData();
  110. // 清除禁用状态
  111. IXSP.setLastIsUserDisabled(false);
  112. // 发送事件
  113. },
  114. ),
  115. transition: Transition.fadeIn,
  116. );
  117. }
  118. return;
  119. } else if (IXSP.getLastIsRegionDisabled()) {
  120. if (!_apiController.isShowDisabled) {
  121. Get.offAll(
  122. () => const CountryRestrictedOverlay(type: RestrictedType.region),
  123. transition: Transition.fadeIn,
  124. );
  125. }
  126. return;
  127. } else if (IXSP.getLastIsDeviceDisabled()) {
  128. if (!_apiController.isShowDisabled) {
  129. Get.offAll(
  130. () => const CountryRestrictedOverlay(type: RestrictedType.device),
  131. transition: Transition.fadeIn,
  132. );
  133. }
  134. return;
  135. }
  136. handleSnackBarError(e, s);
  137. log(TAG, 'initApiInfo error: $e');
  138. final launch = IXSP.getLaunch();
  139. if (launch != null) {
  140. _apiController.initLaunch(launch);
  141. handleLaunch(launch);
  142. } else {
  143. Get.to(
  144. () => CountryRestrictedOverlay(
  145. type: RestrictedType.network,
  146. onPressed: () async {
  147. Navigator.pop(Get.context!);
  148. initApiInfo();
  149. },
  150. ),
  151. transition: Transition.fadeIn,
  152. );
  153. }
  154. }
  155. }