app-starlock/lib/tools/aliyunRealNameAuth/aliyunRealNameAuthHandle.dart
2024-05-18 09:37:50 +08:00

89 lines
2.9 KiB
Dart
Executable File
Raw 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 {
final aliyunFacePlugin = AliyunFacePlugin(); //实名认证初始化
var infos = ''; //打印信息
var metainfosMap = {}; //认证信息
var certifyId = ''; //认证ID
var getLockInfo = LockListInfoItemEntity(); //锁信息
final Function(bool, int) onCertifyResultWithTime; //认证结果及下次认证时间
AliyunRealNameAuthProvider(
{required this.getLockInfo, required this.onCertifyResultWithTime});
//初始化
initAliyunRealNameAuth() {
aliyunFacePlugin.init();
getMetaInfos();
}
// 获取客户端metainfos将信息发送到服务器端调用服务器端相关接口获取认证ID即CertifyId。
Future<void> getMetaInfos() async {
String metainfos;
try {
metainfos = await aliyunFacePlugin.getMetaInfos() ?? 'Unknown metainfos';
metainfosMap = jsonDecode(metainfos);
} on PlatformException {
metainfos = 'Failed to get metainfos.';
}
infos = "metainfos: $metainfos";
AppLog.log(infos);
getFaceCertifyId();
}
// 获取人脸认证certifyId
void getFaceCertifyId() async {
LockCertifyEntity entity = await ApiRepository.to.getFaceCertifyId(
lockId: getLockInfo.lockId ?? 0,
keyId: getLockInfo.keyId ?? 0,
metaInfo: metainfosMap);
if (entity.errorCode!.codeIsSuccessful) {
certifyId = entity.data!.certifyId!;
startVerify();
}
}
// 调用认证接口CertifyId需要调用服务器端接口获取。
Future<void> startVerify() async {
String verifyResult;
try {
// 每个CertifyId只能使用一次否则会返回code: "2002(iOS), 1001(Android)"。
verifyResult =
await aliyunFacePlugin.verify("certifyId", certifyId) ?? '-1,error';
} on PlatformException {
verifyResult = '-2,exception';
}
infos = "verifyResult: $verifyResult";
AppLog.log(infos);
getServiceCheckCertify();
}
// 检测certifyId是否完成认证
Future<void> getServiceCheckCertify() async {
ServiceAuthResultEntity entity =
await ApiRepository.to.getServiceCheckCertify(
certifyId: certifyId,
keyId: getLockInfo.keyId ?? 0,
);
if (entity.errorCode!.codeIsSuccessful) {
// 如果认证成功,则调用回调函数,将结果传递给调用处
onCertifyResultWithTime(true, entity.data!.nextFaceValidateTime!);
} else {
// 如果认证失败
// await startVerify();
}
}
}