|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- import 'dart:io';
-
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter_alibc/alibc_const_key.dart';
- import 'package:flutter_alibc/flutter_alibc.dart';
- import 'package:jdsdk/jdsdk.dart';
- import 'package:provider/provider.dart';
- import 'package:url_launcher/url_launcher.dart';
- import 'package:zhiying_comm/pages/login_page/account/login_account_page.dart';
- import 'package:zhiying_comm/pages/login_page/login_page.dart';
- import 'package:zhiying_comm/util/dialog/loading/loading.dart';
- import 'package:zhiying_comm/util/event_util/event_util.dart';
- import 'package:zhiying_comm/util/event_util/login_success_event.dart';
- import 'package:zhiying_comm/util/pdd_auth/pdd_auth.dart';
- import 'package:zhiying_comm/util/shared_prefe_util.dart';
- import 'package:zhiying_comm/zhiying_comm.dart';
-
- class RouterUtil {
- /*公共跳转
- * skipModel 跳转参数
- * data 额外参数
- * */
- static Future route(SkipModel skipModel, Map<String, dynamic> data, BuildContext context) async {
- if (skipModel.skipIdentifier == null || skipModel.skipIdentifier == '') {
- print('skipIdentifier 参数不存在,无法跳转页面');
- return Future.error('skipIdentifier 参数不存在,无法跳转页面');
- }
- // is_jump等于0不跳转
- if (!EmptyUtil.isEmpty(skipModel.isJump) && skipModel.isJump == '0') {
- return;
- }
- print('skipIdentifier: ${skipModel.skipIdentifier}');
- hideKeyboard(context);
- if (skipModel?.requiredLogin == '1') {
- UserInfoModel user = await Provider.of<UserInfoNotifier>(context, listen: false).getUserInfoModel();
- print(user.toString());
- if (user?.token == null || user.token == '') {
- print('need login...');
- return RouterUtil.goLogin(context);
- }
- }
-
- if (skipModel?.requiredTaobaoAuth == '1') {
- UserInfoModel user = await Provider.of<UserInfoNotifier>(context, listen: false).getUserInfoModel();
- if (!user?.isTBAuth ?? false) {
- TaobaoAuth.auth(context);
- return;
- }
- }
-
-
- if (skipModel?.skipIdentifier == 'pub.flutter.url') {
- if (skipModel?.url?.contains("sourceType=jd") ?? false) {
- Loading.show(context);
- Dio dio = Dio();
- var responds = await dio.get(skipModel.url);
- var detailUrl = responds?.realUri?.toString();
- Logger.debug(detailUrl ?? "");
- Loading.dismiss();
- if (await canLaunch(detailUrl)) {
- Jdsdk.openUrl(url: detailUrl);
- return;
- }
- }
- RouterUtil.openWebview(skipModel?.url, context);
- return;
- }
-
- if (skipModel?.skipIdentifier == 'pub.flutter.taobao_url') {
- print("淘宝链接" + skipModel.url);
- Loading.show(context);
- Dio dio = Dio();
- var responds = await dio.get(skipModel.url);
- var detailUrl = responds?.realUri?.toString();
- Logger.debug(detailUrl ?? "");
- Loading.dismiss();
- FlutterAlibc.openByUrl(url: detailUrl, schemeType: AlibcSchemeType.AlibcSchemeTaoBao);
- return;
- }
-
- Widget page = EmptyPage();
- if (PageFactory.hasRegisted(skipModel.skipIdentifier)) {
- page = PageFactory.create(skipModel.skipIdentifier, data);
- } else {
- if (Application.hasStringMethod(skipModel.skipIdentifier.toString())) {
- Application.doStringMethod(skipModel.skipIdentifier.toString());
- return null;
- }
- var result = await NetUtil.post('/api/v1/mod/${skipModel.skipIdentifier.toString()}', method: NetMethod.GET);
- Logger.debug(result);
- page = PageFactory.create('index', Map<String, dynamic>.from(result));
- }
- return Navigator.of(context).push(CupertinoPageRoute(builder: (BuildContext context) {
- return page;
- }));
- }
-
- // 跳转登录
- static Future goLogin(BuildContext context) async {
- String isIosReview = await SharedPreferencesUtil.getStringValue(GlobalConfig.IS_IOS_REVIEW, defaultVal: '0');
- return Navigator.of(context).push(CupertinoPageRoute(builder: (BuildContext context) {
- // 苹果审核登录样式
- if (Platform.isIOS && isIosReview == '1') {
- return LoginAccountPage(null);
- }
- return LoginPage();
- }));
- }
-
- // 打开网页
- static Future openWebview(String url, BuildContext context) {
- if (url == null || url == '') {
- Logger.error('跳转链接不能为空');
- return Future.error('跳转链接不能为空');
- }
-
- SkipModel model = SkipModel();
- model.skipIdentifier = 'pub.flutter.url';
- model.url = url;
- if (model.url.contains("official/redirect_url")) {
- model.url = model.url + "&zuid=" + (UserInfoNotifier?.staitcUserInfo?.userId ?? "");
- }
-
- Application.doStringParamsMethod("openUrl", data: {"url": model.url});
- return null;
- //
- // RouterUtil.route(model, model.toJson(), context);
- }
-
- // 回到首页
- static Future goBackHomePage(BuildContext context, {bool needUpdateAuth = true}) async {
- if (needUpdateAuth) {
- await TaobaoAuth.initAuth(context);
- EventUtil.instance.fire(LoginSuccessEvent());
- }
- Navigator.popUntil(context, ModalRoute.withName('/homePage'));
- }
-
- // 隐藏键盘
- static void hideKeyboard(BuildContext context) {
- try {
- FocusScopeNode currentFocus = FocusScope.of(context);
- if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
- FocusManager.instance.primaryFocus.unfocus();
- }
- // SystemChannels.textInput.invokeMethod('TextInput.hide');
- // FocusScope.of(context).requestFocus(FocusNode());
- } catch (e, s) {
- Logger.error(e, s);
- }
- }
- }
|