基础库
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

118 lines
4.1 KiB

  1. import 'dart:io';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:provider/provider.dart';
  5. import 'package:zhiying_comm/pages/login_page/account/login_account_page.dart';
  6. import 'package:zhiying_comm/pages/login_page/login_page.dart';
  7. import 'package:zhiying_comm/util/event_util/event_util.dart';
  8. import 'package:zhiying_comm/util/event_util/login_success_event.dart';
  9. import 'package:zhiying_comm/util/shared_prefe_util.dart';
  10. import 'package:zhiying_comm/zhiying_comm.dart';
  11. class RouterUtil {
  12. /*公共跳转
  13. * skipModel 跳转参数
  14. * data 额外参数
  15. * */
  16. static Future route(SkipModel skipModel, Map<String, dynamic> data, BuildContext context) async {
  17. if (skipModel.skipIdentifier == null || skipModel.skipIdentifier == '') {
  18. print('skipIdentifier 参数不存在,无法跳转页面');
  19. return Future.error('skipIdentifier 参数不存在,无法跳转页面');
  20. }
  21. // is_jump等于0不跳转
  22. if (!EmptyUtil.isEmpty(skipModel.isJump) && skipModel.isJump == '0') {
  23. return;
  24. }
  25. print('skipIdentifier: ${skipModel.skipIdentifier}');
  26. hideKeyboard(context);
  27. if (skipModel?.requiredLogin == '1') {
  28. UserInfoModel user = await Provider.of<UserInfoNotifier>(context, listen: false).getUserInfoModel();
  29. print(user.toString());
  30. if (user?.token == null || user.token == '') {
  31. print('need login...');
  32. return RouterUtil.goLogin(context);
  33. }
  34. }
  35. if (skipModel?.requiredTaobaoAuth == '1') {
  36. UserInfoModel user =
  37. await Provider.of<UserInfoNotifier>(context, listen: false).getUserInfoModel();
  38. if (!user?.isTBAuth ?? false) {
  39. TaobaoAuth.auth(context);
  40. return;
  41. }
  42. }
  43. // webView
  44. if (skipModel?.skipIdentifier == 'pub.flutter.url') {
  45. this.openWebview(skipModel?.url, context);
  46. return;
  47. }
  48. Widget page = EmptyPage();
  49. if (PageFactory.hasRegisted(skipModel.skipIdentifier)) {
  50. page = PageFactory.create(skipModel.skipIdentifier, data);
  51. } else {
  52. if (Application.hasStringMethod(skipModel.skipIdentifier.toString())) {
  53. Application.doStringMethod(skipModel.skipIdentifier.toString());
  54. return null;
  55. }
  56. var result = await NetUtil.post('/api/v1/mod/${skipModel.skipIdentifier.toString()}', method: NetMethod.GET);
  57. Logger.debug(result);
  58. page = PageFactory.create('index', Map<String, dynamic>.from(result));
  59. }
  60. return Navigator.of(context).push(CupertinoPageRoute(builder: (BuildContext context) {
  61. return page;
  62. }));
  63. }
  64. // 跳转登录
  65. static Future goLogin(BuildContext context) async {
  66. String isIosReview = await SharedPreferencesUtil.getStringValue(GlobalConfig.IS_IOS_REVIEW, defaultVal: '0');
  67. return Navigator.of(context).push(CupertinoPageRoute(builder: (BuildContext context) {
  68. // 苹果审核登录样式
  69. if (Platform.isIOS && isIosReview == '1') {
  70. return LoginAccountPage(null);
  71. }
  72. return LoginPage();
  73. }));
  74. }
  75. // 打开网页
  76. static Future openWebview(String url, BuildContext context) {
  77. if (url == null || url == '') {
  78. Logger.error('跳转链接不能为空');
  79. return Future.error('跳转链接不能为空');
  80. }
  81. SkipModel model = SkipModel();
  82. model.skipIdentifier = 'pub.flutter.url';
  83. model.url = url;
  84. RouterUtil.route(model, model.toJson(), context);
  85. }
  86. // 回到首页
  87. static Future goBackHomePage(BuildContext context, {bool needUpdateAuth = true}) async {
  88. if (needUpdateAuth) {
  89. await TaobaoAuth.initAuth(context);
  90. EventUtil.instance.fire(LoginSuccessEvent());
  91. }
  92. Navigator.popUntil(context, ModalRoute.withName('/homePage'));
  93. }
  94. // 隐藏键盘
  95. static void hideKeyboard(BuildContext context) {
  96. try {
  97. FocusScopeNode currentFocus = FocusScope.of(context);
  98. if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
  99. FocusManager.instance.primaryFocus.unfocus();
  100. }
  101. // SystemChannels.textInput.invokeMethod('TextInput.hide');
  102. // FocusScope.of(context).requestFocus(FocusNode());
  103. } catch (e, s) {
  104. Logger.error(e, s);
  105. }
  106. }
  107. }