智莺生活APP的阿里百川 Flutter 插件
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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