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

146 wiersze
4.4 KiB

  1. /*
  2. * 与原生app进行交互
  3. * @Author: fnuoos
  4. * @Date: 2020-05-15 11:53:06
  5. * @Last Modified by: mikey.zhaopeng
  6. * @Last Modified time: 2020-05-15 11:53:58
  7. */
  8. import 'package:flutter/material.dart';
  9. import 'package:flutter/services.dart';
  10. import 'package:fluttertoast/fluttertoast.dart';
  11. import 'package:zhiying_comm/zhiying_comm.dart';
  12. class NativeUtil {
  13. // 工厂模式
  14. factory NativeUtil() => _getInstance();
  15. static NativeUtil get instance => _getInstance();
  16. static NativeUtil _instance;
  17. MethodChannel _methodChannel;
  18. NativeUtil._internal() {
  19. // 初始化
  20. _methodChannel = MethodChannel('hairuyi_flutter_comm://method');
  21. }
  22. static NativeUtil _getInstance() {
  23. if (_instance == null) {
  24. _instance = new NativeUtil._internal();
  25. }
  26. return _instance;
  27. }
  28. static Future<Map<String, dynamic>> _invokeChannel(String method,
  29. {Map<String, dynamic> params}) async {
  30. var res;
  31. try {
  32. res =
  33. await NativeUtil.instance._methodChannel.invokeMethod(method, params);
  34. } catch (e) {
  35. res = {'Failed': e.message};
  36. }
  37. print(res);
  38. return Map<String, dynamic>.from(res);
  39. }
  40. // 通过原生webview打开url
  41. static Future openUrl(BuildContext context, String url) {
  42. Map<String, dynamic> params = Map<String, dynamic>();
  43. params['url'] = url ?? '';
  44. params['SkipUIIdentifier'] = 'pub_wailian';
  45. NativeUtil.openPage(context, params);
  46. }
  47. // 公共跳转方法
  48. static Future openPage(BuildContext context, Map params) {
  49. // if (params.containsKey('is_to_flutter') &&
  50. // params['is_to_flutter'].toString() == '1') {
  51. // BasePageModel model = BasePageModel.fromJson(params);
  52. // return Navigator.of(context).push(MaterialPageRoute(
  53. // builder: (BuildContext context) =>
  54. // PageFactory.create(model.SkipUIIdentifier, model)));
  55. // }
  56. return NativeUtil._invokeChannel('openPage',
  57. params: new Map<String, dynamic>.from(params));
  58. }
  59. //跳转原生普通页面,非公共跳转
  60. static Future openNativePage(NativeRouter router, {Map params}) {
  61. String method = enumToString(router);
  62. return NativeUtil._invokeChannel('openNativePage', params: {
  63. 'method': method,
  64. 'params': params == null ? {} : Map<String, dynamic>.from(params)
  65. });
  66. }
  67. // 跳转原生商品详情
  68. static openGoodsDetail(Map<String, dynamic> params) async {
  69. NativeUtil._invokeChannel('openGoodsDetail', params: params);
  70. }
  71. static Future<String> getToken() async {
  72. Map<String, dynamic> data = await NativeUtil._invokeChannel('getToken');
  73. if (data.containsKey('token')) {
  74. return data['token'];
  75. }
  76. return '';
  77. }
  78. static Future<Map<String, dynamic>> getSetting() async {
  79. return NativeUtil._invokeChannel('getSetting');
  80. }
  81. // 打开支付宝支付
  82. static Future<bool> payByAlipay(String payCode) async {
  83. Map params = {"code": payCode};
  84. Map<String, dynamic> result = await NativeUtil._invokeChannel('openAliPay',
  85. params: new Map<String, dynamic>.from(params));
  86. if (result.containsKey('msg')) {
  87. Fluttertoast.showToast(
  88. msg: result['msg'],
  89. toastLength: Toast.LENGTH_SHORT,
  90. gravity: ToastGravity.BOTTOM,
  91. );
  92. }
  93. if (result.containsKey('success') && result['success'] == '1') {
  94. return true;
  95. }
  96. return false;
  97. }
  98. // 打开微信支付
  99. static Future<bool> payByWechat(Map params) async {
  100. Map<String, dynamic> result = await NativeUtil._invokeChannel(
  101. 'openWechatPay',
  102. params: new Map<String, dynamic>.from(params));
  103. if (result.containsKey('msg')) {
  104. Fluttertoast.showToast(
  105. msg: result['msg'],
  106. toastLength: Toast.LENGTH_SHORT,
  107. gravity: ToastGravity.BOTTOM,
  108. );
  109. }
  110. if (result.containsKey('success') && result['success'] == '1') {
  111. return true;
  112. }
  113. return false;
  114. }
  115. static Future<Map<String, dynamic>> onPagePop(bool isTop) async {
  116. NativeUtil._invokeChannel('pageDidPop',
  117. params: new Map<String, dynamic>.from({'isTop': isTop}));
  118. }
  119. static Future<Map<String, dynamic>> onPagePush(bool isTop) async {
  120. NativeUtil._invokeChannel('pageDidPush',
  121. params: new Map<String, dynamic>.from({'isTop': isTop}));
  122. }
  123. static Future<Map<String, dynamic>> invokeMethod(
  124. String method, Map<String, dynamic> params) {
  125. Map<String, dynamic> request = {"method": method, "params": params};
  126. return NativeUtil._invokeChannel('invokeMethod', params: request);
  127. }
  128. }