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

turn_chain_util.dart 6.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import 'dart:io';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter_alibc/alibc_model.dart';
  4. import 'package:flutter_alibc/flutter_alibc.dart';
  5. import 'package:jdsdk/jdsdk.dart';
  6. import 'package:url_launcher/url_launcher.dart';
  7. import 'package:zhiying_comm/models/user/user_info_model.dart';
  8. import 'package:zhiying_comm/util/dialog/loading/loading.dart';
  9. import 'package:zhiying_comm/util/empty_util.dart';
  10. import 'package:zhiying_comm/util/global_config.dart';
  11. import 'package:zhiying_comm/util/log/let_log.dart';
  12. import 'package:zhiying_comm/util/net_util.dart';
  13. import 'package:zhiying_comm/util/taobao/taobao_auth.dart';
  14. import 'package:fluttertoast/fluttertoast.dart';
  15. import '../router_util.dart';
  16. class TurnChainUtil {
  17. ///
  18. /// 跳转app或者打开url进行领券
  19. /// userInfoModel: 用户登陆的带有token 与 淘宝是否授权的 model类
  20. /// provider: 商品的渠道
  21. /// data: 转链需要的请求参数
  22. ///
  23. /// 一些常用的APP的 URL scheme:
  24. /// 来源:https://blog.csdn.net/jdazy/article/details/79208754
  25. /// QQ: mqq://
  26. /// 微信: weixin://
  27. /// 京东: openapp.jdmobile://
  28. /// 淘宝: taobao://
  29. /// 美团: imeituan://
  30. /// 点评: dianping://
  31. /// 1号店: wccbyihaodian://
  32. /// 支付宝: alipay://
  33. /// 微博: sinaweibo://
  34. /// 腾讯微博: TencentWeibo://
  35. /// weico微博: weico://
  36. /// 知乎: zhihu://
  37. /// 豆瓣fm: doubanradio://
  38. /// 网易公开课: ntesopen://
  39. /// Chrome: googlechrome://
  40. /// QQ浏览器: mqqbrowser://
  41. /// uc浏览器: ucbrowser://
  42. /// 搜狗浏览器: SogouMSE://
  43. /// 百度地图: baidumap:// bdmap://
  44. /// 优酷: youku://
  45. /// 人人: renren://
  46. /// 我查查: wcc://
  47. /// 有道词典: yddictproapp://
  48. /// 微盘: sinavdisk://
  49. /// 名片全能王: camcard://
  50. ///
  51. static Future<void> openReceiveCoupon(BuildContext context, UserInfoModel userInfoModel, String provider, Map<String, dynamic> data) async {
  52. /// 1、先判断是否登陆
  53. if (EmptyUtil.isEmpty(userInfoModel) || EmptyUtil.isEmpty(userInfoModel?.token)) {
  54. RouterUtil.goLogin(context);
  55. return;
  56. }
  57. /// 2、如果是淘宝,判断是否授权
  58. if (provider == GlobalConfig.PROVIDER_TB && !userInfoModel.isTBAuth) {
  59. TaobaoAuth.auth(context);
  60. return;
  61. }
  62. /// 3、获取转链,进行跳转
  63. Map<String, dynamic> result = await getTurnChainResult(context, provider, data, isShare: false);
  64. if (!EmptyUtil.isEmpty(result)) {
  65. String openAppUrl = result['open_app_url'];
  66. String appUrl = result['app_url'];
  67. String webUrl = result['no_open_app_url'];
  68. /// 4、根据渠道进行不同的跳转
  69. switch (provider) {
  70. case GlobalConfig.PROVIDER_TB:
  71. case GlobalConfig.PROVIDER_TM:
  72. if (!EmptyUtil.isEmpty(openAppUrl)) {
  73. TradeResult tradeResult;
  74. if (Platform.isAndroid) {
  75. tradeResult = await FlutterAlibc.openByUrl(url: openAppUrl, backUrl: "alisdk://");
  76. } else if (Platform.isIOS) {
  77. tradeResult = await FlutterAlibc.openByUrl(url: openAppUrl);
  78. }
  79. Logger.debug('taobao result = ${tradeResult.errorCode} ${tradeResult.errorMessage} ');
  80. } else if (!EmptyUtil.isEmpty(webUrl)) {
  81. RouterUtil.openWebview(webUrl, context);
  82. } else {
  83. Fluttertoast.showToast(msg: '购买链接不存在');
  84. }
  85. break;
  86. case GlobalConfig.PROVIDER_JD:
  87. String tempURLScheme1 = 'openapp.jdmobile://virtual?params=%';
  88. String tempURLScheme2 = 'openapp.jdmobile://';
  89. if (!EmptyUtil.isEmpty(openAppUrl) && await canLaunch(tempURLScheme1) || await canLaunch(tempURLScheme2)) {
  90. Jdsdk.openUrl(url: openAppUrl);
  91. } else if (!EmptyUtil.isEmpty(webUrl)) {
  92. RouterUtil.openWebview(webUrl, context);
  93. } else {
  94. Fluttertoast.showToast(msg: '购买链接不存在');
  95. }
  96. break;
  97. case GlobalConfig.PROVIDER_KL:
  98. case GlobalConfig.PROVIDER_PDD:
  99. case GlobalConfig.PROVIDER_SN:
  100. case GlobalConfig.PROVIDER_VIP:
  101. print(await canLaunch(appUrl));
  102. bool launchable = await launch(appUrl);
  103. if (launchable) {
  104. // Logger.log('打开${provider} app, url = ${appUrl}');
  105. } else if (!EmptyUtil.isEmpty(webUrl)) {
  106. Logger.log('打开${provider} webUrl, url = ${webUrl}');
  107. RouterUtil.openWebview(webUrl, context);
  108. } else {
  109. Fluttertoast.showToast(msg: '购买链接不存在');
  110. }
  111. break;
  112. }
  113. } else {
  114. Fluttertoast.showToast(msg: '购买链接不存在');
  115. }
  116. }
  117. ///
  118. /// 获取分享的转链
  119. /// userInfoModel: 用户登陆的带有token 与 淘宝是否授权的 model类
  120. /// provider: 商品的渠道
  121. /// data: 转链需要的请求参数
  122. ///
  123. /// 返回参数: 只需要获取返回结果的 open_app_url 值即可。
  124. /// 例如: Map<String, dynamic> result = await getShareTurnChain(context, _user, provider, data);
  125. /// String buyUrl = result['open_app_url']
  126. ///
  127. static Future<Map<String, dynamic>> getShareTurnChain(BuildContext context, UserInfoModel userInfoModel, String provider, Map<String, dynamic> data) async {
  128. /// 1、先判断是否登陆
  129. if (EmptyUtil.isEmpty(userInfoModel) || EmptyUtil.isEmpty(userInfoModel?.token)) {
  130. RouterUtil.goLogin(context);
  131. return null;
  132. }
  133. /// 2、如果是淘宝,判断是否授权
  134. if (provider == GlobalConfig.PROVIDER_TB && !userInfoModel.isTBAuth) {
  135. TaobaoAuth.auth(context);
  136. return null;
  137. }
  138. /// 3、获取转链的结果
  139. Map<String, dynamic> result = await getTurnChainResult(context, provider, data, isShare: true);
  140. if (!EmptyUtil.isEmpty(result) && !EmptyUtil.isEmpty(result['open_app_url'])) {
  141. return result;
  142. }
  143. Fluttertoast.showToast(msg: '购买链接不存在');
  144. return null;
  145. }
  146. ///
  147. /// 接口文档:https://www.showdoc.com.cn/1003739271891029?page_id=5760575662067820
  148. /// 根据商品id等信息,获取领券或者分享的转链接
  149. ///
  150. ///
  151. static Future<Map<String, dynamic>> getTurnChainResult(BuildContext context, String provider, Map<String, dynamic> data, {bool isShare = false}) async {
  152. try {
  153. if (!EmptyUtil.isEmpty(context) && !EmptyUtil.isEmpty(provider) && !EmptyUtil.isEmpty(data) && !EmptyUtil.isEmpty('gid')) {
  154. // 设置是否分享还是转链
  155. data['is_share'] = isShare ? '1' : '0';
  156. // 开启loading
  157. Loading.show(context);
  158. var result = await NetUtil.post('/api/v1/convert/$provider', params: data, method: NetMethod.POST);
  159. // 关闭loading
  160. Loading.dismiss();
  161. if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
  162. return result[GlobalConfig.HTTP_RESPONSE_KEY_DATA];
  163. }
  164. }
  165. } catch (e, s) {
  166. Logger.error(e, s);
  167. }
  168. return null;
  169. }
  170. }