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

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