基础库
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

router_util.dart 4.8 KiB

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