fix: .
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Text;
|
||||
using Dlhis.Basic.Organization.Response;
|
||||
using Kurisu.AspNetCore.CustomClass;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Dlhis.Basic.Organization.Services;
|
||||
|
||||
@@ -72,7 +73,7 @@ public class AliyunClient : IAliyunClient
|
||||
var bodyString = BuildBodyString(parameters);
|
||||
|
||||
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 response = await client.PostAsync(url, content);
|
||||
var rawResponse = await response.Content.ReadAsStringAsync();
|
||||
@@ -89,26 +90,39 @@ public class AliyunClient : IAliyunClient
|
||||
RawResponse = rawResponse
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var response = JsonConvert.DeserializeObject<AliyunId3MetaVerifyApiResponse>(rawResponse);
|
||||
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
|
||||
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 = result.BizCode switch
|
||||
{
|
||||
"1" => true,
|
||||
"2" or "3" => false,
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
result.Code = "ParseError";
|
||||
result.Message = $"阿里云响应解析失败:{e.Message}";
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static double ParseVerifyScore(string faceDetail)
|
||||
private static double? ParseVerifyScore(string faceDetail)
|
||||
{
|
||||
var detail = JsonConvert.DeserializeObject<AliyunId3MetaVerifyFaceDetail>(faceDetail);
|
||||
return detail.VerifyScore;
|
||||
if (string.IsNullOrWhiteSpace(faceDetail))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var detail = JObject.Parse(faceDetail);
|
||||
return detail.Value<double?>("verifyScore");
|
||||
}
|
||||
|
||||
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; }
|
||||
}
|
||||
|
||||
private class AliyunId3MetaVerifyFaceDetail
|
||||
{
|
||||
[JsonProperty("verifyScore")]
|
||||
public double VerifyScore { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,49 +79,36 @@ public class GainfoIdCardFaceCompareService : IGainfoIdCardFaceCompareService
|
||||
.Where(x => x.UserCode == gainfo.Code)
|
||||
.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())
|
||||
{
|
||||
await UpdateRecordErrorAndThrowAsync(record, "人员证件号为空,无法对比", userId);
|
||||
await SaveCompareRecordAsync(
|
||||
gainfo,
|
||||
record,
|
||||
IdCardFaceCompareResultEnumType.NotCompared,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
"人员证件号为空,无法对比",
|
||||
userId,
|
||||
now,
|
||||
false);
|
||||
throw new UserFriendlyException("人员证件号为空,无法对比");
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
@@ -131,21 +118,31 @@ public class GainfoIdCardFaceCompareService : IGainfoIdCardFaceCompareService
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
await UpdateRecordErrorAndThrowAsync(record, $"人证人脸对比接口调用失败:{e.Message}", userId);
|
||||
await SaveCompareRecordAsync(
|
||||
gainfo,
|
||||
record,
|
||||
IdCardFaceCompareResultEnumType.NotCompared,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
$"人证人脸对比接口调用失败:{e.Message}",
|
||||
userId,
|
||||
now,
|
||||
false);
|
||||
throw;
|
||||
}
|
||||
|
||||
record.CompareResult = IdCardFaceCompareResultEnumType.Compared;
|
||||
record.ApiResponse = response.RawResponse;
|
||||
record.IsPassed = response.Passed;
|
||||
record.Similarity = response.Similarity;
|
||||
record.CompareCount += 1;
|
||||
record.LastCompareTime = now;
|
||||
record.ErrorMessage = null;
|
||||
record.ModifiedTime = DateTime.Now;
|
||||
record.ModifiedBy = userId;
|
||||
|
||||
await _dbContext.UpdateAsync(record);
|
||||
record = await SaveCompareRecordAsync(
|
||||
gainfo,
|
||||
record,
|
||||
IdCardFaceCompareResultEnumType.Compared,
|
||||
response.Passed,
|
||||
response.Similarity,
|
||||
response.RawResponse,
|
||||
GetAliyunErrorMessage(response),
|
||||
userId,
|
||||
now,
|
||||
true);
|
||||
|
||||
return await GetRecordAsync(record.UserCode);
|
||||
}
|
||||
@@ -179,7 +176,9 @@ public class GainfoIdCardFaceCompareService : IGainfoIdCardFaceCompareService
|
||||
record.UserName = gainfo.Name;
|
||||
record.IdNo = gainfo.IdNo;
|
||||
record.CompareResult = request.CompareResult
|
||||
?? (request.ErrorMessage.IsPresent()
|
||||
?? (request.ApiResponse.IsPresent()
|
||||
? IdCardFaceCompareResultEnumType.Compared
|
||||
: request.ErrorMessage.IsPresent()
|
||||
? IdCardFaceCompareResultEnumType.NotCompared
|
||||
: IdCardFaceCompareResultEnumType.Compared);
|
||||
record.ApiResponse = request.ApiResponse;
|
||||
@@ -203,13 +202,65 @@ public class GainfoIdCardFaceCompareService : IGainfoIdCardFaceCompareService
|
||||
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;
|
||||
record.ModifiedTime = DateTime.Now;
|
||||
var now = 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;
|
||||
|
||||
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);
|
||||
throw new UserFriendlyException(message);
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user