基础库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

121 lines
4.3 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 = await Provider.of<UserInfoNotifier>(context, listen: false).getUserInfoModel();
  37. if (!user?.isTBAuth ?? false) {
  38. TaobaoAuth.auth(context);
  39. return;
  40. }
  41. }
  42. // TODO webView 临时代码,IOS添加原生WebView需要改动这里
  43. if (skipModel?.skipIdentifier == 'pub.flutter.url' && Platform.isAndroid) {
  44. RouterUtil.openWebview(skipModel?.url, context);
  45. return;
  46. }
  47. Widget page = EmptyPage();
  48. if (PageFactory.hasRegisted(skipModel.skipIdentifier)) {
  49. page = PageFactory.create(skipModel.skipIdentifier, data);
  50. } else {
  51. if (Application.hasStringMethod(skipModel.skipIdentifier.toString())) {
  52. Application.doStringMethod(skipModel.skipIdentifier.toString());
  53. return null;
  54. }
  55. var result = await NetUtil.post('/api/v1/mod/${skipModel.skipIdentifier.toString()}', method: NetMethod.GET);
  56. Logger.debug(result);
  57. page = PageFactory.create('index', Map<String, dynamic>.from(result));
  58. }
  59. return Navigator.of(context).push(CupertinoPageRoute(builder: (BuildContext context) {
  60. return page;
  61. }));
  62. }
  63. // 跳转登录
  64. static Future goLogin(BuildContext context) async {
  65. String isIosReview = await SharedPreferencesUtil.getStringValue(GlobalConfig.IS_IOS_REVIEW, defaultVal: '0');
  66. return Navigator.of(context).push(CupertinoPageRoute(builder: (BuildContext context) {
  67. // 苹果审核登录样式
  68. if (Platform.isIOS && isIosReview == '1') {
  69. return LoginAccountPage(null);
  70. }
  71. return LoginPage();
  72. }));
  73. }
  74. // 打开网页
  75. static Future openWebview(String url, BuildContext context) {
  76. if (url == null || url == '') {
  77. Logger.error('跳转链接不能为空');
  78. return Future.error('跳转链接不能为空');
  79. }
  80. SkipModel model = SkipModel();
  81. model.skipIdentifier = 'pub.flutter.url';
  82. model.url = url;
  83. if (Platform.isAndroid) {
  84. Application.doStringParamsMethod("openUrl", data: {"url": model.url});
  85. return null;
  86. }
  87. RouterUtil.route(model, model.toJson(), context);
  88. }
  89. // 回到首页
  90. static Future goBackHomePage(BuildContext context, {bool needUpdateAuth = true}) async {
  91. if (needUpdateAuth) {
  92. await TaobaoAuth.initAuth(context);
  93. EventUtil.instance.fire(LoginSuccessEvent());
  94. }
  95. Navigator.popUntil(context, ModalRoute.withName('/homePage'));
  96. }
  97. // 隐藏键盘
  98. static void hideKeyboard(BuildContext context) {
  99. try {
  100. FocusScopeNode currentFocus = FocusScope.of(context);
  101. if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
  102. FocusManager.instance.primaryFocus.unfocus();
  103. }
  104. // SystemChannels.textInput.invokeMethod('TextInput.hide');
  105. // FocusScope.of(context).requestFocus(FocusNode());
  106. } catch (e, s) {
  107. Logger.error(e, s);
  108. }
  109. }
  110. }