lcl 4 ヶ月 前
コミット
debcb1b986
3 ファイル変更117 行追加1 行削除
  1. 36 0
      Areas/Api/Controllers/PubController.cs
  2. 2 1
      MySystem.csproj
  3. 79 0
      Util/Aliyun/AliyunOcr.cs

+ 36 - 0
Areas/Api/Controllers/PubController.cs

@@ -0,0 +1,36 @@
+using System;
+using System.Collections.Generic;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Http;
+using LitJson;
+using Microsoft.Extensions.Logging;
+using Microsoft.Extensions.Options;
+
+namespace MySystem.Areas.Api.Controllers
+{
+    [Area("Api")]
+    [Route("Api/[controller]/[action]")]
+    public class PubController : BaseController
+    {
+        public PubController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
+        {
+        }
+        
+        #region OCR识别-营业执照
+        public JsonResult BusinessLicenseOcr(string value)
+        {
+            value = DesDecrypt(value);
+            Dictionary<string, object> Obj = BusinessLicenseOcrDo(value);
+            return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
+        }
+        public Dictionary<string, object> BusinessLicenseOcrDo(string value)
+        {
+            JsonData data = JsonMapper.ToObject(value);
+            string Url = data["Url"].ToString();
+            Dictionary<string, object> Obj = new Dictionary<string, object>();
+            Obj.Add("OcrData", AliyunOcr.Check(Url));
+            return Obj;
+        }
+        #endregion
+    }
+}

+ 2 - 1
MySystem.csproj

@@ -50,6 +50,7 @@
     </Reference>
   </ItemGroup>
   <ItemGroup>
+    <PackageReference Include="AlibabaCloud.SDK.Ocr-api20210707" Version="3.1.2" />
     <PackageReference Include="Aliyun.Api.LogService" Version="1.1.0" />
     <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.0.0" />
     <PackageReference Include="RabbitMQ.Client" Version="6.2.2" />
@@ -69,7 +70,7 @@
     <PackageReference Include="System.Drawing.Common" Version="4.7.0" />
     <PackageReference Include="MySql.Data" Version="8.0.18" />
     <PackageReference Include="CSRedisCore" Version="3.6.5" />
-    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0"/>
+    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" />
   </ItemGroup>
   <ItemGroup>
     <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />

+ 79 - 0
Util/Aliyun/AliyunOcr.cs

@@ -0,0 +1,79 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Web;
+using Library;
+using Tea;
+using Tea.Utils;
+
+namespace MySystem
+{
+    public class AliyunOcr
+    {
+        private static string AccessKeyID = "LTAI5tBNY8CzWquEUx8HGqUX";
+        private static string AccessKeySecret = "VOGf7ibdydKeKqkb2BfEwp7Nh4gPRJ";
+        
+        /**
+         * 使用AK&SK初始化账号Client
+         * @return Client
+         * @throws Exception
+         */
+        public static AlibabaCloud.SDK.Ocr_api20210707.Client CreateClient()
+        {
+            // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
+            // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378671.html。
+            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
+            {
+                // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
+                AccessKeyId = AccessKeyID,
+                // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
+                AccessKeySecret = AccessKeySecret,
+            };
+            // Endpoint 请参考 https://api.aliyun.com/product/ocr-api
+            config.Endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
+            return new AlibabaCloud.SDK.Ocr_api20210707.Client(config);
+        }
+
+        public static string Check(string Url)
+        {
+            string result = "";
+            AlibabaCloud.SDK.Ocr_api20210707.Client client = CreateClient();
+            AlibabaCloud.SDK.Ocr_api20210707.Models.RecognizeBusinessLicenseRequest recognizeBusinessLicenseRequest = new AlibabaCloud.SDK.Ocr_api20210707.Models.RecognizeBusinessLicenseRequest
+            {
+                Url = Url,
+            };
+            AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
+            try
+            {
+                // 复制代码运行请自行打印 API 的返回值
+                var res = client.RecognizeBusinessLicenseWithOptions(recognizeBusinessLicenseRequest, runtime);
+                result = res.Body.Data;
+            }
+            catch (TeaException error)
+            {
+                // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
+                // 错误 message
+                function.WriteLog(error.Message, "OCR异常");
+                // 诊断地址
+                function.WriteLog(error.Data["Recommend"].ToString(), "OCR异常");
+                AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
+            }
+            catch (Exception _error)
+            {
+                TeaException error = new TeaException(new Dictionary<string, object>
+                {
+                    { "message", _error.Message }
+                });
+                // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
+                // 错误 message
+                function.WriteLog(error.Message, "OCR异常");
+                // 诊断地址
+                function.WriteLog(error.Data["Recommend"].ToString(), "OCR异常");
+                AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
+            }
+            return result;
+        }
+
+        
+    }
+}