基础库
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

router_util.dart 4.3 KiB

vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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') {
  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. Application.doStringParamsMethod("openUrl", data: {"url": model.url});
  84. return null;
  85. //
  86. // RouterUtil.route(model, model.toJson(), context);
  87. }
  88. // 回到首页
  89. static Future goBackHomePage(BuildContext context, {bool needUpdateAuth = true}) async {
  90. if (needUpdateAuth) {
  91. await TaobaoAuth.initAuth(context);
  92. EventUtil.instance.fire(LoginSuccessEvent());
  93. }
  94. Navigator.popUntil(context, ModalRoute.withName('/homePage'));
  95. }
  96. // 隐藏键盘
  97. static void hideKeyboard(BuildContext context) {
  98. try {
  99. FocusScopeNode currentFocus = FocusScope.of(context);
  100. if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
  101. FocusManager.instance.primaryFocus.unfocus();
  102. }
  103. // SystemChannels.textInput.invokeMethod('TextInput.hide');
  104. // FocusScope.of(context).requestFocus(FocusNode());
  105. } catch (e, s) {
  106. Logger.error(e, s);
  107. }
  108. }
  109. }