智莺生活APP的阿里百川 Flutter 插件
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.
 
 
 
 
 
 

298 lines
9.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.invokeMethod("initAlibc", {"version": version, "appName": appName}) as FutureOr<Map<dynamic, dynamic>>);
  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") as FutureOr<Map<dynamic, dynamic>>);
  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: 渠道授权,获取access_token
  102. /// @param {type}
  103. /// @return:
  104. /// Map<String,String>
  105. static Future<Map<dynamic, dynamic>?> taoKeLoginNewIOS(String name, String alibcAppKey) async {
  106. Map? result = await _channel.invokeMethod("taoKeLogin", {
  107. "name": name,
  108. "alibcAppKey": alibcAppKey,
  109. });
  110. return result;
  111. }
  112. ///
  113. /// @description: 通过url打开,包括h5,唤起手淘等
  114. /// @param
  115. /// url:目标url
  116. /// openType:打开类型
  117. /// isNeedCustomNativeFailMode:是否需要设置唤端失败策略
  118. /// nativeFailMode:唤端失败策略
  119. /// schemeType:唤起哪个端
  120. /// taokeParams:淘客数据
  121. /// backUrl: 跳转回来的url
  122. /// @return:
  123. ///
  124. static Future<TradeResult> openByUrl(
  125. {required String url,
  126. AlibcOpenType openType = AlibcOpenType.AlibcOpenTypeAuto,
  127. bool isNeedCustomNativeFailMode = false,
  128. AlibcNativeFailMode nativeFailMode = AlibcNativeFailMode.AlibcNativeFailModeNone,
  129. AlibcSchemeType schemeType = AlibcSchemeType.AlibcSchemeTmall,
  130. TaokeParams? taokeParams,
  131. String? backUrl,
  132. bool isAuth = false}) async {
  133. Map? taoKe = AlibcTools.getTaokeMap(taokeParams);
  134. var result = await _channel.invokeMethod("openByUrl", {
  135. "url": url,
  136. "openType": openType.index,
  137. "isNeedCustomNativeFailMode": isNeedCustomNativeFailMode,
  138. "nativeFailMode": nativeFailMode.index,
  139. "schemeType": schemeType.index,
  140. "taokeParams": taoKe,
  141. "backUrl": backUrl,
  142. "auth": isAuth
  143. });
  144. if (isAuth && Platform.isAndroid) {
  145. TradeResult tradeResult = TradeResult("-1", "");
  146. if (result != null && result) {
  147. ///授权成功
  148. tradeResult.errorCode = "0";
  149. return tradeResult;
  150. } else {
  151. ///授权失败
  152. tradeResult.errorCode = "-1";
  153. return tradeResult;
  154. }
  155. }
  156. TradeResult tradeResult = AlibcTools.getTradeResult(result);
  157. return tradeResult;
  158. }
  159. ///
  160. /// @description: 打开商品详情
  161. /// @param
  162. /// 同上
  163. /// itemID 商品id,可以是真实的也可以是混淆的
  164. /// isNeedPush iOS独占
  165. /// @return:
  166. ///
  167. static Future<TradeResult> openItemDetail({
  168. required String itemID,
  169. // iOS独占
  170. // bool isNeedPush = false,
  171. AlibcOpenType openType = AlibcOpenType.AlibcOpenTypeAuto,
  172. bool isNeedCustomNativeFailMode = false,
  173. AlibcNativeFailMode nativeFailMode = AlibcNativeFailMode.AlibcNativeFailModeNone,
  174. AlibcSchemeType schemeType = AlibcSchemeType.AlibcSchemeTmall,
  175. TaokeParams? taokeParams,
  176. // 额外需要追踪的业务数据
  177. Map? trackParam,
  178. String? backUrl,
  179. }) async {
  180. Map? taoKe = AlibcTools.getTaokeMap(taokeParams);
  181. Map result = await (_channel.invokeMethod("openItemDetail", {
  182. "itemID": itemID,
  183. // "isNeedPush": isNeedPush,
  184. "openType": openType.index,
  185. "isNeedCustomNativeFailMode": isNeedCustomNativeFailMode,
  186. "nativeFailMode": nativeFailMode.index,
  187. "schemeType": schemeType.index,
  188. "taokeParams": taoKe,
  189. "trackParam": trackParam,
  190. "backUrl": backUrl
  191. }) as FutureOr<Map<dynamic, dynamic>>);
  192. TradeResult tradeResult = AlibcTools.getTradeResult(result);
  193. return tradeResult;
  194. }
  195. ///
  196. /// @description: 打开店铺
  197. /// @param {type}
  198. /// shopId 店铺id
  199. /// @return:
  200. ///
  201. static Future<TradeResult> openShop({
  202. required String shopId,
  203. // iOS独占
  204. // bool isNeedPush = false,
  205. AlibcOpenType openType = AlibcOpenType.AlibcOpenTypeAuto,
  206. bool isNeedCustomNativeFailMode = false,
  207. AlibcNativeFailMode nativeFailMode = AlibcNativeFailMode.AlibcNativeFailModeNone,
  208. AlibcSchemeType schemeType = AlibcSchemeType.AlibcSchemeTmall,
  209. TaokeParams? taokeParams,
  210. // 额外需要追踪的业务数据
  211. Map? trackParam,
  212. String? backUrl,
  213. }) async {
  214. Map? taoKe = AlibcTools.getTaokeMap(taokeParams);
  215. Map result = await (_channel.invokeMethod("openShop", {
  216. "shopId": shopId,
  217. // "isNeedPush": isNeedPush,
  218. "openType": openType.index,
  219. "isNeedCustomNativeFailMode": isNeedCustomNativeFailMode,
  220. "nativeFailMode": nativeFailMode.index,
  221. "schemeType": schemeType.index,
  222. "taokeParams": taoKe,
  223. "trackParam": trackParam,
  224. "backUrl": backUrl
  225. }) as FutureOr<Map<dynamic, dynamic>>);
  226. TradeResult tradeResult = AlibcTools.getTradeResult(result);
  227. return tradeResult;
  228. }
  229. ///
  230. /// @description: 打开购物车
  231. /// @param {type}
  232. /// @return:
  233. ///
  234. static Future<TradeResult> openCart({
  235. // iOS独占
  236. // bool isNeedPush = false,
  237. AlibcOpenType openType = AlibcOpenType.AlibcOpenTypeAuto,
  238. bool isNeedCustomNativeFailMode = false,
  239. AlibcNativeFailMode nativeFailMode = AlibcNativeFailMode.AlibcNativeFailModeNone,
  240. AlibcSchemeType schemeType = AlibcSchemeType.AlibcSchemeTmall,
  241. TaokeParams? taokeParams,
  242. // 额外需要追踪的业务数据
  243. Map? trackParam,
  244. String? backUrl,
  245. }) async {
  246. Map? taoKe = AlibcTools.getTaokeMap(taokeParams);
  247. Map result = await (_channel.invokeMethod("openCart", {
  248. // "isNeedPush": isNeedPush,
  249. "openType": openType.index,
  250. "isNeedCustomNativeFailMode": isNeedCustomNativeFailMode,
  251. "nativeFailMode": nativeFailMode.index,
  252. "schemeType": schemeType.index,
  253. "taokeParams": taoKe,
  254. "trackParam": trackParam,
  255. "backUrl": backUrl
  256. }) as FutureOr<Map<dynamic, dynamic>>);
  257. TradeResult tradeResult = AlibcTools.getTradeResult(result);
  258. return tradeResult;
  259. }
  260. // 是否需要设置打点
  261. static syncForTaoke(bool isSync) {
  262. _channel.invokeMethod("syncForTaoke", {"isSync": isSync});
  263. }
  264. // 是否需要 Native AliPay 接口
  265. static useAlipayNative(bool isNeed) {
  266. _channel.invokeMethod("useAlipayNative", {"isNeed": isNeed});
  267. }
  268. static Future<String?> getUdid() async {
  269. var map = await _channel.invokeMethod("getUdid");
  270. print(map);
  271. return map['udid'];
  272. }
  273. }