app-starlock/lib/tools/aliyunRealNameAuth/aliyunRealNameAuthHandle.dart
2024-05-22 10:41:44 +08:00

96 lines
3.2 KiB
Dart
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:convert';
import 'package:aliyun_face_plugin/aliyun_face_plugin.dart';
import 'package:flutter/services.dart';
import 'package:star_lock/app_settings/app_settings.dart';
import 'package:star_lock/main/lockMian/entity/lockListInfo_entity.dart';
import 'package:star_lock/network/api_repository.dart';
import 'package:star_lock/tools/aliyunRealNameAuth/realNameVertify_entity.dart';
import 'package:star_lock/tools/aliyunRealNameAuth/serviceAuthResult_entity.dart';
import 'package:star_lock/tools/baseGetXController.dart';
class AliyunRealNameAuthProvider {
AliyunRealNameAuthProvider({
required this.getLockInfo,
required this.onCertifyResultWithTime,
});
final AliyunFacePlugin aliyunFacePlugin = AliyunFacePlugin();
final Function(bool, int) onCertifyResultWithTime; //认证结果及下次认证时间回调
final LockListInfoItemEntity getLockInfo; //锁信息
String infos = ''; //打印信息
Map<String, dynamic> metainfosMap = {}; //认证信息
String certifyId = ''; //认证ID
void initAliyunRealNameAuth() {
aliyunFacePlugin.init();
getMetaInfos();
}
// 获取客户端metainfos将信息发送到服务器端
Future<void> getMetaInfos() async {
try {
final String metainfos =
await aliyunFacePlugin.getMetaInfos() ?? 'Unknown metainfos';
metainfosMap = jsonDecode(metainfos);
infos = 'metainfos: $metainfos';
AppLog.log(infos);
await getFaceCertifyId();
} on PlatformException {
infos = '获取metainfos失败。';
AppLog.log(infos);
}
}
//调用服务器端相关接口获取认证ID即CertifyId
Future<void> getFaceCertifyId() async {
try {
final LockCertifyEntity entity = await ApiRepository.to.getFaceCertifyId(
lockId: getLockInfo.lockId ?? 0,
keyId: getLockInfo.keyId ?? 0,
metaInfo: metainfosMap,
);
if (entity.errorCode!.codeIsSuccessful) {
certifyId = entity.data!.certifyId!;
await startVerify();
} else {
AppLog.log('获取certifyId失败: ${entity.errorCode}');
}
} catch (e) {
AppLog.log('getFaceCertifyId中出现异常: $e');
}
}
// 调用认证接口CertifyId需要调用服务器端接口获取。
Future<void> startVerify() async {
try {
final String verifyResult =
await aliyunFacePlugin.verify('certifyId', certifyId) ?? '-1,error';
infos = 'verifyResult: $verifyResult';
AppLog.log(infos);
await getServiceCheckCertify();
} on PlatformException {
infos = '验证过程中出现异常。';
AppLog.log(infos);
}
}
// 检测certifyId是否完成认证
Future<void> getServiceCheckCertify() async {
try {
final ServiceAuthResultEntity entity =
await ApiRepository.to.getServiceCheckCertify(
certifyId: certifyId,
keyId: getLockInfo.keyId ?? 0,
);
if (entity.errorCode!.codeIsSuccessful) {
onCertifyResultWithTime(true, entity.data!.nextFaceValidateTime!);
} else {
AppLog.log('认证失败: ${entity.errorCode}');
// 可选:在这里处理重试逻辑
}
} catch (e) {
AppLog.log('getServiceCheckCertify中出现异常: $e');
}
}
}