|
- /*
- * 与原生app进行交互
- * @Author: fnuoos
- * @Date: 2020-05-15 11:53:06
- * @Last Modified by: mikey.zhaopeng
- * @Last Modified time: 2020-05-15 11:53:58
- */
-
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:fluttertoast/fluttertoast.dart';
- import 'package:zhiying_comm/zhiying_comm.dart';
-
- class NativeUtil {
- // 工厂模式
- factory NativeUtil() => _getInstance();
- static NativeUtil get instance => _getInstance();
- static NativeUtil _instance;
-
- MethodChannel _methodChannel;
- NativeUtil._internal() {
- // 初始化
- _methodChannel = MethodChannel('hairuyi_flutter_comm://method');
- }
- static NativeUtil _getInstance() {
- if (_instance == null) {
- _instance = new NativeUtil._internal();
- }
- return _instance;
- }
-
- static Future<Map<String, dynamic>> _invokeChannel(String method,
- {Map<String, dynamic> params}) async {
- var res;
- try {
- res =
- await NativeUtil.instance._methodChannel.invokeMethod(method, params);
- } catch (e) {
- res = {'Failed': e.message};
- }
-
- print(res);
- return Map<String, dynamic>.from(res);
- }
-
- // 通过原生webview打开url
- static Future openUrl(BuildContext context, String url) {
- Map<String, dynamic> params = Map<String, dynamic>();
- params['url'] = url ?? '';
- params['SkipUIIdentifier'] = 'pub_wailian';
- NativeUtil.openPage(context, params);
- }
-
- // 公共跳转方法
- static Future openPage(BuildContext context, Map params) {
- // if (params.containsKey('is_to_flutter') &&
- // params['is_to_flutter'].toString() == '1') {
- // BasePageModel model = BasePageModel.fromJson(params);
- // return Navigator.of(context).push(MaterialPageRoute(
- // builder: (BuildContext context) =>
- // PageFactory.create(model.SkipUIIdentifier, model)));
- // }
- return NativeUtil._invokeChannel('openPage',
- params: new Map<String, dynamic>.from(params));
- }
-
- //跳转原生普通页面,非公共跳转
- static Future openNativePage(NativeRouter router, {Map params}) {
- String method = enumToString(router);
- return NativeUtil._invokeChannel('openNativePage', params: {
- 'method': method,
- 'params': params == null ? {} : Map<String, dynamic>.from(params)
- });
- }
-
- // 跳转原生商品详情
- static openGoodsDetail(Map<String, dynamic> params) async {
- NativeUtil._invokeChannel('openGoodsDetail', params: params);
- }
-
- static Future<String> getToken() async {
- Map<String, dynamic> data = await NativeUtil._invokeChannel('getToken');
- if (data.containsKey('token')) {
- return data['token'];
- }
- return '';
- }
-
- static Future<Map<String, dynamic>> getSetting() async {
- return NativeUtil._invokeChannel('getSetting');
- }
-
- // 打开支付宝支付
- static Future<bool> payByAlipay(String payCode) async {
- Map params = {"code": payCode};
- Map<String, dynamic> result = await NativeUtil._invokeChannel('openAliPay',
- params: new Map<String, dynamic>.from(params));
- if (result.containsKey('msg')) {
- Fluttertoast.showToast(
- msg: result['msg'],
- toastLength: Toast.LENGTH_SHORT,
- gravity: ToastGravity.BOTTOM,
- );
- }
- if (result.containsKey('success') && result['success'] == '1') {
- return true;
- }
- return false;
- }
-
- // 打开微信支付
- static Future<bool> payByWechat(Map params) async {
- Map<String, dynamic> result = await NativeUtil._invokeChannel(
- 'openWechatPay',
- params: new Map<String, dynamic>.from(params));
-
- if (result.containsKey('msg')) {
- Fluttertoast.showToast(
- msg: result['msg'],
- toastLength: Toast.LENGTH_SHORT,
- gravity: ToastGravity.BOTTOM,
- );
- }
- if (result.containsKey('success') && result['success'] == '1') {
- return true;
- }
- return false;
- }
-
- static Future<Map<String, dynamic>> onPagePop(bool isTop) async {
- NativeUtil._invokeChannel('pageDidPop',
- params: new Map<String, dynamic>.from({'isTop': isTop}));
- }
-
- static Future<Map<String, dynamic>> onPagePush(bool isTop) async {
- NativeUtil._invokeChannel('pageDidPush',
- params: new Map<String, dynamic>.from({'isTop': isTop}));
- }
-
- static Future<Map<String, dynamic>> invokeMethod(
- String method, Map<String, dynamic> params) {
- Map<String, dynamic> request = {"method": method, "params": params};
- return NativeUtil._invokeChannel('invokeMethod', params: request);
- }
- }
|