海运出口ocr识别接口

usertest
ZR20090193-陈敬勇 5 months ago
parent 1929b0d9b1
commit 23ab3a1c9a

@ -47,6 +47,7 @@ public class MultiLanguageMiddleware
|| context.Request.Path.Value.IndexOf("HealthCheck", StringComparison.InvariantCultureIgnoreCase) > -1 || context.Request.Path.Value.IndexOf("HealthCheck", StringComparison.InvariantCultureIgnoreCase) > -1
|| context.Request.Path.Value.IndexOf("DownloadOpFileInfo", 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("LinkAttach", StringComparison.InvariantCultureIgnoreCase) > -1
|| context.Request.Path.Value.IndexOf("GetOcrImg", StringComparison.InvariantCultureIgnoreCase) > -1
) )
) )
{ {

@ -58,5 +58,12 @@ namespace DS.WMS.Core.Op.Interface
/// <param name="tenantDb"></param> /// <param name="tenantDb"></param>
/// <returns></returns> /// <returns></returns>
public string GetPortCode(long Id, SqlSugarScopeProvider tenantDb); public string GetPortCode(long Id, SqlSugarScopeProvider tenantDb);
/// <summary>
/// 获取系统配置信息
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public Task<DataResult<string>> GetConfigData(string code);
} }
} }

@ -2,6 +2,7 @@ using DS.Module.Core;
using DS.Module.Core.Data; using DS.Module.Core.Data;
using DS.WMS.Core.Op.Dtos; using DS.WMS.Core.Op.Dtos;
using DS.WMS.Core.Op.EDI; using DS.WMS.Core.Op.EDI;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace DS.WMS.Core.Op.Interface; namespace DS.WMS.Core.Op.Interface;
@ -151,4 +152,12 @@ public interface ISeaExportService
/// <param name="req">订舱主键数组</param> /// <param name="req">订舱主键数组</param>
/// <returns>每单的处理提交结果</returns> /// <returns>每单的处理提交结果</returns>
public Task<DataResult<List<SubmitTelexResultDto>>> SubmitTelex(IdModel req); public Task<DataResult<List<SubmitTelexResultDto>>> SubmitTelex(IdModel req);
/// <summary>
/// 上传OCR附件
/// </summary>
/// <param name="file"></param>
/// <param name="req"></param>
/// <returns></returns>
public Task<DataResult<string>> UploadOcrFile(IFormFile file, [FromForm] OpFileReq req);
} }

@ -1806,6 +1806,23 @@ namespace DS.WMS.Core.Op.Method
} }
} }
#endregion #endregion
/// <summary>
///
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public async Task<DataResult<string>> GetConfigData(string code)
{
var config = await db.Queryable<SysConfig>().Where(x => x.Code == code && x.Status == StatusEnum.Enable).FirstAsync();
if (config.IsNull())
{
return await Task.FromResult(DataResult<string>.Failed("OCR接口地址未配置!"));
}
return await Task.FromResult(DataResult<string>.Success(config.Value));
}
} }
} }

@ -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
{
/// <summary>
/// 海运出口Ocr相关接口
/// </summary>
public partial class SeaExportService
{
#region 调用大简云OCR接口
/// <summary>
/// 上传OCR附件
/// </summary>
/// <param name="file"></param>
/// <param name="req"></param>
/// <returns></returns>
public async Task<DataResult<string>> UploadOcrFile(IFormFile file, [FromForm] OpFileReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
//未上传文件
if (file == null || file.Length == 0)
{
return await Task.FromResult(DataResult<string>.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<SysConfig>().Where(x => x.Code == "ocr_api_url" && x.Status == StatusEnum.Enable).FirstAsync();
if (config.IsNull())
{
return await Task.FromResult(DataResult<string>.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<string>.Success(newFile.FileName));
}
else
{
return await Task.FromResult(DataResult<string>.Failed(jobj.GetStringValue("message")));
}
}
else
{
return await Task.FromResult(DataResult<string>.Failed("请求大简云OCR接口错误请联系管理员!"));
}
}
#endregion
}
}

@ -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.Data;
using DS.Module.Core.Helpers;
using DS.WMS.Core.Info.Dtos; using DS.WMS.Core.Info.Dtos;
using DS.WMS.Core.Info.Interface; using DS.WMS.Core.Info.Interface;
using DS.WMS.Core.Op.Dtos; 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.Dtos;
using DS.WMS.Core.Sys.Interface; using DS.WMS.Core.Sys.Interface;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NPOI.XWPF.UserModel;
using Org.BouncyCastle.Ocsp; using Org.BouncyCastle.Ocsp;
namespace DS.WMS.OpApi.Controllers; namespace DS.WMS.OpApi.Controllers;
@ -18,14 +22,15 @@ namespace DS.WMS.OpApi.Controllers;
public class SeaExportController : ApiController public class SeaExportController : ApiController
{ {
private readonly ISeaExportService _invokeService; private readonly ISeaExportService _invokeService;
private readonly ISeaExportCommonService _commonService;
/// <summary> /// <summary>
/// 构造函数 /// 构造函数
/// </summary> /// </summary>
/// <param name="invokeService"></param> /// <param name="invokeService"></param>
public SeaExportController(ISeaExportService invokeService) public SeaExportController(ISeaExportService invokeService, ISeaExportCommonService commonService)
{ {
_invokeService = invokeService; _invokeService = invokeService;
_commonService = commonService;
} }
/// <summary> /// <summary>
@ -274,4 +279,84 @@ public class SeaExportController : ApiController
return res; return res;
} }
/// <summary>
/// 上传OCR附件
/// </summary>
/// <param name="file">文件信息</param>
/// <param name="req">业务附件请求实体 TypeCode = "other",TypeName = "其他"</param>
/// <returns></returns>
[HttpPost]
[Route("UploadOcrFile")]
public async Task<DataResult<string>> UploadOcrFile(IFormFile file, [FromForm] OpFileReq req)
{
var res = _invokeService.UploadOcrFile(file, req);
return await res;
}
/// <summary>
/// 获取文字
/// </summary>
/// <param name="fileName">文件名称</param>
/// <param name="scale">缩放比例</param>
/// <param name="x">x坐标</param>
/// <param name="y">y坐标</param>
/// <param name="w">宽度</param>
/// <param name="h">高度</param>
/// <returns></returns>
[HttpGet("GetOcrText")]
public async Task<DataResult<string>> 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<string>.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<string>.Success(str)); ;
}
/// <summary>
/// 获取图片
/// </summary>
/// <param name="fileName">文件名称</param>
/// <param name="scale">缩放比例默认为1.5</param>
/// <returns></returns>
[HttpGet]
[Route("GetOcrImg")]
[ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)]
public async Task<IActionResult> GetOcrImg([FromQuery] string fileName, float scale = 1.5f)
{
var config = await _commonService.GetConfigData("ocr_api_url");
//if (!config.Succeeded)
//{
// return await Task.FromResult(DataResult<string>.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
};
}
} }

@ -2,6 +2,7 @@ using System.Reflection;
using DS.Module.Core; using DS.Module.Core;
using DS.Module.Core.Extensions; using DS.Module.Core.Extensions;
using DS.Module.SqlSugar; using DS.Module.SqlSugar;
using DS.WMS.Core.Code.Entity;
using DS.WMS.Core.Info.Entity; using DS.WMS.Core.Info.Entity;
using DS.WMS.Core.Op.Entity; using DS.WMS.Core.Op.Entity;
using DS.WMS.Core.Sys.Entity; using DS.WMS.Core.Sys.Entity;
@ -63,7 +64,7 @@ public class SaasTest
{ {
var tenantDb = saasService.GetBizDbScopeById("1750335377144680448"); var tenantDb = saasService.GetBizDbScopeById("1750335377144680448");
StaticConfig.CodeFirst_MySqlCollate = "utf8mb4_0900_ai_ci";//较高版本支持 StaticConfig.CodeFirst_MySqlCollate = "utf8mb4_0900_ai_ci";//较高版本支持
tenantDb.CodeFirst.InitTables(typeof(BusinessOrderContact)); tenantDb.CodeFirst.InitTables(typeof(CodeFactory));
//db.CodeFirst.InitTables(typeof(OpBusinessTruckCtn)); //db.CodeFirst.InitTables(typeof(OpBusinessTruckCtn));
Assert.True(true); Assert.True(true);
} }

Loading…
Cancel
Save