splash_controller.dart 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. Get.offAllNamed(Routes.HOME);
  89. });
  90. } else {
  91. //允许游客访问,直接进入主页
  92. Get.offAllNamed(Routes.HOME);
  93. }
  94. break;
  95. default:
  96. //登录用户,直接进入主页
  97. Get.offAllNamed(Routes.HOME);
  98. break;
  99. }
  100. }
  101. }
  102. Future<void> handleLaunchError(dynamic e, StackTrace s) async {
  103. if (IXSP.getLastIsUserDisabled()) {
  104. if (!_apiController.isShowDisabled) {
  105. Get.offAll(
  106. () => CountryRestrictedOverlay(
  107. type: RestrictedType.user,
  108. onPressed: () async {
  109. // 清除LaunchData
  110. await IXSP.clearLaunchData();
  111. // 清除禁用状态
  112. IXSP.setLastIsUserDisabled(false);
  113. // 发送事件
  114. },
  115. ),
  116. transition: Transition.fadeIn,
  117. );
  118. }
  119. return;
  120. } else if (IXSP.getLastIsRegionDisabled()) {
  121. if (!_apiController.isShowDisabled) {
  122. Get.offAll(
  123. () => const CountryRestrictedOverlay(type: RestrictedType.region),
  124. transition: Transition.fadeIn,
  125. );
  126. }
  127. return;
  128. } else if (IXSP.getLastIsDeviceDisabled()) {
  129. if (!_apiController.isShowDisabled) {
  130. Get.offAll(
  131. () => const CountryRestrictedOverlay(type: RestrictedType.device),
  132. transition: Transition.fadeIn,
  133. );
  134. }
  135. return;
  136. }
  137. handleSnackBarError(e, s);
  138. log(TAG, 'initApiInfo error: $e');
  139. final launch = IXSP.getLaunch();
  140. if (launch != null) {
  141. _apiController.initLaunch(launch);
  142. handleLaunch(launch);
  143. } else {
  144. Get.to(
  145. () => CountryRestrictedOverlay(
  146. type: RestrictedType.network,
  147. onPressed: () async {
  148. Navigator.pop(Get.context!);
  149. initApiInfo();
  150. },
  151. ),
  152. transition: Transition.fadeIn,
  153. );
  154. }
  155. }
  156. }