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

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