splash_controller.dart 4.6 KB

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