智莺生活APP的阿里百川 Flutter 插件
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

279 wiersze
8.6 KiB

  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:flutter_alibc/albc_tools.dart';
  6. import 'package:flutter_alibc/alibc_const_key.dart';
  7. import 'package:flutter_alibc/alibc_model.dart';
  8. class FlutterAlibc {
  9. // static StreamController<String> _responseTaoKeLoginController = new StreamController.broadcast();
  10. // static Stream<String> get responseFromShare =>
  11. // _responseTaoKeLoginController.stream;
  12. // static Future<dynamic> _handler(MethodCall methodCall) {
  13. // if ("taoKeLogin" == methodCall.method) {
  14. // _responseTaoKeLoginController.sink.add(methodCall.arguments);
  15. // }
  16. // return Future.value(true);
  17. // }
  18. // 通信的桥接类
  19. static final MethodChannel _channel = const MethodChannel("flutter_alibc");
  20. // ..setMethodCallHandler(_handler);
  21. static Future<String> get platformVersion async {
  22. final String version = await _channel.invokeMethod('getPlatformVersion');
  23. return version;
  24. }
  25. ///初始化
  26. ///version:当前app版本
  27. ///appName:当前app名称
  28. ///result:{
  29. /// errorCode, //0为初始化成功,其他为失败
  30. /// errorMessage, //message
  31. ///}
  32. static Future<InitModel> initAlibc({String version, String appName}) async {
  33. Map result = await _channel
  34. .invokeMethod("initAlibc", {"version": version, "appName": appName});
  35. return InitModel(
  36. result[AlibcConstKey.errorCode], result[AlibcConstKey.errorMessage]);
  37. }
  38. ///
  39. /// @description: 登录淘宝
  40. ///
  41. /// @return: 成功则返回的data为用户信息,失败则没有data
  42. ///
  43. static Future<LoginModel> loginTaoBao() async {
  44. Map result = await _channel.invokeMethod("loginTaoBao");
  45. // 判断成功还是失败
  46. if (result[AlibcConstKey.errorCode] != "0") {
  47. return LoginModel(
  48. result[AlibcConstKey.errorCode],
  49. result[AlibcConstKey.errorMessage],
  50. );
  51. }
  52. return LoginModel(
  53. result[AlibcConstKey.errorCode], result[AlibcConstKey.errorMessage],
  54. data: UserModel(
  55. result[AlibcConstKey.data]["nick"],
  56. result[AlibcConstKey.data]["avatarUrl"],
  57. result[AlibcConstKey.data]["openId"],
  58. result[AlibcConstKey.data]["openSid"],
  59. result[AlibcConstKey.data]["topAccessToken"],
  60. result[AlibcConstKey.data]["topAuthCode"]));
  61. }
  62. ///
  63. /// @description: 退出淘宝登录
  64. /// @param {type}
  65. /// @return:
  66. ///
  67. static loginOut() {
  68. _channel.invokeMethod("loginOut");
  69. }
  70. ///
  71. /// @description: 渠道授权,获取access_token
  72. /// @param {type}
  73. /// @return:
  74. /// Map<String,String>
  75. static Future<Map<dynamic, dynamic>> taoKeLogin({
  76. @required String url,
  77. AlibcOpenType openType = AlibcOpenType.AlibcOpenTypeAuto,
  78. bool isNeedCustomNativeFailMode = false,
  79. AlibcNativeFailMode nativeFailMode =
  80. AlibcNativeFailMode.AlibcNativeFailModeNone,
  81. AlibcSchemeType schemeType = AlibcSchemeType.AlibcSchemeTaoBao,
  82. TaokeParams taokeParams,
  83. String backUrl,
  84. }) async {
  85. Map taoKe = AlibcTools.getTaokeMap(taokeParams);
  86. Map result = await _channel.invokeMethod("taoKeLogin", {
  87. "url": url,
  88. "openType": openType.index,
  89. "isNeedCustomNativeFailMode": isNeedCustomNativeFailMode,
  90. "nativeFailMode": nativeFailMode.index,
  91. "schemeType": schemeType.index,
  92. "taokeParams": taoKe,
  93. "backUrl": backUrl
  94. });
  95. return result;
  96. }
  97. ///
  98. /// @description: 通过url打开,包括h5,唤起手淘等
  99. /// @param
  100. /// url:目标url
  101. /// openType:打开类型
  102. /// isNeedCustomNativeFailMode:是否需要设置唤端失败策略
  103. /// nativeFailMode:唤端失败策略
  104. /// schemeType:唤起哪个端
  105. /// taokeParams:淘客数据
  106. /// backUrl: 跳转回来的url
  107. /// @return:
  108. ///
  109. static Future<TradeResult> openByUrl({
  110. @required String url,
  111. AlibcOpenType openType = AlibcOpenType.AlibcOpenTypeAuto,
  112. bool isNeedCustomNativeFailMode = false,
  113. AlibcNativeFailMode nativeFailMode =
  114. AlibcNativeFailMode.AlibcNativeFailModeNone,
  115. AlibcSchemeType schemeType = AlibcSchemeType.AlibcSchemeTmall,
  116. TaokeParams taokeParams,
  117. String backUrl,
  118. bool isAuth = false
  119. }) async {
  120. Map taoKe = AlibcTools.getTaokeMap(taokeParams);
  121. var result = await _channel.invokeMethod("openByUrl", {
  122. "url": url,
  123. "openType": openType.index,
  124. "isNeedCustomNativeFailMode": isNeedCustomNativeFailMode,
  125. "nativeFailMode": nativeFailMode.index,
  126. "schemeType": schemeType.index,
  127. "taokeParams": taoKe,
  128. "backUrl": backUrl,
  129. "auth": isAuth
  130. });
  131. if (isAuth && Platform.isAndroid) {
  132. TradeResult tradeResult = TradeResult("-1", "");
  133. if (result != null && result) {
  134. ///授权成功
  135. tradeResult.errorCode = "0";
  136. return tradeResult;
  137. } else {
  138. ///授权失败
  139. tradeResult.errorCode = "-1";
  140. return tradeResult;
  141. }
  142. }
  143. TradeResult tradeResult = AlibcTools.getTradeResult(result);
  144. return tradeResult;
  145. }
  146. ///
  147. /// @description: 打开商品详情
  148. /// @param
  149. /// 同上
  150. /// itemID 商品id,可以是真实的也可以是混淆的
  151. /// isNeedPush iOS独占
  152. /// @return:
  153. ///
  154. static Future<TradeResult> openItemDetail({
  155. @required String itemID,
  156. // iOS独占
  157. // bool isNeedPush = false,
  158. AlibcOpenType openType = AlibcOpenType.AlibcOpenTypeAuto,
  159. bool isNeedCustomNativeFailMode = false,
  160. AlibcNativeFailMode nativeFailMode =
  161. AlibcNativeFailMode.AlibcNativeFailModeNone,
  162. AlibcSchemeType schemeType = AlibcSchemeType.AlibcSchemeTmall,
  163. TaokeParams taokeParams,
  164. // 额外需要追踪的业务数据
  165. Map trackParam,
  166. String backUrl,
  167. }) async {
  168. Map taoKe = AlibcTools.getTaokeMap(taokeParams);
  169. Map result = await _channel.invokeMethod("openItemDetail", {
  170. "itemID": itemID,
  171. // "isNeedPush": isNeedPush,
  172. "openType": openType.index,
  173. "isNeedCustomNativeFailMode": isNeedCustomNativeFailMode,
  174. "nativeFailMode": nativeFailMode.index,
  175. "schemeType": schemeType.index,
  176. "taokeParams": taoKe,
  177. "trackParam": trackParam,
  178. "backUrl": backUrl
  179. });
  180. TradeResult tradeResult = AlibcTools.getTradeResult(result);
  181. return tradeResult;
  182. }
  183. ///
  184. /// @description: 打开店铺
  185. /// @param {type}
  186. /// shopId 店铺id
  187. /// @return:
  188. ///
  189. static Future<TradeResult> openShop({
  190. @required String shopId,
  191. // iOS独占
  192. // bool isNeedPush = false,
  193. AlibcOpenType openType = AlibcOpenType.AlibcOpenTypeAuto,
  194. bool isNeedCustomNativeFailMode = false,
  195. AlibcNativeFailMode nativeFailMode =
  196. AlibcNativeFailMode.AlibcNativeFailModeNone,
  197. AlibcSchemeType schemeType = AlibcSchemeType.AlibcSchemeTmall,
  198. TaokeParams taokeParams,
  199. // 额外需要追踪的业务数据
  200. Map trackParam,
  201. String backUrl,
  202. }) async {
  203. Map taoKe = AlibcTools.getTaokeMap(taokeParams);
  204. Map result = await _channel.invokeMethod("openShop", {
  205. "shopId": shopId,
  206. // "isNeedPush": isNeedPush,
  207. "openType": openType.index,
  208. "isNeedCustomNativeFailMode": isNeedCustomNativeFailMode,
  209. "nativeFailMode": nativeFailMode.index,
  210. "schemeType": schemeType.index,
  211. "taokeParams": taoKe,
  212. "trackParam": trackParam,
  213. "backUrl": backUrl
  214. });
  215. TradeResult tradeResult = AlibcTools.getTradeResult(result);
  216. return tradeResult;
  217. }
  218. ///
  219. /// @description: 打开购物车
  220. /// @param {type}
  221. /// @return:
  222. ///
  223. static Future<TradeResult> openCart({
  224. // iOS独占
  225. // bool isNeedPush = false,
  226. AlibcOpenType openType = AlibcOpenType.AlibcOpenTypeAuto,
  227. bool isNeedCustomNativeFailMode = false,
  228. AlibcNativeFailMode nativeFailMode =
  229. AlibcNativeFailMode.AlibcNativeFailModeNone,
  230. AlibcSchemeType schemeType = AlibcSchemeType.AlibcSchemeTmall,
  231. TaokeParams taokeParams,
  232. // 额外需要追踪的业务数据
  233. Map trackParam,
  234. String backUrl,
  235. }) async {
  236. Map taoKe = AlibcTools.getTaokeMap(taokeParams);
  237. Map result = await _channel.invokeMethod("openCart", {
  238. // "isNeedPush": isNeedPush,
  239. "openType": openType.index,
  240. "isNeedCustomNativeFailMode": isNeedCustomNativeFailMode,
  241. "nativeFailMode": nativeFailMode.index,
  242. "schemeType": schemeType.index,
  243. "taokeParams": taoKe,
  244. "trackParam": trackParam,
  245. "backUrl": backUrl
  246. });
  247. TradeResult tradeResult = AlibcTools.getTradeResult(result);
  248. return tradeResult;
  249. }
  250. // 是否需要设置打点
  251. static syncForTaoke(bool isSync) {
  252. _channel.invokeMethod("syncForTaoke", {"isSync": isSync});
  253. }
  254. // 是否需要 Native AliPay 接口
  255. static useAlipayNative(bool isNeed) {
  256. _channel.invokeMethod("useAlipayNative", {"isNeed": isNeed});
  257. }
  258. }