app-starlock/star_lock/lib/tools/aliyunRealNameAuth/aliyunRealNameAuthHandle.dart
Daisy d7db12dba9 1,新增接入阿里云人脸认证SDK流程
2,接收及设置实名认证频次接口对接及逻辑处理
3,新增检测实名认证是否支持开启接口对接
4,新增检测certifyId是否完成认证接口对接
5,实现需实名认证的钥匙,认证完成后方可开锁的逻辑处理
2024-05-07 18:07:57 +08:00

87 lines
2.8 KiB
Dart
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/baseGetXController.dart';
class AliyunRealNameAuthProvider {
final aliyunFacePlugin = AliyunFacePlugin(); //实名认证初始化
var infos = ''; //打印信息
var metainfosMap = {}; //认证信息
var certifyId = ''; //认证ID
var getLockInfo = LockListInfoItemEntity(); //锁信息
final Function(bool) onCertifyResult; //认证结果
AliyunRealNameAuthProvider(
{required this.getLockInfo, required this.onCertifyResult});
//初始化
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 {
var entity = await ApiRepository.to.getServiceCheckCertify(
certifyId: certifyId,
keyId: getLockInfo.keyId ?? 0,
);
if (entity.errorCode!.codeIsSuccessful) {
// 如果认证成功,则调用回调函数,将结果传递给调用处
onCertifyResult(true);
} else {
// 如果认证失败
// await startVerify();
}
}
}