智莺生活APP的阿里百川 Flutter 插件
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

284 righe
9.0 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.invokeMethod("initAlibc", {"version": version, "appName": appName});
  34. return InitModel(result[AlibcConstKey.errorCode], result[AlibcConstKey.errorMessage]);
  35. }
  36. ///
  37. /// @description: 登录淘宝
  38. ///
  39. /// @return: 成功则返回的data为用户信息,失败则没有data
  40. ///
  41. static Future<LoginModel> loginTaoBao() async {
  42. Map result = await _channel.invokeMethod("loginTaoBao");
  43. // 判断成功还是失败
  44. if (result[AlibcConstKey.errorCode] != "0") {
  45. return LoginModel(
  46. result[AlibcConstKey.errorCode],
  47. result[AlibcConstKey.errorMessage],
  48. );
  49. }
  50. return LoginModel(result[AlibcConstKey.errorCode], result[AlibcConstKey.errorMessage],
  51. data: UserModel(result[AlibcConstKey.data]["nick"], result[AlibcConstKey.data]["avatarUrl"], result[AlibcConstKey.data]["openId"], result[AlibcConstKey.data]["openSid"],
  52. result[AlibcConstKey.data]["topAccessToken"], result[AlibcConstKey.data]["topAuthCode"]));
  53. }
  54. ///
  55. /// @description: 退出淘宝登录
  56. /// @param {type}
  57. /// @return:
  58. ///
  59. static loginOut() {
  60. _channel.invokeMethod("loginOut");
  61. }
  62. ///
  63. /// @description: 渠道授权,获取access_token
  64. /// @param {type}
  65. /// @return:
  66. /// Map<String,String>
  67. static Future<Map<dynamic, dynamic>> taoKeLogin({
  68. @required String url,
  69. AlibcOpenType openType = AlibcOpenType.AlibcOpenTypeAuto,
  70. bool isNeedCustomNativeFailMode = false,
  71. AlibcNativeFailMode nativeFailMode = AlibcNativeFailMode.AlibcNativeFailModeNone,
  72. AlibcSchemeType schemeType = AlibcSchemeType.AlibcSchemeTaoBao,
  73. TaokeParams taokeParams,
  74. String backUrl,
  75. }) async {
  76. Map taoKe = AlibcTools.getTaokeMap(taokeParams);
  77. Map result = await _channel.invokeMethod("taoKeLogin", {
  78. "url": url,
  79. "openType": openType.index,
  80. "isNeedCustomNativeFailMode": isNeedCustomNativeFailMode,
  81. "nativeFailMode": nativeFailMode.index,
  82. "schemeType": schemeType.index,
  83. "taokeParams": taoKe,
  84. "backUrl": backUrl
  85. });
  86. return result;
  87. }
  88. ///
  89. /// @description: 渠道授权,获取access_token
  90. /// name app名字
  91. /// alibcAppKey 百川Key
  92. /// Map<String,String>
  93. static Future<Map<dynamic, dynamic>> taoKeLoginNew(String name, String alibcAppKey) async {
  94. Map result = await _channel.invokeMethod("taoKeLoginNew", {
  95. "name": name,
  96. "alibcAppKey": alibcAppKey,
  97. });
  98. return result;
  99. }
  100. ///
  101. /// @description: 通过url打开,包括h5,唤起手淘等
  102. /// @param
  103. /// url:目标url
  104. /// openType:打开类型
  105. /// isNeedCustomNativeFailMode:是否需要设置唤端失败策略
  106. /// nativeFailMode:唤端失败策略
  107. /// schemeType:唤起哪个端
  108. /// taokeParams:淘客数据
  109. /// backUrl: 跳转回来的url
  110. /// @return:
  111. ///
  112. static Future<TradeResult> openByUrl(
  113. {@required String url,
  114. AlibcOpenType openType = AlibcOpenType.AlibcOpenTypeAuto,
  115. bool isNeedCustomNativeFailMode = false,
  116. AlibcNativeFailMode nativeFailMode = AlibcNativeFailMode.AlibcNativeFailModeNone,
  117. AlibcSchemeType schemeType = AlibcSchemeType.AlibcSchemeTmall,
  118. TaokeParams taokeParams,
  119. String backUrl,
  120. bool isAuth = false}) async {
  121. Map taoKe = AlibcTools.getTaokeMap(taokeParams);
  122. var result = await _channel.invokeMethod("openByUrl", {
  123. "url": url,
  124. "openType": openType.index,
  125. "isNeedCustomNativeFailMode": isNeedCustomNativeFailMode,
  126. "nativeFailMode": nativeFailMode.index,
  127. "schemeType": schemeType.index,
  128. "taokeParams": taoKe,
  129. "backUrl": backUrl,
  130. "auth": isAuth
  131. });
  132. if (isAuth && Platform.isAndroid) {
  133. TradeResult tradeResult = TradeResult("-1", "");
  134. if (result != null && result) {
  135. ///授权成功
  136. tradeResult.errorCode = "0";
  137. return tradeResult;
  138. } else {
  139. ///授权失败
  140. tradeResult.errorCode = "-1";
  141. return tradeResult;
  142. }
  143. }
  144. TradeResult tradeResult = AlibcTools.getTradeResult(result);
  145. return tradeResult;
  146. }
  147. ///
  148. /// @description: 打开商品详情
  149. /// @param
  150. /// 同上
  151. /// itemID 商品id,可以是真实的也可以是混淆的
  152. /// isNeedPush iOS独占
  153. /// @return:
  154. ///
  155. static Future<TradeResult> openItemDetail({
  156. @required String itemID,
  157. // iOS独占
  158. // bool isNeedPush = false,
  159. AlibcOpenType openType = AlibcOpenType.AlibcOpenTypeAuto,
  160. bool isNeedCustomNativeFailMode = false,
  161. AlibcNativeFailMode nativeFailMode = 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 = AlibcNativeFailMode.AlibcNativeFailModeNone,
  196. AlibcSchemeType schemeType = AlibcSchemeType.AlibcSchemeTmall,
  197. TaokeParams taokeParams,
  198. // 额外需要追踪的业务数据
  199. Map trackParam,
  200. String backUrl,
  201. }) async {
  202. Map taoKe = AlibcTools.getTaokeMap(taokeParams);
  203. Map result = await _channel.invokeMethod("openShop", {
  204. "shopId": shopId,
  205. // "isNeedPush": isNeedPush,
  206. "openType": openType.index,
  207. "isNeedCustomNativeFailMode": isNeedCustomNativeFailMode,
  208. "nativeFailMode": nativeFailMode.index,
  209. "schemeType": schemeType.index,
  210. "taokeParams": taoKe,
  211. "trackParam": trackParam,
  212. "backUrl": backUrl
  213. });
  214. TradeResult tradeResult = AlibcTools.getTradeResult(result);
  215. return tradeResult;
  216. }
  217. ///
  218. /// @description: 打开购物车
  219. /// @param {type}
  220. /// @return:
  221. ///
  222. static Future<TradeResult> openCart({
  223. // iOS独占
  224. // bool isNeedPush = false,
  225. AlibcOpenType openType = AlibcOpenType.AlibcOpenTypeAuto,
  226. bool isNeedCustomNativeFailMode = false,
  227. AlibcNativeFailMode nativeFailMode = AlibcNativeFailMode.AlibcNativeFailModeNone,
  228. AlibcSchemeType schemeType = AlibcSchemeType.AlibcSchemeTmall,
  229. TaokeParams taokeParams,
  230. // 额外需要追踪的业务数据
  231. Map trackParam,
  232. String backUrl,
  233. }) async {
  234. Map taoKe = AlibcTools.getTaokeMap(taokeParams);
  235. Map result = await _channel.invokeMethod("openCart", {
  236. // "isNeedPush": isNeedPush,
  237. "openType": openType.index,
  238. "isNeedCustomNativeFailMode": isNeedCustomNativeFailMode,
  239. "nativeFailMode": nativeFailMode.index,
  240. "schemeType": schemeType.index,
  241. "taokeParams": taoKe,
  242. "trackParam": trackParam,
  243. "backUrl": backUrl
  244. });
  245. TradeResult tradeResult = AlibcTools.getTradeResult(result);
  246. return tradeResult;
  247. }
  248. // 是否需要设置打点
  249. static syncForTaoke(bool isSync) {
  250. _channel.invokeMethod("syncForTaoke", {"isSync": isSync});
  251. }
  252. // 是否需要 Native AliPay 接口
  253. static useAlipayNative(bool isNeed) {
  254. _channel.invokeMethod("useAlipayNative", {"isNeed": isNeed});
  255. }
  256. static Future<String> getUdid(bool isNeed) async {
  257. var map = await _channel.invokeMethod("getUdid");
  258. return map['udid'];
  259. }
  260. }