James 3 هفته پیش
والد
کامیت
358103b822
2فایلهای تغییر یافته به همراه66 افزوده شده و 9 حذف شده
  1. 37 9
      lib/utils/biometricAuthUtils.dart
  2. 29 0
      lib/utils/tools.dart

+ 37 - 9
lib/utils/biometricAuthUtils.dart

@@ -1,5 +1,4 @@
 
-import 'package:flutter/services.dart';
 import 'package:appfx/utils/logger.dart';
 import 'package:local_auth/local_auth.dart';
 import 'package:flutter_secure_storage/flutter_secure_storage.dart';
@@ -7,6 +6,23 @@ import 'package:flutter_secure_storage/flutter_secure_storage.dart';
 export 'package:local_auth/local_auth.dart' show BiometricType;
 
 
+
+class BioAuthResult {
+  final bool authed;
+  final int errorCode;
+  final String errorReason;
+
+  BioAuthResult({
+    this.authed = false,
+    this.errorCode = 0,
+    this.errorReason = '',
+  });
+
+  bool get isAuthed => (errorCode == 0) && authed;
+  bool get isError  => (errorCode < 0);
+  bool get isBenignError  => (errorCode > 0);
+}
+
 class BiometricAuthUtils {
 
   bool canCheckBiometrics = false;
@@ -40,7 +56,7 @@ class BiometricAuthUtils {
     try {
       canCheckBiometrics = await auth.canCheckBiometrics;
       return canCheckBiometrics;
-    } on PlatformException catch (e) {
+    } catch (e) {
       Log.e('检查设备是否支持生物识别失败: $e');
       canCheckBiometrics = false;
       return false;
@@ -52,7 +68,7 @@ class BiometricAuthUtils {
     try {
       availableBiometrics = await auth.getAvailableBiometrics();
       return availableBiometrics;
-    } on PlatformException catch (e) {
+    } catch (e) {
       Log.e('获取生物识别类型失败: $e');
       availableBiometrics = [];
       return availableBiometrics;
@@ -60,18 +76,30 @@ class BiometricAuthUtils {
   }
 
   // 进行生物识别认证
-  Future<bool> authenticateWithBiometrics(String reason) async {
+  Future<BioAuthResult> authenticateWithBiometrics(String reason) async {
     try {
-      final bool didAuthenticate = await auth.authenticate(
+      final bool authed = await auth.authenticate(
         localizedReason: reason, // '请进行人脸识别以访问您的密码',
         biometricOnly: true,
         sensitiveTransaction: true,
         persistAcrossBackgrounding: false,
       );
-      return didAuthenticate;
-    } on PlatformException catch (e) {
-      Log.e('认证失败: $e');
-      return false;
+      return BioAuthResult(authed: authed, errorCode: 0);
+
+    } on LocalAuthException catch (e) {
+      Log.e('biometric LocalAuthException error: $e');
+      if ((e.code == LocalAuthExceptionCode.timeout) ||
+          (e.code == LocalAuthExceptionCode.userCanceled) ||
+          (e.code == LocalAuthExceptionCode.systemCanceled) ||
+          (e.code == LocalAuthExceptionCode.temporaryLockout)) {
+        return BioAuthResult(errorCode: 1, errorReason: e.description ?? 'canceled');
+
+      } else {
+        return BioAuthResult(errorCode: -1, errorReason: e.description ?? e.toString());
+      }
+    } catch (e) {
+      Log.e('biometric authenticate error: $e');
+      return BioAuthResult(errorCode: -1, errorReason: e.toString());
     }
   }
 

+ 29 - 0
lib/utils/tools.dart

@@ -1,5 +1,6 @@
 
 import 'dart:convert';
+import 'dart:math';
 
 
 // DateFormat('dd.MM.yy HH:mm:ss').format(dateCreated); // ??
@@ -110,3 +111,31 @@ List<int> uniformGrouping(int totalSize, int defaultGroupSize) {
 
   return group;
 }
+
+
+String ellipsizeStrMiddle(String str, {
+  int headerLen = 4,
+  int tailLen = 4,
+  String splitter = '...',
+}) {
+  if (str.length > 2) {
+    int liveHeaderLen = headerLen;
+    int liveTailLen = tailLen;
+    int loopN = min(liveHeaderLen, liveTailLen);
+    for (int i = 0; i < loopN; i++) {
+      if (str.length > (liveHeaderLen + liveTailLen)) {
+        String prefix = str.substring(0, liveHeaderLen);
+        String suffix = str.substring(str.length - liveTailLen);
+        return '$prefix$splitter$suffix';
+      }
+      liveHeaderLen--;
+      liveTailLen--;
+    }
+    String prefix = str.substring(0, liveHeaderLen);
+    String suffix = str.substring(str.length - liveTailLen);
+    return '$prefix$splitter$suffix';
+  } else {
+    return str;
+  }
+}
+