From f24804481fc188b423c64d0bb8a89ea7bc6d1ffc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E6=A2=93=E5=BD=AD?= Date: Fri, 20 Mar 2026 12:13:33 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BA=AB=E4=BB=BD=E8=AF=81=E8=AF=86=E5=88=AB?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YD_Event.Application/applicationsettings.json | 2 +- .../Infrastructure/Aliyun/Ocr/OcrService.cs | 47 ++++++++++++------- YD_Event.Core/YD_Event.Core.csproj | 2 +- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/YD_Event.Application/applicationsettings.json b/YD_Event.Application/applicationsettings.json index dc7a254..e1a49ba 100644 --- a/YD_Event.Application/applicationsettings.json +++ b/YD_Event.Application/applicationsettings.json @@ -153,6 +153,6 @@ "AliyunOCR": { "AccessKeyId": "LTAI5tLfbdZwBoA9bCZVjZkF", "AccessKeySecret": "4DmFIY77zwdp1gaRUTmBMf7WfdsNsC", - "Endpoint": "ocr.cn-shanghai.aliyuncs.com" + "Endpoint": "ocr-api.cn-hangzhou.aliyuncs.com" } } \ No newline at end of file diff --git a/YD_Event.Core/Infrastructure/Aliyun/Ocr/OcrService.cs b/YD_Event.Core/Infrastructure/Aliyun/Ocr/OcrService.cs index 18061d3..e9a5b1f 100644 --- a/YD_Event.Core/Infrastructure/Aliyun/Ocr/OcrService.cs +++ b/YD_Event.Core/Infrastructure/Aliyun/Ocr/OcrService.cs @@ -3,10 +3,11 @@ using Furion.DependencyInjection; using Microsoft.Extensions.Options; using YD_Event.Core.Options; using AlibabaCloud.OpenApiClient.Models; -using AlibabaCloud.SDK.Ocr20191230; -using AlibabaCloud.SDK.Ocr20191230.Models; +using AlibabaCloud.SDK.Ocr_api20210707; +using AlibabaCloud.SDK.Ocr_api20210707.Models; using System.Threading.Tasks; using System; +using Newtonsoft.Json.Linq; namespace YD_Event.Core.Infrastructure.Aliyun.Ocr; @@ -44,28 +45,38 @@ public class OcrService : ITransient }; var client = new Client(config); - var request = new RecognizeIdentityCardRequest + var request = new RecognizeIdcardRequest { - ImageURL = imageUrl, - Side = "face" // 默认正面 + Url = imageUrl }; - // 使用 RuntimeOptions 可以自定义超时等配置,这里简单起见使用默认 - var response = await client.RecognizeIdentityCardAsync(request); + var runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions(); + var response = await client.RecognizeIdcardWithOptionsAsync(request, runtime); - if (response.Body != null && response.Body.Data != null && response.Body.Data.FrontResult != null) + if (response.Body != null && !string.IsNullOrEmpty(response.Body.Data)) { - var data = response.Body.Data.FrontResult; - // 解析结果 - return new OcrResult + // Data是一个JSON字符串,包含识别结果 + var json = JObject.Parse(response.Body.Data); + + // 阿里云 OCR 统一识别 API 身份证返回的结构一般在 data.face.data 下或者直接是平铺的属性 + // 我们直接使用 SelectToken 来兼容获取 + var name = (string?)json.SelectToken("..data.name") ?? (string?)json.SelectToken("..name") ?? (string?)json.SelectToken("..data.face.data.name"); + var sex = (string?)json.SelectToken("..data.sex") ?? (string?)json.SelectToken("..sex") ?? (string?)json.SelectToken("..data.gender"); + var idNumber = (string?)json.SelectToken("..data.idNumber") ?? (string?)json.SelectToken("..idNumber"); + var birthDate = (string?)json.SelectToken("..data.birthDate") ?? (string?)json.SelectToken("..birthDate"); + + if (!string.IsNullOrEmpty(idNumber)) { - Success = true, - Name = data.Name, - Gender = data.Gender, - IdCardNumber = data.IDNumber, - BirthDate = data.BirthDate, - Message = "识别成功" - }; + return new OcrResult + { + Success = true, + Name = name, + Gender = sex, + IdCardNumber = idNumber, + BirthDate = birthDate, + Message = "识别成功" + }; + } } return new OcrResult { Success = false, Message = "未识别到有效身份证信息" }; diff --git a/YD_Event.Core/YD_Event.Core.csproj b/YD_Event.Core/YD_Event.Core.csproj index 66f237d..cef7d9b 100644 --- a/YD_Event.Core/YD_Event.Core.csproj +++ b/YD_Event.Core/YD_Event.Core.csproj @@ -9,7 +9,7 @@ - +