基础库
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 

187 rader
7.0 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.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. bool launchable = await canLaunch(appUrl);
  102. if (Platform.isIOS) {
  103. launchable = await launch(appUrl);
  104. }
  105. if (launchable) {
  106. if (!Platform.isIOS) {
  107. await launch(appUrl);
  108. }
  109. } else if (!EmptyUtil.isEmpty(webUrl)) {
  110. Logger.log('打开${provider} webUrl, url = ${webUrl}');
  111. RouterUtil.openWebview(webUrl, context);
  112. } else {
  113. Fluttertoast.showToast(msg: '购买链接不存在');
  114. }
  115. break;
  116. }
  117. } else {
  118. Fluttertoast.showToast(msg: '购买链接不存在');
  119. }
  120. }
  121. ///
  122. /// 获取分享的转链
  123. /// userInfoModel: 用户登陆的带有token 与 淘宝是否授权的 model类
  124. /// provider: 商品的渠道
  125. /// data: 转链需要的请求参数
  126. ///
  127. /// 返回参数: 只需要获取返回结果的 open_app_url 值即可。
  128. /// 例如: Map<String, dynamic> result = await getShareTurnChain(context, _user, provider, data);
  129. /// String buyUrl = result['open_app_url']
  130. ///
  131. static Future<Map<String, dynamic>> getShareTurnChain(BuildContext context, UserInfoModel userInfoModel, String provider, Map<String, dynamic> data) async {
  132. /// 1、先判断是否登陆
  133. if (EmptyUtil.isEmpty(userInfoModel) || EmptyUtil.isEmpty(userInfoModel?.token)) {
  134. RouterUtil.goLogin(context);
  135. return null;
  136. }
  137. /// 2、如果是淘宝,判断是否授权
  138. if (provider == GlobalConfig.PROVIDER_TB && !userInfoModel.isTBAuth) {
  139. TaobaoAuth.auth(context);
  140. return null;
  141. }
  142. /// 3、获取转链的结果
  143. Map<String, dynamic> result = await getTurnChainResult(context, provider, data, isShare: true);
  144. if (!EmptyUtil.isEmpty(result) && !EmptyUtil.isEmpty(result['open_app_url'])) {
  145. return result;
  146. }
  147. Fluttertoast.showToast(msg: '购买链接不存在');
  148. return null;
  149. }
  150. ///
  151. /// 接口文档:https://www.showdoc.com.cn/1003739271891029?page_id=5760575662067820
  152. /// 根据商品id等信息,获取领券或者分享的转链接
  153. ///
  154. ///
  155. static Future<Map<String, dynamic>> getTurnChainResult(BuildContext context, String provider, Map<String, dynamic> data, {bool isShare = false}) async {
  156. try {
  157. if (!EmptyUtil.isEmpty(context) && !EmptyUtil.isEmpty(provider) && !EmptyUtil.isEmpty(data) && !EmptyUtil.isEmpty('gid')) {
  158. // 设置是否分享还是转链
  159. data['is_share'] = isShare ? '1' : '0';
  160. // 开启loading
  161. Loading.show(context);
  162. var result = await NetUtil.post('/api/v1/convert/$provider', params: data, method: NetMethod.POST);
  163. // 关闭loading
  164. Loading.dismiss();
  165. if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
  166. return result[GlobalConfig.HTTP_RESPONSE_KEY_DATA];
  167. }
  168. }
  169. } catch (e, s) {
  170. Logger.error(e, s);
  171. }
  172. return null;
  173. }
  174. }