123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- import 'dart:convert';
- import 'package:fis_common/index.dart';
- import 'package:fis_jsonrpc/rpc.dart';
- import 'package:get/get.dart';
- import 'package:idread/model/id_card_info_model.dart';
- import 'package:vitalapp/architecture/utils/prompt_box.dart';
- import 'package:idread/idread.dart';
- import 'package:vitalapp/js_platform/handlers/idcard_handler.dart';
- import 'package:vitalapp/managers/interfaces/device.dart';
- import 'package:vitalapp/managers/interfaces/models/device.dart';
- import 'package:vitalapp/rpc.dart';
- class IDInformation {
- String? name;
- String? sex;
- String? id;
- String? nation;
- String? birthday;
- String? address;
- IDInformation(
- {this.name, this.sex, this.id, this.nation, this.birthday, this.address});
- factory IDInformation.fromJson(Map<String, dynamic> json) {
- return IDInformation(
- name: json['Name'],
- sex: json['Sex'],
- id: json['ID'],
- nation: json['Nation'],
- birthday: json['Birthday'],
- address: json['Address'],
- );
- }
- Map<String, dynamic> toJson() {
- final Map<String, dynamic> data = new Map<String, dynamic>();
- data['Name'] = this.name;
- data['Sex'] = this.sex;
- data['ID'] = this.id;
- data['Nation'] = this.nation;
- data['Birthday'] = this.birthday;
- data['Address'] = this.address;
- return data;
- }
- }
- class CardReaderController extends GetxController {
- CardReaderController();
- bool isCardReaderConnected = false; // 是否连接读卡器
- bool isCardReaderBinding = false; //是否绑定读卡器
- _initData() async {
- if (FPlatform.isWindows) {
- Idread.isShell = true;
- Idread.shellRead = rpc.platform.startIDCardReader;
- Idread.shellStopRead = rpc.platform.stopIDCardReader;
- }
- bool result = await Idread.init();
- if (result) {
- bool start = await Idread.startRead();
- if (FPlatform.isWindows) {
- OpenIDCardPageHandler.onIDCardRead.addListener((sender, e) {
- var jsonOBJ = jsonDecode(e);
- var shellIDData = IDInformation.fromJson(jsonOBJ);
- var data = IdCardInfoModel(
- people: shellIDData.nation!,
- peopleName: shellIDData.name!,
- sex: shellIDData.sex!,
- birthDay: shellIDData.birthday!,
- address: shellIDData.address!,
- idCard: shellIDData.id!,
- );
- onReadInfo(data);
- });
- } else {
- Idread.dataStreamListen((data) {
- if (data is IdCardInfoModel) {
- onReadInfo(data);
- }
- });
- }
- }
- isCardReaderConnected = result;
- update(["card_reader"]);
- }
- Future<bool> getDevice(String type) async {
- List<DeviceModel> devices =
- await Get.find<IDeviceManager>().getDeviceList();
- var device = devices.firstWhereOrNull((element) => element.type == type);
- if (device != null) {
- return true;
- } else {
- return false;
- }
- }
- void checkReader() async {
- if (FPlatform.isWindows) {
- return;
- }
- bool result = await Idread.init();
- if (result) {
- bool start = await Idread.startRead();
- if (start) {
- Idread.dataStreamListen((data) {
- if (data is IdCardInfoModel) {
- onReadInfo(data);
- }
- });
- }
- } else {
- PromptBox.toast("未检测到读卡器,请检查连接后重试");
- }
- isCardReaderConnected = result;
- update(["card_reader"]);
- }
- /// 读取到数据的回调
- void onReadInfo(IdCardInfoModel data) {
- print(data.toJson().toString());
- final result = CardReaderResult(
- success: true,
- cardNo: data.idCard,
- name: data.peopleName,
- nation: data.people,
- gender: data.sex == '男' ? GenderEnum.Male : GenderEnum.Female,
- birthday: DateTime.parse(data.birthDay),
- address: data.address,
- );
- Get.back(
- result: result,
- );
- }
- Future<void> unInit() async {
- await Idread.unInit();
- }
- // @override
- // void onInit() {
- // super.onInit();
- // }
- @override
- void onReady() {
- super.onReady();
- _initData();
- }
- @override
- void onClose() {
- super.onClose();
- Idread.stopRead();
- unInit();
- }
- }
- class CardReaderResult {
- bool success;
- /// 身份证号
- String cardNo;
- /// 姓名
- String name;
- /// 性别
- GenderEnum gender;
- /// 民族
- String nation;
- /// 出生日期
- DateTime birthday;
- /// 地址
- String address;
- CardReaderResult({
- required this.success,
- required this.cardNo,
- required this.name,
- required this.gender,
- required this.nation,
- required this.birthday,
- required this.address,
- });
- }
|