基础库
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.
 
 
 
 
 

102 line
3.5 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,
  17. BuildContext context) async {
  18. if (skipModel.skipIdentifier == null || skipModel.skipIdentifier == '') {
  19. print('skipIdentifier 参数不存在,无法跳转页面');
  20. return Future.error('skipIdentifier 参数不存在,无法跳转页面');
  21. }
  22. print('skipIdentifier: ${skipModel.skipIdentifier}');
  23. hideKeyboard(context);
  24. if (skipModel?.requiredLogin == '1') {
  25. UserInfoModel user =
  26. await Provider.of<UserInfoNotifier>(context, listen: false)
  27. .getUserInfoModel();
  28. print(user.toString());
  29. if (user?.token == null || user.token == '') {
  30. print('need login...');
  31. return RouterUtil.goLogin(context);
  32. }
  33. }
  34. Widget page = EmptyPage();
  35. if (PageFactory.hasRegisted(skipModel.skipIdentifier)) {
  36. page = PageFactory.create(skipModel.skipIdentifier, data);
  37. } else {
  38. var result = await NetUtil.post(
  39. '/api/v1/mod/${skipModel.skipIdentifier.toString()}',
  40. method: NetMethod.GET);
  41. Logger.debug(result);
  42. page = PageFactory.create('index', Map<String, dynamic>.from(result));
  43. }
  44. return Navigator.of(context)
  45. .push(CupertinoPageRoute(builder: (BuildContext context) {
  46. return page;
  47. }));
  48. }
  49. // 跳转登录
  50. static Future goLogin(BuildContext context) async{
  51. String isIosReview = await SharedPreferencesUtil.getStringValue(GlobalConfig.IS_IOS_REVIEW, defaultVal: '0');
  52. return Navigator.of(context)
  53. .push(CupertinoPageRoute(builder: (BuildContext context) {
  54. // 苹果审核登录样式
  55. if(Platform.isIOS && isIosReview == '1'){
  56. return LoginAccountPage(null);
  57. }
  58. return LoginPage();
  59. }));
  60. }
  61. // 打开网页
  62. static Future openWebview(String url, BuildContext context) {
  63. if (url == null || url == '') {
  64. Logger.error('跳转链接不能为空');
  65. return Future.error('跳转链接不能为空');
  66. }
  67. SkipModel model = SkipModel();
  68. model.skipIdentifier = 'pub.flutter.url';
  69. model.url = url;
  70. RouterUtil.route(model, model.toJson(), context);
  71. }
  72. // 回到首页
  73. static Future goBackHomePage(BuildContext context, {bool needUpdateAuth = true}) async{
  74. if(needUpdateAuth) {
  75. await TaobaoAuth.initAuth(context);
  76. EventUtil.instance.fire(LoginSuccessEvent());
  77. }
  78. Navigator.popUntil(context, ModalRoute.withName('/homePage'));
  79. }
  80. // 隐藏键盘
  81. static void hideKeyboard(BuildContext context){
  82. try {
  83. FocusScopeNode currentFocus = FocusScope.of(context);
  84. if (!currentFocus.hasPrimaryFocus &&
  85. currentFocus.focusedChild != null) {
  86. FocusManager.instance.primaryFocus.unfocus();
  87. }
  88. // SystemChannels.textInput.invokeMethod('TextInput.hide');
  89. // FocusScope.of(context).requestFocus(FocusNode());
  90. }catch(e, s){
  91. Logger.error(e,s);
  92. }
  93. }
  94. }