|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- import 'dart:io';
-
- import 'package:flutter/cupertino.dart';
- import 'package:flutter_alibc/alibc_const_key.dart';
- import 'package:flutter_alibc/alibc_model.dart';
- import 'package:flutter_alibc/flutter_alibc.dart';
- import 'package:fluttertoast/fluttertoast.dart';
- import 'package:jdsdk/jdsdk.dart';
- import 'package:url_launcher/url_launcher.dart';
- import 'package:zhiying_comm/models/user/user_info_model.dart';
- import 'package:zhiying_comm/util/dialog/loading/loading.dart';
- import 'package:zhiying_comm/util/empty_util.dart';
- import 'package:zhiying_comm/util/global_config.dart';
- import 'package:zhiying_comm/util/log/let_log.dart';
- import 'package:zhiying_comm/util/net_util.dart';
- import 'package:zhiying_comm/util/pdd_auth/pdd_auth.dart';
- import 'package:zhiying_comm/util/taobao/taobao_auth.dart';
- import 'package:zhiying_comm/util/turn_chain/turn_chain_dialog_repository.dart';
- import 'package:zhiying_comm/util/turn_chain/turn_chain_style_model.dart';
- import 'package:zhiying_comm/zhiying_comm.dart';
-
- import '../router_util.dart';
- import 'turn_chain_dialog.dart';
- import 'package:zhiying_comm/util/shared_prefe_util.dart';
-
- class TurnChainUtil {
- ///
- /// 跳转app或者打开url进行领券
- /// userInfoModel: 用户登陆的带有token 与 淘宝是否授权的 model类
- /// provider: 商品的渠道
- /// data: 转链需要的请求参数
- ///
- /// 一些常用的APP的 URL scheme:
- /// 来源:https://blog.csdn.net/jdazy/article/details/79208754
- /// QQ: mqq://
- /// 微信: weixin://
- /// 京东: openapp.jdmobile://
- /// 淘宝: taobao://
- /// 美团: imeituan://
- /// 点评: dianping://
- /// 1号店: wccbyihaodian://
- /// 支付宝: alipay://
- /// 微博: sinaweibo://
- /// 腾讯微博: TencentWeibo://
- /// weico微博: weico://
- /// 知乎: zhihu://
- /// 豆瓣fm: doubanradio://
- /// 网易公开课: ntesopen://
- /// Chrome: googlechrome://
- /// QQ浏览器: mqqbrowser://
- /// uc浏览器: ucbrowser://
- /// 搜狗浏览器: SogouMSE://
- /// 百度地图: baidumap:// bdmap://
- /// 优酷: youku://
- /// 人人: renren://
- /// 我查查: wcc://
- /// 有道词典: yddictproapp://
- /// 微盘: sinavdisk://
- /// 名片全能王: camcard://
- ///
- static Future<void> openReceiveCoupon(BuildContext context, UserInfoModel userInfoModel, String goodsId, String provider, Map<String, dynamic> data,
- {bool isFree = false}) async {
- /// iOS 审核状态
- // String is_ios_review = await SharedPreferencesUtil.getStringValue(GlobalConfig.IS_IOS_REVIEW, defaultVal: '0');
-
- /// 1、先判断是否登陆
- if (EmptyUtil.isEmpty(userInfoModel) || EmptyUtil.isEmpty(userInfoModel?.token)) {
- RouterUtil.goLogin(context);
- return;
- }
-
- /// 2、如果是淘宝或者天猫,判断是否授权
- ///
- /// ⚠️ 因业务修改,淘宝或者天猫每次都必须请求网络请求状态
- // if ((provider == GlobalConfig.PROVIDER_TB || provider == GlobalConfig.PROVIDER_TM ) && !userInfoModel.isTBAuth) {
- // TaobaoAuth.auth(context);
- // return;
- // }
- if (provider == GlobalConfig.PROVIDER_TB || provider == GlobalConfig.PROVIDER_TM) {
- bool isAuth = await TaobaoAuth.auth(context);
- if (!isAuth) return;
- }
-
- /// 3、获取转链,进行跳转
- Map<String, dynamic> result = await getTurnChainResult(context, goodsId, provider, data, isShare: false, isFree: isFree);
- if (!EmptyUtil.isEmpty(result)) {
- String openAppUrl = result['open_app_url'];
- String appUrl = result['app_url'];
- String webUrl = result['no_open_app_url'];
-
- bool jumpResult = await jumpNative(context, provider: provider, openAppUrl: openAppUrl, appUrl: appUrl, webUrl: webUrl);
- if (!jumpResult) {
- Fluttertoast.cancel();
- Fluttertoast.showToast(msg: '购买链接不存在');
- }
-
- // /// 4、根据渠道进行不同的跳转
- // switch (provider) {
- // case GlobalConfig.PROVIDER_TB:
- // case GlobalConfig.PROVIDER_TM:
- // if (!EmptyUtil.isEmpty(openAppUrl)) {
- // TradeResult tradeResult;
- // if (Platform.isAndroid) {
- // // print("跳转链接"+openAppUrl);
- // tradeResult = await FlutterAlibc.openByUrl(url: openAppUrl, backUrl: "alisdk://");
- // } else if (Platform.isIOS) {
- // if (is_ios_review == '1') {
- // print('iOS审核:' + is_ios_review);
- // RouterUtil.openWebview(webUrl, context);
- // } else {
- // tradeResult = await FlutterAlibc.openByUrl(url: openAppUrl);
- // }
- // }
- // Logger.debug('taobao result = ${tradeResult.errorCode} ${tradeResult.errorMessage} ');
- // } else if (!EmptyUtil.isEmpty(webUrl)) {
- // RouterUtil.openWebview(webUrl, context);
- // } else {
- // Fluttertoast.cancel();
- // Fluttertoast.showToast(msg: '购买链接不存在');
- // }
- //
- // break;
- // case GlobalConfig.PROVIDER_JD:
- // String tempURLScheme1 = 'openapp.jdmobile://virtual?params=%';
- // String tempURLScheme2 = 'openapp.jdmobile://';
- // if (!EmptyUtil.isEmpty(openAppUrl) && await canLaunch(tempURLScheme1) || await canLaunch(tempURLScheme2)) {
- // Jdsdk.openUrl(url: openAppUrl);
- // } else if (!EmptyUtil.isEmpty(webUrl)) {
- // RouterUtil.openWebview(webUrl, context);
- // } else {
- // Fluttertoast.cancel();
- // Fluttertoast.showToast(msg: '购买链接不存在');
- // }
- // break;
- // case GlobalConfig.PROVIDER_KL:
- // case GlobalConfig.PROVIDER_PDD:
- // case GlobalConfig.PROVIDER_SN:
- // bool launchable = await canLaunch(appUrl);
- // if (Platform.isIOS) {
- // launchable = await launch(appUrl);
- // }
- // if (launchable) {
- // if (!Platform.isIOS) {
- // if (appUrl.startsWith("suning")) {
- // RouterUtil.openWebview(webUrl, context);
- // } else {
- // RouterUtil.openWebview(webUrl, context);
- // }
- // }
- // } else if (!EmptyUtil.isEmpty(webUrl)) {
- // Logger.log('打开${provider} webUrl, url = ${webUrl}');
- // RouterUtil.openWebview(webUrl, context);
- // } else {
- // Fluttertoast.cancel();
- // Fluttertoast.showToast(msg: '购买链接不存在');
- // }
- // break;
- // case GlobalConfig.PROVIDER_VIP:
- // bool launchable = await canLaunch(appUrl);
- // if (Platform.isIOS) {
- // launchable = await launch(appUrl);
- // }
- // if (launchable) {
- // if (!Platform.isIOS) {
- // await launch(appUrl);
- // }
- // } else if (!EmptyUtil.isEmpty(webUrl)) {
- // Logger.log('打开${provider} webUrl, url = ${webUrl}');
- // RouterUtil.openWebview(webUrl, context);
- // } else {
- // Fluttertoast.cancel();
- // Fluttertoast.showToast(msg: '购买链接不存在');
- // }
- // break;
- // default:
- // if (!EmptyUtil.isEmpty(openAppUrl)) {
- // bool launchable = await canLaunch(appUrl);
- // if (launchable) {
- // launchable = await launch(appUrl);
- // }
- // if (launchable) {
- // if (!Platform.isIOS) {
- // RouterUtil.openWebview(webUrl, context);
- // }
- // } else if (!EmptyUtil.isEmpty(webUrl)) {
- // Logger.log('打开${provider} webUrl, url = ${webUrl}');
- // RouterUtil.openWebview(webUrl, context);
- // } else {
- // Fluttertoast.cancel();
- // Fluttertoast.showToast(msg: '购买链接不存在');
- // }
- // } else {
- // Fluttertoast.cancel();
- // Fluttertoast.showToast(msg: '购买链接不存在');
- // }
- // break;
- // }
- } else {
- Fluttertoast.cancel();
- Fluttertoast.showToast(msg: '购买链接不存在');
- }
- }
-
- ///
- /// 跳转原生或者webView打开购买链接
- ///
- /// provider: 商品渠道
- /// openAppUrl: 打开商品链接
- /// appUrl: 打开商品链接,不是Http开头,类似 taobao://, opennapp.jdxxx://
- /// webUrl: 打开商品WebView链接
- ///
- static Future<bool> jumpNative(BuildContext context, {String provider = GlobalConfig.PROVIDER_TB, String openAppUrl, String appUrl, String webUrl}) async {
- bool rlt = false;
-
- ///iOS 审核状态
- String isIosReview = await SharedPreferencesUtil.getStringValue(GlobalConfig.IS_IOS_REVIEW, defaultVal: '0');
- print("渠道" + provider);
-
- /// 4、根据渠道进行不同的跳转
- switch (provider) {
- case GlobalConfig.PROVIDER_TB:
- case GlobalConfig.PROVIDER_TM:
- if (!EmptyUtil.isEmpty(openAppUrl)) {
- TradeResult tradeResult;
- if (Platform.isAndroid) {
- tradeResult = await FlutterAlibc.openByUrl(url: openAppUrl, backUrl: "alisdk://");
- rlt = true;
- } else if (Platform.isIOS) {
- if (isIosReview == '1') {
- print('iOS审核:' + isIosReview);
- RouterUtil.openWebview(webUrl, context);
- rlt = true;
- } else {
- // IOS强制用淘宝打开
- tradeResult = await FlutterAlibc.openByUrl(url: openAppUrl, schemeType: AlibcSchemeType.AlibcSchemeTaoBao);
- rlt = true;
- }
- }
- Logger.debug('taobao result = ${tradeResult.errorCode} ${tradeResult.errorMessage} ');
- } else if (!EmptyUtil.isEmpty(webUrl)) {
- RouterUtil.openWebview(webUrl, context);
- rlt = true;
- } else {
- rlt = false;
- }
- break;
- case GlobalConfig.PROVIDER_JD:
- String tempURLScheme1 = 'openapp.jdmobile://virtual?params=%';
- String tempURLScheme2 = 'openapp.jdmobile://';
- if (!EmptyUtil.isEmpty(openAppUrl) && await canLaunch(tempURLScheme1) || await canLaunch(tempURLScheme2)) {
- Jdsdk.openUrl(url: openAppUrl);
- rlt = true;
- } else if (!EmptyUtil.isEmpty(webUrl)) {
- RouterUtil.openWebview(webUrl, context);
- rlt = true;
- } else {
- rlt = false;
- }
- break;
- case GlobalConfig.PROVIDER_KL:
- case GlobalConfig.PROVIDER_PDD:
- case GlobalConfig.PROVIDER_SN:
- bool launchable = await canLaunch(appUrl);
- if (Platform.isIOS) {
- launchable = await launch(appUrl);
- }
-
- if (launchable) {
- if (!Platform.isIOS) {
- if (provider == GlobalConfig.PROVIDER_SN) {
- RouterUtil.openWebview(webUrl, context);
- } else {
- if (appUrl.startsWith("http")) {
- RouterUtil.openWebview(webUrl, context);
- } else {
- await launch(appUrl);
- }
- }
- rlt = true;
- }
- } else if (!EmptyUtil.isEmpty(webUrl)) {
- Logger.log('打开${provider} webUrl, url = ${webUrl}');
- RouterUtil.openWebview(webUrl, context);
- rlt = true;
- } else {
- rlt = false;
- }
- break;
- case GlobalConfig.PROVIDER_VIP:
- bool launchable = await canLaunch(appUrl);
- if (Platform.isIOS) {
- launchable = await launch(appUrl);
- }
- if (launchable) {
- if (!Platform.isIOS) {
- await launch(appUrl);
- rlt = true;
- }
- } else if (!EmptyUtil.isEmpty(webUrl)) {
- Logger.log('打开${provider} webUrl, url = ${webUrl}');
- RouterUtil.openWebview(webUrl, context);
- rlt = true;
- } else {
- rlt = false;
- }
- break;
- default:
- if (!EmptyUtil.isEmpty(openAppUrl)) {
- bool launchable = await canLaunch(appUrl);
- if (launchable) {
- launchable = await launch(appUrl);
- }
- if (launchable) {
- if (!Platform.isIOS) {
- RouterUtil.openWebview(webUrl, context);
- rlt = true;
- }
- } else if (!EmptyUtil.isEmpty(webUrl)) {
- Logger.log('打开${provider} webUrl, url = ${webUrl}');
- RouterUtil.openWebview(webUrl, context);
- rlt = true;
- } else {
- rlt = false;
- }
- } else {
- rlt = false;
- }
- break;
- }
- return Future.value(rlt);
- }
-
- ///
- /// 获取分享的转链
- /// userInfoModel: 用户登陆的带有token 与 淘宝是否授权的 model类
- /// provider: 商品的渠道
- /// data: 转链需要的请求参数
- ///
- /// 返回参数: 只需要获取返回结果的 open_app_url 值即可。
- /// 例如: Map<String, dynamic> result = await getShareTurnChain(context, _user, provider, data);
- /// String buyUrl = result['open_app_url']
- ///
- static Future<Map<String, dynamic>> getShareTurnChain(BuildContext context, UserInfoModel userInfoModel, String goodsId, String provider, Map<String, dynamic> data) async {
- /// 1、先判断是否登陆
- if (EmptyUtil.isEmpty(userInfoModel) || EmptyUtil.isEmpty(userInfoModel?.token)) {
- RouterUtil.goLogin(context);
- return null;
- }
-
- /// 2、如果是淘宝,判断是否授权
- // if ((provider == GlobalConfig.PROVIDER_TB || provider == GlobalConfig.PROVIDER_TM) && !userInfoModel.isTBAuth) {
- // TaobaoAuth.auth(context);
- // return null;
- // }
- /// ⚠️ 因业务修改,淘宝或者天猫每次都必须请求网络请求状态
- if (provider == GlobalConfig.PROVIDER_TB || provider == GlobalConfig.PROVIDER_TM) {
- bool isAuth = await TaobaoAuth.auth(context);
- if (!isAuth) return null;
- }
-
- /// 2.5 如果是拼多多,判断是否需要授权
- if (provider == GlobalConfig.PROVIDER_PDD) {
- // 如果为true,说明需要进行授权,停止执行。
- bool result = await PddAuth.auth(context);
- if (result) return null;
- }
-
- /// 3、获取转链的结果
- Map<String, dynamic> result = await getTurnChainResult(context, goodsId, provider, data, isShare: true);
- if (!EmptyUtil.isEmpty(result) && !EmptyUtil.isEmpty(result['open_app_url'])) {
- return result;
- }
- Fluttertoast.cancel();
- Fluttertoast.showToast(msg: '购买链接不存在');
- return null;
- }
-
- ///
- /// 获取跳转的dialog样式
- ///
- static Future<dynamic> getCacheTurnChainDialogStyle(String goodsId, String provider, String commission, String coupon) async {
- TurnChainDialogRepository repository = TurnChainDialogRepository();
- var result = await repository.fetchCacheData(goodsId, provider, commission, coupon);
- if (!EmptyUtil.isEmpty(result)) {
- return result;
- }
- return null;
- }
-
- ///
- /// 缓存跳转的dialog样式
- ///
- static Future<bool> cacheTurnChainDialogStyle(String goodsId, String provider, String commission, String coupon) async {
- TurnChainDialogRepository repository = TurnChainDialogRepository();
- bool result = await repository.cacheData(goodsId, provider, commission, coupon);
- return result;
- }
-
- ///
- /// 接口文档:https://www.showdoc.com.cn/1003739271891029?page_id=5760575662067820
- /// 根据商品id等信息,获取领券或者分享的转链接
- ///
- ///
- static Future<Map<String, dynamic>> getTurnChainResult(BuildContext context, String goodsId, String provider, Map<String, dynamic> data,
- {bool isShare = false, bool isFree = false}) async {
- try {
- TurnChainDialogRepository repository = TurnChainDialogRepository();
- if (!EmptyUtil.isEmpty(context) && !EmptyUtil.isEmpty(provider) && !EmptyUtil.isEmpty(data) && !EmptyUtil.isEmpty('gid')) {
- TurnChainStyleModel model = await repository.fetchCacheData(goodsId, provider, data['commission'], data['coupon_price']);
- // 设置是否分享还是转链
- data['is_share'] = isShare ? '1' : '0';
-
- ///1为免单,0为普通
- data['is_free'] = (isFree ?? false) ? "1" : "0";
- (isFree ?? false) ? print("免单") : print("普通");
-
- if (EmptyUtil.isEmpty(model)) {
- // 开启loading
- Loading.show(context);
- } else {
- TurnChainLoading.show(context, model);
- }
- var result = await NetUtil.post('/api/v1/convert/$provider', params: data, method: NetMethod.POST);
- if (EmptyUtil.isEmpty(model)) {
- // 关闭loading
- Loading.dismiss();
- } else {
- TurnChainLoading.dismiss();
- }
- if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
- return result[GlobalConfig.HTTP_RESPONSE_KEY_DATA];
- }
- }
- } catch (e, s) {
- Logger.error(e, s);
- }
- return null;
- }
- }
|