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

192 lines
7.1 KiB

  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:fluttertoast/fluttertoast.dart';
  6. import 'package:jdsdk/jdsdk.dart';
  7. import 'package:url_launcher/url_launcher.dart';
  8. import 'package:zhiying_comm/models/user/user_info_model.dart';
  9. import 'package:zhiying_comm/util/dialog/loading/loading.dart';
  10. import 'package:zhiying_comm/util/empty_util.dart';
  11. import 'package:zhiying_comm/util/global_config.dart';
  12. import 'package:zhiying_comm/util/log/let_log.dart';
  13. import 'package:zhiying_comm/util/net_util.dart';
  14. import 'package:zhiying_comm/util/taobao/taobao_auth.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.cancel();
  84. Fluttertoast.showToast(msg: '购买链接不存在');
  85. }
  86. break;
  87. case GlobalConfig.PROVIDER_JD:
  88. String tempURLScheme1 = 'openapp.jdmobile://virtual?params=%';
  89. String tempURLScheme2 = 'openapp.jdmobile://';
  90. if (!EmptyUtil.isEmpty(openAppUrl) && await canLaunch(tempURLScheme1) || await canLaunch(tempURLScheme2)) {
  91. Jdsdk.openUrl(url: openAppUrl);
  92. } else if (!EmptyUtil.isEmpty(webUrl)) {
  93. RouterUtil.openWebview(webUrl, context);
  94. } else {
  95. Fluttertoast.cancel();
  96. Fluttertoast.showToast(msg: '购买链接不存在');
  97. }
  98. break;
  99. case GlobalConfig.PROVIDER_KL:
  100. case GlobalConfig.PROVIDER_PDD:
  101. case GlobalConfig.PROVIDER_SN:
  102. case GlobalConfig.PROVIDER_VIP:
  103. bool launchable = await canLaunch(appUrl);
  104. if (Platform.isIOS) {
  105. launchable = await launch(appUrl);
  106. }
  107. if (launchable) {
  108. if (!Platform.isIOS) {
  109. await launch(appUrl);
  110. }
  111. } else if (!EmptyUtil.isEmpty(webUrl)) {
  112. Logger.log('打开${provider} webUrl, url = ${webUrl}');
  113. RouterUtil.openWebview(webUrl, context);
  114. } else {
  115. Fluttertoast.cancel();
  116. Fluttertoast.showToast(msg: '购买链接不存在');
  117. }
  118. break;
  119. }
  120. } else {
  121. Fluttertoast.cancel();
  122. Fluttertoast.showToast(msg: '购买链接不存在');
  123. }
  124. }
  125. ///
  126. /// 获取分享的转链
  127. /// userInfoModel: 用户登陆的带有token 与 淘宝是否授权的 model类
  128. /// provider: 商品的渠道
  129. /// data: 转链需要的请求参数
  130. ///
  131. /// 返回参数: 只需要获取返回结果的 open_app_url 值即可。
  132. /// 例如: Map<String, dynamic> result = await getShareTurnChain(context, _user, provider, data);
  133. /// String buyUrl = result['open_app_url']
  134. ///
  135. static Future<Map<String, dynamic>> getShareTurnChain(BuildContext context, UserInfoModel userInfoModel, String provider, Map<String, dynamic> data) async {
  136. /// 1、先判断是否登陆
  137. if (EmptyUtil.isEmpty(userInfoModel) || EmptyUtil.isEmpty(userInfoModel?.token)) {
  138. RouterUtil.goLogin(context);
  139. return null;
  140. }
  141. /// 2、如果是淘宝,判断是否授权
  142. if (provider == GlobalConfig.PROVIDER_TB && !userInfoModel.isTBAuth) {
  143. TaobaoAuth.auth(context);
  144. return null;
  145. }
  146. /// 3、获取转链的结果
  147. Map<String, dynamic> result = await getTurnChainResult(context, provider, data, isShare: true);
  148. if (!EmptyUtil.isEmpty(result) && !EmptyUtil.isEmpty(result['open_app_url'])) {
  149. return result;
  150. }
  151. Fluttertoast.cancel();
  152. Fluttertoast.showToast(msg: '购买链接不存在');
  153. return null;
  154. }
  155. ///
  156. /// 接口文档:https://www.showdoc.com.cn/1003739271891029?page_id=5760575662067820
  157. /// 根据商品id等信息,获取领券或者分享的转链接
  158. ///
  159. ///
  160. static Future<Map<String, dynamic>> getTurnChainResult(BuildContext context, String provider, Map<String, dynamic> data, {bool isShare = false}) async {
  161. try {
  162. if (!EmptyUtil.isEmpty(context) && !EmptyUtil.isEmpty(provider) && !EmptyUtil.isEmpty(data) && !EmptyUtil.isEmpty('gid')) {
  163. // 设置是否分享还是转链
  164. data['is_share'] = isShare ? '1' : '0';
  165. // 开启loading
  166. Loading.show(context);
  167. var result = await NetUtil.post('/api/v1/convert/$provider', params: data, method: NetMethod.POST);
  168. // 关闭loading
  169. Loading.dismiss();
  170. if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
  171. return result[GlobalConfig.HTTP_RESPONSE_KEY_DATA];
  172. }
  173. }
  174. } catch (e, s) {
  175. Logger.error(e, s);
  176. }
  177. return null;
  178. }
  179. }