| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- import 'package:appfx/utils/logger.dart';
- import 'package:local_auth/local_auth.dart';
- 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;
- List<BiometricType> availableBiometrics = [];
- final LocalAuthentication auth = LocalAuthentication();
- final FlutterSecureStorage storage = const FlutterSecureStorage();
- Future<void> init() async {
- try {
- canCheckBiometrics = await checkBiometrics();
- if (canCheckBiometrics) {
- availableBiometrics = await getAvailableBiometrics();
- }
- } catch (error) {
- Log.e('init error: $error');
- }
- }
- bool get isSupportFaceId {
- return availableBiometrics.any((type) => type == BiometricType.face);
- }
- bool get isSupportTouchId {
- return availableBiometrics.any((type) => type == BiometricType.fingerprint);
- }
- // 检查设备是否支持生物识别
- Future<bool> checkBiometrics() async {
- try {
- canCheckBiometrics = await auth.canCheckBiometrics;
- return canCheckBiometrics;
- } catch (e) {
- Log.e('检查设备是否支持生物识别失败: $e');
- canCheckBiometrics = false;
- return false;
- }
- }
- // 获取可用的生物识别类型
- Future<List<BiometricType>> getAvailableBiometrics() async {
- try {
- availableBiometrics = await auth.getAvailableBiometrics();
- return availableBiometrics;
- } catch (e) {
- Log.e('获取生物识别类型失败: $e');
- availableBiometrics = [];
- return availableBiometrics;
- }
- }
- // 进行生物识别认证
- Future<BioAuthResult> authenticateWithBiometrics(String reason) async {
- try {
- final bool authed = await auth.authenticate(
- localizedReason: reason, // '请进行人脸识别以访问您的密码',
- // biometricOnly: true,
- sensitiveTransaction: true,
- persistAcrossBackgrounding: 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());
- }
- }
- }
|