controller.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import 'dart:convert';
  2. import 'package:fis_common/index.dart';
  3. import 'package:fis_jsonrpc/rpc.dart';
  4. import 'package:get/get.dart';
  5. import 'package:idread/model/id_card_info_model.dart';
  6. import 'package:vitalapp/architecture/utils/prompt_box.dart';
  7. import 'package:idread/idread.dart';
  8. import 'package:vitalapp/js_platform/handlers/idcard_handler.dart';
  9. import 'package:vitalapp/managers/interfaces/device.dart';
  10. import 'package:vitalapp/managers/interfaces/models/device.dart';
  11. import 'package:vitalapp/rpc.dart';
  12. class IDInformation {
  13. String? name;
  14. String? sex;
  15. String? id;
  16. String? nation;
  17. String? birthday;
  18. String? address;
  19. IDInformation(
  20. {this.name, this.sex, this.id, this.nation, this.birthday, this.address});
  21. factory IDInformation.fromJson(Map<String, dynamic> json) {
  22. return IDInformation(
  23. name: json['Name'],
  24. sex: json['Sex'],
  25. id: json['ID'],
  26. nation: json['Nation'],
  27. birthday: json['Birthday'],
  28. address: json['Address'],
  29. );
  30. }
  31. Map<String, dynamic> toJson() {
  32. final Map<String, dynamic> data = new Map<String, dynamic>();
  33. data['Name'] = this.name;
  34. data['Sex'] = this.sex;
  35. data['ID'] = this.id;
  36. data['Nation'] = this.nation;
  37. data['Birthday'] = this.birthday;
  38. data['Address'] = this.address;
  39. return data;
  40. }
  41. }
  42. class CardReaderController extends GetxController {
  43. CardReaderController();
  44. bool isCardReaderConnected = false; // 是否连接读卡器
  45. bool isCardReaderBinding = false; //是否绑定读卡器
  46. _initData() async {
  47. if (FPlatform.isWindows) {
  48. Idread.isShell = true;
  49. Idread.shellRead = rpc.platform.startIDCardReader;
  50. Idread.shellStopRead = rpc.platform.stopIDCardReader;
  51. }
  52. bool result = await Idread.init();
  53. if (result) {
  54. bool start = await Idread.startRead();
  55. if (FPlatform.isWindows) {
  56. OpenIDCardPageHandler.onIDCardRead.addListener((sender, e) {
  57. var jsonOBJ = jsonDecode(e);
  58. var shellIDData = IDInformation.fromJson(jsonOBJ);
  59. var data = IdCardInfoModel(
  60. people: shellIDData.nation!,
  61. peopleName: shellIDData.name!,
  62. sex: shellIDData.sex!,
  63. birthDay: shellIDData.birthday!,
  64. address: shellIDData.address!,
  65. idCard: shellIDData.id!,
  66. );
  67. onReadInfo(data);
  68. });
  69. } else {
  70. Idread.dataStreamListen((data) {
  71. if (data is IdCardInfoModel) {
  72. onReadInfo(data);
  73. }
  74. });
  75. }
  76. }
  77. isCardReaderConnected = result;
  78. update(["card_reader"]);
  79. }
  80. Future<bool> getDevice(String type) async {
  81. List<DeviceModel> devices =
  82. await Get.find<IDeviceManager>().getDeviceList();
  83. var device = devices.firstWhereOrNull((element) => element.type == type);
  84. if (device != null) {
  85. return true;
  86. } else {
  87. return false;
  88. }
  89. }
  90. void checkReader() async {
  91. if (FPlatform.isWindows) {
  92. return;
  93. }
  94. bool result = await Idread.init();
  95. if (result) {
  96. bool start = await Idread.startRead();
  97. if (start) {
  98. Idread.dataStreamListen((data) {
  99. if (data is IdCardInfoModel) {
  100. onReadInfo(data);
  101. }
  102. });
  103. }
  104. } else {
  105. PromptBox.toast("未检测到读卡器,请检查连接后重试");
  106. }
  107. isCardReaderConnected = result;
  108. update(["card_reader"]);
  109. }
  110. /// 读取到数据的回调
  111. void onReadInfo(IdCardInfoModel data) {
  112. print(data.toJson().toString());
  113. final result = CardReaderResult(
  114. success: true,
  115. cardNo: data.idCard,
  116. name: data.peopleName,
  117. nation: data.people,
  118. gender: data.sex == '男' ? GenderEnum.Male : GenderEnum.Female,
  119. birthday: DateTime.parse(data.birthDay),
  120. address: data.address,
  121. );
  122. Get.back(
  123. result: result,
  124. );
  125. }
  126. Future<void> unInit() async {
  127. await Idread.unInit();
  128. }
  129. // @override
  130. // void onInit() {
  131. // super.onInit();
  132. // }
  133. @override
  134. void onReady() {
  135. super.onReady();
  136. _initData();
  137. }
  138. @override
  139. void onClose() {
  140. super.onClose();
  141. Idread.stopRead();
  142. unInit();
  143. }
  144. }
  145. class CardReaderResult {
  146. bool success;
  147. /// 身份证号
  148. String cardNo;
  149. /// 姓名
  150. String name;
  151. /// 性别
  152. GenderEnum gender;
  153. /// 民族
  154. String nation;
  155. /// 出生日期
  156. DateTime birthday;
  157. /// 地址
  158. String address;
  159. CardReaderResult({
  160. required this.success,
  161. required this.cardNo,
  162. required this.name,
  163. required this.gender,
  164. required this.nation,
  165. required this.birthday,
  166. required this.address,
  167. });
  168. }