fix: .
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Text;
|
|||||||
using Dlhis.Basic.Organization.Response;
|
using Dlhis.Basic.Organization.Response;
|
||||||
using Kurisu.AspNetCore.CustomClass;
|
using Kurisu.AspNetCore.CustomClass;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
namespace Dlhis.Basic.Organization.Services;
|
namespace Dlhis.Basic.Organization.Services;
|
||||||
|
|
||||||
@@ -72,7 +73,7 @@ public class AliyunClient : IAliyunClient
|
|||||||
var bodyString = BuildBodyString(parameters);
|
var bodyString = BuildBodyString(parameters);
|
||||||
|
|
||||||
var client = _httpClientFactory.CreateClient();
|
var client = _httpClientFactory.CreateClient();
|
||||||
client.DefaultRequestHeaders.Add("Host", Endpoint);
|
//client.DefaultRequestHeaders.Add("Host", Endpoint);
|
||||||
var content = new StringContent(bodyString, Encoding.UTF8, "application/x-www-form-urlencoded");
|
var content = new StringContent(bodyString, Encoding.UTF8, "application/x-www-form-urlencoded");
|
||||||
var response = await client.PostAsync(url, content);
|
var response = await client.PostAsync(url, content);
|
||||||
var rawResponse = await response.Content.ReadAsStringAsync();
|
var rawResponse = await response.Content.ReadAsStringAsync();
|
||||||
@@ -89,26 +90,39 @@ public class AliyunClient : IAliyunClient
|
|||||||
RawResponse = rawResponse
|
RawResponse = rawResponse
|
||||||
};
|
};
|
||||||
|
|
||||||
var response = JsonConvert.DeserializeObject<AliyunId3MetaVerifyApiResponse>(rawResponse);
|
try
|
||||||
result.RequestId = response.RequestId;
|
|
||||||
result.Code = response.Code;
|
|
||||||
result.Message = response.Message;
|
|
||||||
result.BizCode = response.ResultObject.BizCode;
|
|
||||||
result.Similarity = ParseVerifyScore(response.ResultObject.FaceDetail);
|
|
||||||
result.Passed = response.ResultObject.BizCode switch
|
|
||||||
{
|
{
|
||||||
"1" => true,
|
var response = JsonConvert.DeserializeObject<AliyunId3MetaVerifyApiResponse>(rawResponse);
|
||||||
"2" or "3" => false,
|
result.RequestId = response?.RequestId;
|
||||||
_ => null
|
result.Code = response?.Code;
|
||||||
};
|
result.Message = response?.Message;
|
||||||
|
result.BizCode = response?.ResultObject?.BizCode;
|
||||||
|
result.Similarity = ParseVerifyScore(response?.ResultObject?.FaceDetail);
|
||||||
|
result.Passed = result.BizCode switch
|
||||||
|
{
|
||||||
|
"1" => true,
|
||||||
|
"2" or "3" => false,
|
||||||
|
_ => null
|
||||||
|
};
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
result.Code = "ParseError";
|
||||||
|
result.Message = $"阿里云响应解析失败:{e.Message}";
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static double ParseVerifyScore(string faceDetail)
|
private static double? ParseVerifyScore(string faceDetail)
|
||||||
{
|
{
|
||||||
var detail = JsonConvert.DeserializeObject<AliyunId3MetaVerifyFaceDetail>(faceDetail);
|
if (string.IsNullOrWhiteSpace(faceDetail))
|
||||||
return detail.VerifyScore;
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var detail = JObject.Parse(faceDetail);
|
||||||
|
return detail.Value<double?>("verifyScore");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string ComputeSignature(SortedDictionary<string, string> parameters, string httpMethod, string accessKeySecret)
|
private static string ComputeSignature(SortedDictionary<string, string> parameters, string httpMethod, string accessKeySecret)
|
||||||
@@ -176,9 +190,4 @@ public class AliyunClient : IAliyunClient
|
|||||||
public string FaceDetail { get; set; }
|
public string FaceDetail { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
private class AliyunId3MetaVerifyFaceDetail
|
|
||||||
{
|
|
||||||
[JsonProperty("verifyScore")]
|
|
||||||
public double VerifyScore { get; set; }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,49 +79,36 @@ public class GainfoIdCardFaceCompareService : IGainfoIdCardFaceCompareService
|
|||||||
.Where(x => x.UserCode == gainfo.Code)
|
.Where(x => x.UserCode == gainfo.Code)
|
||||||
.SingleAsync();
|
.SingleAsync();
|
||||||
|
|
||||||
if (record == null)
|
|
||||||
{
|
|
||||||
record = new IdCardFaceCompareRecordEntity
|
|
||||||
{
|
|
||||||
UserCode = gainfo.Code,
|
|
||||||
UserType = gainfo.Type,
|
|
||||||
UserName = gainfo.Name,
|
|
||||||
IdNo = gainfo.IdNo,
|
|
||||||
CompareResult = IdCardFaceCompareResultEnumType.NotCompared,
|
|
||||||
CompareCount = 0,
|
|
||||||
ErrorMessage = null,
|
|
||||||
CreatedBy = userId,
|
|
||||||
ModifiedBy = userId,
|
|
||||||
CreateTime = now,
|
|
||||||
ModifiedTime = now
|
|
||||||
};
|
|
||||||
|
|
||||||
await _dbContext.InsertAsync(record);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
record.UserType = gainfo.Type;
|
|
||||||
record.UserName = gainfo.Name;
|
|
||||||
record.IdNo = gainfo.IdNo;
|
|
||||||
record.CompareResult = IdCardFaceCompareResultEnumType.NotCompared;
|
|
||||||
record.IsPassed = null;
|
|
||||||
record.Similarity = null;
|
|
||||||
record.ApiResponse = null;
|
|
||||||
record.ErrorMessage = null;
|
|
||||||
record.ModifiedTime = now;
|
|
||||||
record.ModifiedBy = userId;
|
|
||||||
await _dbContext.UpdateAsync(record);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!gainfo.IdNo.IsPresent())
|
if (!gainfo.IdNo.IsPresent())
|
||||||
{
|
{
|
||||||
await UpdateRecordErrorAndThrowAsync(record, "人员证件号为空,无法对比", userId);
|
await SaveCompareRecordAsync(
|
||||||
|
gainfo,
|
||||||
|
record,
|
||||||
|
IdCardFaceCompareResultEnumType.NotCompared,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
"人员证件号为空,无法对比",
|
||||||
|
userId,
|
||||||
|
now,
|
||||||
|
false);
|
||||||
|
throw new UserFriendlyException("人员证件号为空,无法对比");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (!request.Face.IsPresent())
|
if (!request.Face.IsPresent())
|
||||||
{
|
{
|
||||||
await UpdateRecordErrorAndThrowAsync(record, "人脸照片尚未录入", userId);
|
await SaveCompareRecordAsync(
|
||||||
|
gainfo,
|
||||||
|
record,
|
||||||
|
IdCardFaceCompareResultEnumType.NotCompared,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
"人脸照片尚未录入",
|
||||||
|
userId,
|
||||||
|
now,
|
||||||
|
false);
|
||||||
|
throw new UserFriendlyException("人脸照片尚未录入");
|
||||||
}
|
}
|
||||||
|
|
||||||
AliyunId3MetaVerifyResponse response;
|
AliyunId3MetaVerifyResponse response;
|
||||||
@@ -131,21 +118,31 @@ public class GainfoIdCardFaceCompareService : IGainfoIdCardFaceCompareService
|
|||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
await UpdateRecordErrorAndThrowAsync(record, $"人证人脸对比接口调用失败:{e.Message}", userId);
|
await SaveCompareRecordAsync(
|
||||||
|
gainfo,
|
||||||
|
record,
|
||||||
|
IdCardFaceCompareResultEnumType.NotCompared,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
$"人证人脸对比接口调用失败:{e.Message}",
|
||||||
|
userId,
|
||||||
|
now,
|
||||||
|
false);
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
record.CompareResult = IdCardFaceCompareResultEnumType.Compared;
|
record = await SaveCompareRecordAsync(
|
||||||
record.ApiResponse = response.RawResponse;
|
gainfo,
|
||||||
record.IsPassed = response.Passed;
|
record,
|
||||||
record.Similarity = response.Similarity;
|
IdCardFaceCompareResultEnumType.Compared,
|
||||||
record.CompareCount += 1;
|
response.Passed,
|
||||||
record.LastCompareTime = now;
|
response.Similarity,
|
||||||
record.ErrorMessage = null;
|
response.RawResponse,
|
||||||
record.ModifiedTime = DateTime.Now;
|
GetAliyunErrorMessage(response),
|
||||||
record.ModifiedBy = userId;
|
userId,
|
||||||
|
now,
|
||||||
await _dbContext.UpdateAsync(record);
|
true);
|
||||||
|
|
||||||
return await GetRecordAsync(record.UserCode);
|
return await GetRecordAsync(record.UserCode);
|
||||||
}
|
}
|
||||||
@@ -179,7 +176,9 @@ public class GainfoIdCardFaceCompareService : IGainfoIdCardFaceCompareService
|
|||||||
record.UserName = gainfo.Name;
|
record.UserName = gainfo.Name;
|
||||||
record.IdNo = gainfo.IdNo;
|
record.IdNo = gainfo.IdNo;
|
||||||
record.CompareResult = request.CompareResult
|
record.CompareResult = request.CompareResult
|
||||||
?? (request.ErrorMessage.IsPresent()
|
?? (request.ApiResponse.IsPresent()
|
||||||
|
? IdCardFaceCompareResultEnumType.Compared
|
||||||
|
: request.ErrorMessage.IsPresent()
|
||||||
? IdCardFaceCompareResultEnumType.NotCompared
|
? IdCardFaceCompareResultEnumType.NotCompared
|
||||||
: IdCardFaceCompareResultEnumType.Compared);
|
: IdCardFaceCompareResultEnumType.Compared);
|
||||||
record.ApiResponse = request.ApiResponse;
|
record.ApiResponse = request.ApiResponse;
|
||||||
@@ -203,13 +202,65 @@ public class GainfoIdCardFaceCompareService : IGainfoIdCardFaceCompareService
|
|||||||
return await GetRecordAsync(record.UserCode);
|
return await GetRecordAsync(record.UserCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task UpdateRecordErrorAndThrowAsync(IdCardFaceCompareRecordEntity record, string message, int userId)
|
private async Task<IdCardFaceCompareRecordEntity> SaveCompareRecordAsync(
|
||||||
|
GainfoEntity gainfo,
|
||||||
|
IdCardFaceCompareRecordEntity record,
|
||||||
|
IdCardFaceCompareResultEnumType compareResult,
|
||||||
|
bool? isPassed,
|
||||||
|
double? similarity,
|
||||||
|
string apiResponse,
|
||||||
|
string errorMessage,
|
||||||
|
int userId,
|
||||||
|
DateTime compareTime,
|
||||||
|
bool increaseCompareCount)
|
||||||
{
|
{
|
||||||
record.ErrorMessage = message;
|
var now = DateTime.Now;
|
||||||
record.ModifiedTime = DateTime.Now;
|
record ??= new IdCardFaceCompareRecordEntity
|
||||||
|
{
|
||||||
|
UserCode = gainfo.Code,
|
||||||
|
CompareCount = 0,
|
||||||
|
CreatedBy = userId,
|
||||||
|
CreateTime = now
|
||||||
|
};
|
||||||
|
|
||||||
|
record.UserType = gainfo.Type;
|
||||||
|
record.UserName = gainfo.Name;
|
||||||
|
record.IdNo = gainfo.IdNo;
|
||||||
|
record.CompareResult = compareResult;
|
||||||
|
record.ApiResponse = apiResponse;
|
||||||
|
record.IsPassed = isPassed;
|
||||||
|
record.Similarity = similarity;
|
||||||
|
record.ErrorMessage = errorMessage;
|
||||||
|
record.LastCompareTime = increaseCompareCount ? compareTime : record.LastCompareTime;
|
||||||
|
record.CompareCount += increaseCompareCount ? 1 : 0;
|
||||||
|
record.ModifiedTime = now;
|
||||||
record.ModifiedBy = userId;
|
record.ModifiedBy = userId;
|
||||||
await _dbContext.UpdateAsync(record);
|
|
||||||
throw new UserFriendlyException(message);
|
if (record.Id == 0)
|
||||||
|
{
|
||||||
|
await _dbContext.InsertAsync(record);
|
||||||
|
return await _dbContext.Queryable<IdCardFaceCompareRecordEntity>()
|
||||||
|
.Where(x => x.UserCode == gainfo.Code)
|
||||||
|
.SingleAsync();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await _dbContext.UpdateAsync(record);
|
||||||
|
}
|
||||||
|
|
||||||
|
return record;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GetAliyunErrorMessage(AliyunId3MetaVerifyResponse response)
|
||||||
|
{
|
||||||
|
if (response.Code != "200")
|
||||||
|
{
|
||||||
|
return response.Message.IsPresent()
|
||||||
|
? $"阿里云接口返回异常:{response.Message}"
|
||||||
|
: $"阿里云接口返回异常:{response.Code}";
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.BizCode.IsPresent() ? null : "阿里云接口未返回业务核验结果";
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<IdCardFaceCompareRecordResponse> GetRecordAsync(string userCode)
|
private async Task<IdCardFaceCompareRecordResponse> GetRecordAsync(string userCode)
|
||||||
|
|||||||
Reference in New Issue
Block a user