基础库
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

135 行
4.8 KiB

  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. }