app_router.dart 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import 'package:auto_route/auto_route.dart';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:flutter_native_splash/flutter_native_splash.dart';
  4. import 'package:telnow_mobile_new/app_router.gr.dart';
  5. import 'package:telnow_mobile_new/src/api/api_auth_provider.dart';
  6. import 'package:telnow_mobile_new/src/injector/injector.dart';
  7. import 'package:telnow_mobile_new/src/storage/sharedpreferences/shared_preferences_manager.dart';
  8. import 'package:telnow_mobile_new/src/utils/U.dart';
  9. @AutoRouterConfig(replaceInRouteName: 'Screen|Page,Route')
  10. class AppRouter extends RootStackRouter {
  11. final SharedPreferencesManager _sharedPreferencesManager = locator<SharedPreferencesManager>();
  12. @override
  13. RouteType get defaultRouteType => RouteType.material(); //.cupertino, .adaptive ..etc
  14. @override
  15. List<AutoRoute> get routes => [
  16. AutoRoute(path: '/qr', page: NewQrRoute.page, guards: qrGuards, initial: true),
  17. AutoRoute(path: '/change-code/:pid', page: ChangeCodeRoute.page),
  18. AutoRoute(
  19. path: '/app/:pid', page: HomeGuardRoute.page, guards: appGuards,
  20. children: [
  21. AutoRoute(path: 'login', page: LoginRoute.page, guards: loginGuards, initial: true),
  22. AutoRoute(path: 'loginme', page: AutoLoginRoute.page, guards: loginGuards),
  23. AutoRoute(path: 'license', page: ErrorRoute.page),
  24. AutoRoute(
  25. path: 'menu', page: AppResponsive.page, guards: menuGuards,
  26. children: [
  27. AutoRoute(path: 'home', page: HomeResponsive.page, initial: true),
  28. AutoRoute(path: 'history', page: HistoryResponsive.page),
  29. AutoRoute(path: 'account', page: AccountResponsive.page),
  30. ]
  31. ),
  32. ]
  33. ),
  34. ];
  35. List<AutoRouteGuard> get qrGuards => [
  36. AutoRouteGuard.simple((resolver, router) async{
  37. Parameters param = resolver.route.queryParams;
  38. router.removeLast();
  39. if (!kIsWeb && U.getAccessCode() != null && param.isEmpty) {
  40. router.pushPath('/app/${Uri.encodeComponent(U.getAccessCode()!)}');
  41. } else{
  42. FlutterNativeSplash.remove();
  43. resolver.next();
  44. }
  45. })
  46. ];
  47. List<AutoRouteGuard> get appGuards => [
  48. AutoRouteGuard.simple((resolver, router) async{
  49. RouteMatch<dynamic> vRedirector = resolver.route;
  50. String? rawPid = '';
  51. var pid;
  52. bool stb = false;
  53. rawPid = vRedirector.pathParams.getString('pid');
  54. try {
  55. pid = Uri.decodeComponent(rawPid);
  56. } catch (e) {
  57. print("err : $e");
  58. stb = true;
  59. }
  60. if (pid == null || stb) {
  61. router.pushPath("/qr?new=true");
  62. }
  63. if(vRedirector.fragment != '' && U.getAccessCode() != null){
  64. await _sharedPreferencesManager.clearKey(SharedPreferencesManager.keyBaseUrl);
  65. await _sharedPreferencesManager.clearKey(SharedPreferencesManager.keyAccessCode);
  66. await _sharedPreferencesManager.clearKey(SharedPreferencesManager.keySerialCode);
  67. await _sharedPreferencesManager.clearKey(SharedPreferencesManager.version);
  68. }
  69. if (U.getAccessCode() == null) {
  70. await _sharedPreferencesManager.putString(SharedPreferencesManager.keyBaseUrl, U.rewriteUrl(vRedirector.fragment));
  71. await _sharedPreferencesManager.putString(SharedPreferencesManager.keyAccessCode, pid);
  72. await _sharedPreferencesManager.putString(SharedPreferencesManager.keySerialCode, 'P-$pid}');
  73. try {
  74. var ver = await U.reloadLicense();
  75. await _sharedPreferencesManager.putInt(SharedPreferencesManager.version, ver['version']);
  76. rawPid = Uri.encodeComponent(pid);
  77. } catch (error) {
  78. await _sharedPreferencesManager.clearKey(SharedPreferencesManager.keyBaseUrl);
  79. await _sharedPreferencesManager.clearKey(SharedPreferencesManager.keyAccessCode);
  80. await _sharedPreferencesManager.clearKey(SharedPreferencesManager.keySerialCode);
  81. await _sharedPreferencesManager.clearKey(SharedPreferencesManager.version);
  82. router.pushPath("/qr?new=true");
  83. }
  84. } else if(_isNewAccessCode(rawPid, U.getAccessCode()!)){
  85. router.pushPath("/change-code/$rawPid");
  86. return;
  87. } else {
  88. if (_sharedPreferencesManager.getInt(SharedPreferencesManager.version) == null){
  89. try {
  90. var ver = await U.reloadLicense();
  91. await _sharedPreferencesManager.putInt(SharedPreferencesManager.version, ver['version']);
  92. } catch (e) {
  93. print('error sini 2: ${e.toString()}');
  94. await _sharedPreferencesManager.clearKey(SharedPreferencesManager.keyBaseUrl);
  95. await _sharedPreferencesManager.clearKey(SharedPreferencesManager.keyAccessCode);
  96. await _sharedPreferencesManager.clearKey(SharedPreferencesManager.keySerialCode);
  97. await _sharedPreferencesManager.clearKey(SharedPreferencesManager.version);
  98. router.pushPath("/qr?new=true");
  99. }
  100. }
  101. }
  102. FlutterNativeSplash.remove();
  103. resolver.next();
  104. })
  105. ];
  106. List<AutoRouteGuard> get loginGuards => [
  107. AutoRouteGuard.simple((resolver, router) async{
  108. router.removeLast();
  109. if (!_isValidVersion()) {
  110. router.pushPath('/app/${Uri.encodeComponent(U.getAccessCode()!)}/license');
  111. } else if (_isLoggedIn()) {
  112. !kIsWeb? router.pushAndPopUntil(AppResponsive(), predicate: (_) => false) : router.pushPath('/app/${Uri.encodeComponent(U.getAccessCode()!)}/menu');
  113. } else{
  114. resolver.next();
  115. }
  116. })
  117. ];
  118. List<AutoRouteGuard> get menuGuards => [
  119. AutoRouteGuard.simple((resolver, router) async{
  120. router.removeLast();
  121. if (!_isValidVersion()) {
  122. router.pushPath('/app/${Uri.encodeComponent(U.getAccessCode()!)}/license');
  123. } else if (!_isLoggedIn()) {
  124. router.pushPath('/app/${Uri.encodeComponent(U.getAccessCode()!)}/login');
  125. } else{
  126. resolver.next();
  127. }
  128. })
  129. ];
  130. bool _isLoggedIn() {
  131. bool? isTokenExist = _sharedPreferencesManager.isKeyExists(SharedPreferencesManager.keyAccessToken);
  132. String? token = _sharedPreferencesManager.getString(SharedPreferencesManager.keyAccessToken);
  133. bool? isLoggedIn = _sharedPreferencesManager.isKeyExists(SharedPreferencesManager.keyIsLogin);
  134. return isLoggedIn! && isTokenExist! && token != null;
  135. }
  136. bool _isValidVersion() {
  137. return ApiAuthProvider().currentVersion == U.getVersion();
  138. }
  139. bool _isNewAccessCode(String pid, String savedPid) {
  140. try{
  141. return U.decodeBase64Url(pid) == U.decodeBase64Url(Uri.decodeComponent(savedPid)) ? false : true;
  142. } catch (e){
  143. return false;
  144. }
  145. }
  146. }