From 23ab3a1c9aef0ed71d75af46238346533ef2eaa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?ZR20090193-=E9=99=88=E6=95=AC=E5=8B=87?= Date: Mon, 24 Jun 2024 11:46:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=B7=E8=BF=90=E5=87=BA=E5=8F=A3ocr?= =?UTF-8?q?=E8=AF=86=E5=88=AB=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MultiLanguageMiddleware.cs | 1 + .../Op/Interface/ISeaExportCommonService.cs | 7 ++ .../Op/Interface/ISeaExportService.cs | 9 ++ .../Op/Method/SeaExportCommonService.cs | 17 ++++ .../Op/Method/SeaExportOcrService.cs | 92 +++++++++++++++++++ .../Controllers/SeaExportController.cs | 91 +++++++++++++++++- ds-wms-service/DS.WMS.Test/SaasTest.cs | 3 +- 7 files changed, 216 insertions(+), 4 deletions(-) create mode 100644 ds-wms-service/DS.WMS.Core/Op/Method/SeaExportOcrService.cs diff --git a/ds-wms-service/DS.Module.MultiLanguage/MultiLanguageMiddleware.cs b/ds-wms-service/DS.Module.MultiLanguage/MultiLanguageMiddleware.cs index 038ba0aa..108029bf 100644 --- a/ds-wms-service/DS.Module.MultiLanguage/MultiLanguageMiddleware.cs +++ b/ds-wms-service/DS.Module.MultiLanguage/MultiLanguageMiddleware.cs @@ -47,6 +47,7 @@ public class MultiLanguageMiddleware || context.Request.Path.Value.IndexOf("HealthCheck", StringComparison.InvariantCultureIgnoreCase) > -1 || context.Request.Path.Value.IndexOf("DownloadOpFileInfo", StringComparison.InvariantCultureIgnoreCase) > -1 || context.Request.Path.Value.IndexOf("LinkAttach", StringComparison.InvariantCultureIgnoreCase) > -1 + || context.Request.Path.Value.IndexOf("GetOcrImg", StringComparison.InvariantCultureIgnoreCase) > -1 ) ) { diff --git a/ds-wms-service/DS.WMS.Core/Op/Interface/ISeaExportCommonService.cs b/ds-wms-service/DS.WMS.Core/Op/Interface/ISeaExportCommonService.cs index 86c9a32a..7ff2336a 100644 --- a/ds-wms-service/DS.WMS.Core/Op/Interface/ISeaExportCommonService.cs +++ b/ds-wms-service/DS.WMS.Core/Op/Interface/ISeaExportCommonService.cs @@ -58,5 +58,12 @@ namespace DS.WMS.Core.Op.Interface /// /// public string GetPortCode(long Id, SqlSugarScopeProvider tenantDb); + + /// + /// 获取系统配置信息 + /// + /// + /// + public Task> GetConfigData(string code); } } diff --git a/ds-wms-service/DS.WMS.Core/Op/Interface/ISeaExportService.cs b/ds-wms-service/DS.WMS.Core/Op/Interface/ISeaExportService.cs index 3de50126..a826dd21 100644 --- a/ds-wms-service/DS.WMS.Core/Op/Interface/ISeaExportService.cs +++ b/ds-wms-service/DS.WMS.Core/Op/Interface/ISeaExportService.cs @@ -2,6 +2,7 @@ using DS.Module.Core; using DS.Module.Core.Data; using DS.WMS.Core.Op.Dtos; using DS.WMS.Core.Op.EDI; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace DS.WMS.Core.Op.Interface; @@ -151,4 +152,12 @@ public interface ISeaExportService /// 订舱主键数组 /// 每单的处理提交结果 public Task>> SubmitTelex(IdModel req); + + /// + /// 上传OCR附件 + /// + /// + /// + /// + public Task> UploadOcrFile(IFormFile file, [FromForm] OpFileReq req); } \ No newline at end of file diff --git a/ds-wms-service/DS.WMS.Core/Op/Method/SeaExportCommonService.cs b/ds-wms-service/DS.WMS.Core/Op/Method/SeaExportCommonService.cs index d69e35b7..5b577ab1 100644 --- a/ds-wms-service/DS.WMS.Core/Op/Method/SeaExportCommonService.cs +++ b/ds-wms-service/DS.WMS.Core/Op/Method/SeaExportCommonService.cs @@ -1806,6 +1806,23 @@ namespace DS.WMS.Core.Op.Method } } + + #endregion + + /// + /// + /// + /// + /// + public async Task> GetConfigData(string code) + { + var config = await db.Queryable().Where(x => x.Code == code && x.Status == StatusEnum.Enable).FirstAsync(); + if (config.IsNull()) + { + return await Task.FromResult(DataResult.Failed("OCR接口地址未配置!")); + } + return await Task.FromResult(DataResult.Success(config.Value)); + } } } diff --git a/ds-wms-service/DS.WMS.Core/Op/Method/SeaExportOcrService.cs b/ds-wms-service/DS.WMS.Core/Op/Method/SeaExportOcrService.cs new file mode 100644 index 00000000..d52585e3 --- /dev/null +++ b/ds-wms-service/DS.WMS.Core/Op/Method/SeaExportOcrService.cs @@ -0,0 +1,92 @@ +using DS.Module.Core; +using DS.Module.Core.Extensions; +using DS.Module.Core.Helpers; +using DS.WMS.Core.Code.Entity; +using DS.WMS.Core.Map.Entity; +using DS.WMS.Core.Op.Dtos; +using DS.WMS.Core.Op.Entity; +using DS.WMS.Core.Sys.Entity; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using NPOI.Util; +using SqlSugar; + +namespace DS.WMS.Core.Op.Method +{ + /// + /// 海运出口Ocr相关接口 + /// + public partial class SeaExportService + { + + #region 调用大简云OCR接口 + + /// + /// 上传OCR附件 + /// + /// + /// + /// + public async Task> UploadOcrFile(IFormFile file, [FromForm] OpFileReq req) + { + var tenantDb = saasService.GetBizDbScopeById(user.TenantId); + //未上传文件 + if (file == null || file.Length == 0) + { + return await Task.FromResult(DataResult.Failed("附件不存在!")); + } + // 先存库获取Id + var newFile = new OpFile + { + TypeCode = req.TypeCode, + TypeName = req.TypeName, + LinkId = req.LinkId, + }; + + var originalFilename = file.FileName; // 文件原始名称 + var fileSuffix = Path.GetExtension(file.FileName).ToLower(); // 文件后缀 + + var config = await db.Queryable().Where(x => x.Code == "ocr_api_url" && x.Status == StatusEnum.Enable).FirstAsync(); + if (config.IsNull()) + { + return await Task.FromResult(DataResult.Failed("OCR接口地址未配置!")); + } + var url = config.Value; + if (!url.EndsWith("/")) + { + url += "/"; + } + var ms = new MemoryStream(); + await file.CopyToAsync(ms); + _logger.Info($"调用ocr处理文件{originalFilename}"); + //使用HttpClient方式上传文件 + ms.Position = 0; + var formData = new MultipartFormDataContent(); + formData.Add(new StreamContent(ms, (int)ms.Length), "file", originalFilename); + var _httpclient = new HttpClient(); + var response = await _httpclient.PostAsync($"{url}pdf/upload", formData); + if (response.IsSuccessStatusCode) + { + var strRtn = response.Content.ReadAsStringAsync().Result; + var jobj = strRtn.ToJObject(); + if (jobj.GetIntValue("code") == 0) + { + newFile.FileName = jobj.GetStringValue("data"); + await tenantDb.Insertable(newFile).ExecuteCommandAsync(); + return await Task.FromResult(DataResult.Success(newFile.FileName)); + } + else + { + return await Task.FromResult(DataResult.Failed(jobj.GetStringValue("message"))); + } + } + else + { + return await Task.FromResult(DataResult.Failed("请求大简云OCR接口错误,请联系管理员!")); + } + } + #endregion + } +} diff --git a/ds-wms-service/DS.WMS.OpApi/Controllers/SeaExportController.cs b/ds-wms-service/DS.WMS.OpApi/Controllers/SeaExportController.cs index 1d6651d8..96533b62 100644 --- a/ds-wms-service/DS.WMS.OpApi/Controllers/SeaExportController.cs +++ b/ds-wms-service/DS.WMS.OpApi/Controllers/SeaExportController.cs @@ -1,5 +1,7 @@ -using DS.Module.Core; +using Amazon.Runtime.Internal.Util; +using DS.Module.Core; using DS.Module.Core.Data; +using DS.Module.Core.Helpers; using DS.WMS.Core.Info.Dtos; using DS.WMS.Core.Info.Interface; using DS.WMS.Core.Op.Dtos; @@ -8,6 +10,8 @@ using DS.WMS.Core.Op.Interface; using DS.WMS.Core.Sys.Dtos; using DS.WMS.Core.Sys.Interface; using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json; +using NPOI.XWPF.UserModel; using Org.BouncyCastle.Ocsp; namespace DS.WMS.OpApi.Controllers; @@ -18,14 +22,15 @@ namespace DS.WMS.OpApi.Controllers; public class SeaExportController : ApiController { private readonly ISeaExportService _invokeService; - + private readonly ISeaExportCommonService _commonService; /// /// 构造函数 /// /// - public SeaExportController(ISeaExportService invokeService) + public SeaExportController(ISeaExportService invokeService, ISeaExportCommonService commonService) { _invokeService = invokeService; + _commonService = commonService; } /// @@ -274,4 +279,84 @@ public class SeaExportController : ApiController return res; } + + /// + /// 上传OCR附件 + /// + /// 文件信息 + /// 业务附件请求实体 TypeCode = "other",TypeName = "其他" + /// + [HttpPost] + [Route("UploadOcrFile")] + public async Task> UploadOcrFile(IFormFile file, [FromForm] OpFileReq req) + { + var res = _invokeService.UploadOcrFile(file, req); + return await res; + } + + + /// + /// 获取文字 + /// + /// 文件名称 + /// 缩放比例 + /// x坐标 + /// y坐标 + /// 宽度 + /// 高度 + /// + [HttpGet("GetOcrText")] + public async Task> GetOcrText([FromQuery] string fileName, float scale, int x, int y, int w, int h) + { + var config = await _commonService.GetConfigData("ocr_api_url"); + + if (!config.Succeeded) + { + return await Task.FromResult(DataResult.Failed("OCR接口地址未配置!")); + } + var url = config.Data; + if (!url.EndsWith("/")) + { + url += "/"; + } + var postUrl = $"{url}pdf/getRegionText?fileName={fileName}&scale={scale}&x={x}&y={y}&w={w}&h={h}"; + //var str = await $"{url}pdf/getRegionText?fileName={fileName}&scale={scale}&x={x}&y={y}&w={w}&h={h}" + //.PostAsStringAsync(); + + var str = RequestHelper.Post("",url); + return await Task.FromResult(DataResult.Success(str)); ; + } + + /// + /// 获取图片 + /// + /// 文件名称 + /// 缩放比例,默认为1.5 + /// + [HttpGet] + [Route("GetOcrImg")] + [ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)] + public async Task GetOcrImg([FromQuery] string fileName, float scale = 1.5f) + { + var config = await _commonService.GetConfigData("ocr_api_url"); + + //if (!config.Succeeded) + //{ + // return await Task.FromResult(DataResult.Failed("OCR接口地址未配置!")); + //} + var url = config.Data; + if (!url.EndsWith("/")) + { + url += "/"; + } + var postUrl = $"{url}pdf/getCanvasImage?fileName={fileName}&scale={scale}"; + + byte[] byteArr = System.IO.File.ReadAllBytes(postUrl); + string mimeType = "application/octet-stream"; + return new FileContentResult(byteArr, mimeType) + { + FileDownloadName = fileName + }; + + } } \ No newline at end of file diff --git a/ds-wms-service/DS.WMS.Test/SaasTest.cs b/ds-wms-service/DS.WMS.Test/SaasTest.cs index 38f3757b..42317ca3 100644 --- a/ds-wms-service/DS.WMS.Test/SaasTest.cs +++ b/ds-wms-service/DS.WMS.Test/SaasTest.cs @@ -2,6 +2,7 @@ using System.Reflection; using DS.Module.Core; using DS.Module.Core.Extensions; using DS.Module.SqlSugar; +using DS.WMS.Core.Code.Entity; using DS.WMS.Core.Info.Entity; using DS.WMS.Core.Op.Entity; using DS.WMS.Core.Sys.Entity; @@ -63,7 +64,7 @@ public class SaasTest { var tenantDb = saasService.GetBizDbScopeById("1750335377144680448"); StaticConfig.CodeFirst_MySqlCollate = "utf8mb4_0900_ai_ci";//较高版本支持 - tenantDb.CodeFirst.InitTables(typeof(BusinessOrderContact)); + tenantDb.CodeFirst.InitTables(typeof(CodeFactory)); //db.CodeFirst.InitTables(typeof(OpBusinessTruckCtn)); Assert.True(true); }