controller.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import 'package:fis_jsonrpc/rpc.dart';
  2. import 'package:get/get.dart';
  3. import 'package:idread/model/id_card_info_model.dart';
  4. import 'package:vitalapp/architecture/utils/prompt_box.dart';
  5. import 'package:idread/idread.dart';
  6. import 'package:vitalapp/managers/interfaces/device.dart';
  7. import 'package:vitalapp/managers/interfaces/models/device.dart';
  8. import 'package:vnote_device_plugin/consts/types.dart';
  9. class CardReaderController extends GetxController {
  10. CardReaderController();
  11. bool isCardReaderConnected = false; // 是否连接读卡器
  12. bool isCardReaderBinding = false; //是否绑定读卡器
  13. _initData() async {
  14. var isBinding = await getDevice(DeviceTypes.IC_READER);
  15. if (isBinding) {
  16. bool result = await Idread.init();
  17. if (result) {
  18. bool start = await Idread.startRead();
  19. if (start) {
  20. Idread.dataStreamListen((data) {
  21. if (data is IdCardInfoModel) {
  22. onReadInfo(data);
  23. }
  24. });
  25. }
  26. }
  27. isCardReaderConnected = result;
  28. update(["card_reader"]);
  29. }
  30. }
  31. Future<bool> getDevice(String type) async {
  32. List<DeviceModel> devices =
  33. await Get.find<IDeviceManager>().getDeviceList();
  34. var device = devices.firstWhereOrNull((element) => element.type == type);
  35. if (device != null) {
  36. return true;
  37. } else {
  38. return false;
  39. }
  40. }
  41. void checkReader() async {
  42. bool result = await Idread.init();
  43. if (result) {
  44. bool start = await Idread.startRead();
  45. if (start) {
  46. Idread.dataStreamListen((data) {
  47. if (data is IdCardInfoModel) {
  48. onReadInfo(data);
  49. }
  50. });
  51. }
  52. } else {
  53. PromptBox.toast("未检测到读卡器,请检查连接后重试");
  54. }
  55. isCardReaderConnected = result;
  56. update(["card_reader"]);
  57. }
  58. /// 读取到数据的回调
  59. void onReadInfo(IdCardInfoModel data) {
  60. final result = CardReaderResult(
  61. success: true,
  62. cardNo: data.idCard,
  63. name: data.peopleName,
  64. nation: data.people,
  65. gender: data.sex == '男' ? GenderEnum.Male : GenderEnum.Female,
  66. birthday: DateTime.parse(data.birthDay),
  67. address: data.address,
  68. );
  69. Get.back(
  70. result: result,
  71. );
  72. }
  73. Future<void> unInit() async {
  74. await Idread.unInit();
  75. }
  76. // @override
  77. // void onInit() {
  78. // super.onInit();
  79. // }
  80. @override
  81. void onReady() {
  82. super.onReady();
  83. _initData();
  84. }
  85. @override
  86. void onClose() {
  87. super.onClose();
  88. Idread.stopRead();
  89. unInit();
  90. }
  91. }
  92. class CardReaderResult {
  93. bool success;
  94. /// 身份证号
  95. String cardNo;
  96. /// 姓名
  97. String name;
  98. /// 性别
  99. GenderEnum gender;
  100. /// 民族
  101. String nation;
  102. /// 出生日期
  103. DateTime birthday;
  104. /// 地址
  105. String address;
  106. CardReaderResult({
  107. required this.success,
  108. required this.cardNo,
  109. required this.name,
  110. required this.gender,
  111. required this.nation,
  112. required this.birthday,
  113. required this.address,
  114. });
  115. }