splash_controller.dart 4.8 KB

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