init
This commit is contained in:
180
lib/idcard_method_channel.dart
Normal file
180
lib/idcard_method_channel.dart
Normal file
@@ -0,0 +1,180 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import 'idcard_platform_interface.dart';
|
||||
|
||||
/// 使用方法通道的IdcardPlatform实现
|
||||
class MethodChannelIdcard extends IdcardPlatform {
|
||||
/// 与原生平台交互的方法通道
|
||||
@visibleForTesting
|
||||
final methodChannel = const MethodChannel('idcard');
|
||||
|
||||
@override
|
||||
Future<String?> getPlatformVersion() async {
|
||||
final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
|
||||
return version;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<int> getUsbPermission(int vid, int pid) async {
|
||||
final result = await methodChannel.invokeMethod<int>('getUsbPermission', {
|
||||
'vid': vid,
|
||||
'pid': pid,
|
||||
});
|
||||
return result ?? -1;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<int> openDevice({String portType = 'USB', String portPara = '', String extendPara = ''}) async {
|
||||
final result = await methodChannel.invokeMethod<int>('openDevice', {
|
||||
'portType': portType,
|
||||
'portPara': portPara,
|
||||
'extendPara': extendPara,
|
||||
});
|
||||
return result ?? -1;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<int> setCurrentDevice(int devHandle) async {
|
||||
final result = await methodChannel.invokeMethod<int>('setCurrentDevice', {
|
||||
'devHandle': devHandle,
|
||||
});
|
||||
return result ?? -1;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<int> getCurrentDevice() async {
|
||||
final result = await methodChannel.invokeMethod<int>('getCurrentDevice');
|
||||
return result ?? -1;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Map<String, String>> getLibraryInfo() async {
|
||||
final result = await methodChannel.invokeMethod('getLibraryInfo');
|
||||
if (result == null) return {'version': '', 'description': ''};
|
||||
// 安全地转换类型
|
||||
final Map<String, dynamic> data = Map<String, dynamic>.from(result as Map);
|
||||
return {
|
||||
'version': data['version']?.toString() ?? '',
|
||||
'description': data['description']?.toString() ?? '',
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String> getTerminalModel() async {
|
||||
final result = await methodChannel.invokeMethod<String>('getTerminalModel');
|
||||
return result ?? '';
|
||||
}
|
||||
|
||||
@override
|
||||
Future<int> terminalHeartBeat() async {
|
||||
final result = await methodChannel.invokeMethod<int>('terminalHeartBeat');
|
||||
return result ?? -1;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<int>> getLastRecvData() async {
|
||||
final result = await methodChannel.invokeMethod<List<dynamic>>('getLastRecvData');
|
||||
return result?.cast<int>() ?? [];
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Map<String, String>> getTerminalFirmVersion() async {
|
||||
final result = await methodChannel.invokeMethod('getTerminalFirmVersion');
|
||||
if (result == null) return {'firmVersion': '', 'hardwareVersion': ''};
|
||||
// 安全地转换类型
|
||||
final Map<String, dynamic> data = Map<String, dynamic>.from(result as Map);
|
||||
return {
|
||||
'firmVersion': data['firmVersion']?.toString() ?? '',
|
||||
'hardwareVersion': data['hardwareVersion']?.toString() ?? '',
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String> getTerminalSn() async {
|
||||
final result = await methodChannel.invokeMethod<String>('getTerminalSn');
|
||||
return result ?? '';
|
||||
}
|
||||
|
||||
@override
|
||||
Future<int> closeDevice() async {
|
||||
final result = await methodChannel.invokeMethod<int>('closeDevice');
|
||||
return result ?? -1;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<int> findCard() async {
|
||||
final result = await methodChannel.invokeMethod<int>('findCard');
|
||||
return result ?? -1;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<int> selectCard() async {
|
||||
final result = await methodChannel.invokeMethod<int>('selectCard');
|
||||
return result ?? -1;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Map<String, dynamic>> idReadCard({int cardType = 0x00, int infoEncoding = 0x01, int timeOut = 30000}) async {
|
||||
final result = await methodChannel.invokeMethod('idReadCard', {
|
||||
'cardType': cardType,
|
||||
'infoEncoding': infoEncoding,
|
||||
'timeOut': timeOut,
|
||||
});
|
||||
if (result == null) {
|
||||
return {'result': -1};
|
||||
}
|
||||
// 安全地转换类型
|
||||
return Map<String, dynamic>.from(result as Map);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Map<String, dynamic>> readCard({int cardType = 1, int infoEncoding = 0, int timeOut = 30000}) async {
|
||||
final result = await methodChannel.invokeMethod('readCard', {
|
||||
'cardType': cardType,
|
||||
'infoEncoding': infoEncoding,
|
||||
'timeOut': timeOut,
|
||||
});
|
||||
if (result == null) {
|
||||
return {'result': -1};
|
||||
}
|
||||
// 安全地转换类型
|
||||
return Map<String, dynamic>.from(result as Map);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<IdCardInfo> readCardInfo() async {
|
||||
// 使用idReadCard方法读取身份证信息
|
||||
final result = await idReadCard();
|
||||
|
||||
if (result['result'] != 0) {
|
||||
throw Exception('Failed to read card info, error code: ${result['result']}');
|
||||
}
|
||||
|
||||
final String? dataStr = result['data'];
|
||||
if (dataStr == null || dataStr.isEmpty) {
|
||||
throw Exception('Card data is empty');
|
||||
}
|
||||
|
||||
// 根据文档,数据是冒号分隔的字符串格式
|
||||
return IdCardInfo.fromColonString(dataStr);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String> readNewAddress() async {
|
||||
final result = await methodChannel.invokeMethod<String>('readNewAddress');
|
||||
return result ?? '';
|
||||
}
|
||||
|
||||
@override
|
||||
Future<int> getSamStatus() async {
|
||||
final result = await methodChannel.invokeMethod<int>('getSamStatus');
|
||||
return result ?? -1;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String> getSamIdStr() async {
|
||||
final result = await methodChannel.invokeMethod<String>('getSamIdStr');
|
||||
return result ?? '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user