|
|
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("附件不存在!"));
|
|
|
}
|
|
|
|
|
|
var limitFiles = AppSetting.app<string>(new string[] { "FileSettings", "FileType" });
|
|
|
var originalFilename = file.FileName; // 文件原始名称
|
|
|
var fileSuffix = Path.GetExtension(file.FileName).ToLower(); // 文件后缀
|
|
|
if (!limitFiles.Contains(fileSuffix))
|
|
|
{
|
|
|
return await Task.FromResult(DataResult<string>.Failed("不允许的文件类型!"));
|
|
|
}
|
|
|
var basePath = AppSetting.app(new string[] { "FileSettings", "BasePath" });
|
|
|
var relativePath = AppSetting.app(new string[] { "FileSettings", "RelativePath" });
|
|
|
var dirAbs = string.Empty;
|
|
|
var fileRelaPath = string.Empty;
|
|
|
var fileAbsPath = string.Empty;
|
|
|
if (string.IsNullOrEmpty(basePath))
|
|
|
{
|
|
|
dirAbs = Path.Combine(_environment.WebRootPath, relativePath);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
dirAbs = Path.Combine(basePath, relativePath);
|
|
|
}
|
|
|
|
|
|
if (!Directory.Exists(dirAbs))
|
|
|
Directory.CreateDirectory(dirAbs);
|
|
|
|
|
|
|
|
|
// 先存库获取Id
|
|
|
var id = SnowFlakeSingle.Instance.NextId();
|
|
|
var fileSaveName = $"{id}{fileSuffix}".ToLower();
|
|
|
fileRelaPath = Path.Combine(relativePath, fileSaveName).ToLower();
|
|
|
fileAbsPath = Path.Combine(dirAbs, fileSaveName).ToLower();
|
|
|
var newFile = new OpFile
|
|
|
{
|
|
|
Id = id,
|
|
|
//FileName = originalFilename,
|
|
|
FilePath = fileSaveName,
|
|
|
TypeCode = req.TypeCode,
|
|
|
TypeName = req.TypeName,
|
|
|
LinkId = req.LinkId,
|
|
|
};
|
|
|
//await tenantDb.Insertable(newFile).ExecuteCommandAsync();
|
|
|
|
|
|
using (var stream = File.Create(fileAbsPath))
|
|
|
{
|
|
|
await file.CopyToAsync(stream);
|
|
|
}
|
|
|
|
|
|
var config = await db.Queryable<SysConfig>().Filter(null,true).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
|
|
|
}
|
|
|
}
|