flutter_wxwork_method_channel.dart 2.46 KB
import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'flutter_wxwork_platform_interface.dart';

/// An implementation of [FlutterWxworkPlatform] that uses method channels.
class MethodChannelFlutterWxwork extends FlutterWxworkPlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('flutter_wxwork');

  @override
  Future<String?> getPlatformVersion() async {
    final version =
        await methodChannel.invokeMethod<String>('getPlatformVersion');
    return version;
  }

  @override
  Future<bool?> registerApp(scheme, corpId, agentId) async {
    String? result =
        await methodChannel.invokeMethod<String>('register', <String, String>{
      'scheme': scheme,
      'corpId': corpId,
      'agentId': agentId,
    });
    return _stringToBool(result);
  }

  @override
  Future<String?> isAppInstalled() async {
    final isAppInstalled =
        await methodChannel.invokeMethod<String>('isAppInstalled');
    return isAppInstalled;
  }

  @override
  Future<bool?> shareImage() async {
    final shareImage = await methodChannel.invokeMethod<bool>('shareImage');
    return shareImage;
  }

  @override
  Future<bool?> shareMiniProgram(
      {String? userName, //必须是应用关联的小程序,注意要有@app后缀
      String? path,
      String? hdImageData,
      String title = "分享标题"}) async {
    final shareMiniProgram =
        await methodChannel.invokeMethod<bool>('shareMiniProgram');
    return shareMiniProgram;
  }

  @override
  Future<AuthModel?> auth({String state = 'state'}) async {
    var content = await methodChannel.invokeMethod('auth', {"state": state});
    return AuthModel.fromJson(Map<String, dynamic>.from(content));
  }

  bool _stringToBool(String? string) {
    if (string == '1') {
      return true;
    } else {
      return false;
    }
  }
}

class AuthModel {
  /// 1.取消 0.成功 2.失败
  String? errCode;
  String? code;
  String? state;

  bool get isSuccess => errCode == '1';
  AuthModel();

  factory AuthModel.fromJson(Map<String, dynamic> json) {
    AuthModel model = AuthModel();
    model.errCode = json['errCode'];
    model.code = json['code'];
    model.state = json['state'];
    return model;
  }

  Map<String, dynamic> toJson() {
    return {'errorCode': errCode, 'code': code, 'state': state};
  }

  @override
  String toString() {
    return jsonEncode(this);
  }
}