| | |
| | | using Azure.Core; |
| | | using Furion; |
| | | using Furion.DatabaseAccessor; |
| | | using Furion.DataEncryption.Extensions; |
| | | using Furion.DependencyInjection; |
| | | using Furion.DistributedIDGenerator; |
| | | using Furion.FriendlyException; |
| | | using Furion.HttpRemote; |
| | | using Microsoft.Extensions.Options; |
| | | using NetTopologySuite.Algorithm; |
| | | using Org.BouncyCastle.Asn1.Ocsp; |
| | |
| | | namespace ApiTools.Core |
| | | { |
| | | public class NongYePayUtils( |
| | | IHttpRemoteService httpRemoteService, |
| | | IOptions<NongYePayOptions> options, |
| | | IRepository<ThreeResourceLog, LogDbContextLocator> repThreeResourceLog |
| | | ) : ITransient |
| | | { |
| | | private readonly IHttpRemoteService httpRemoteService = httpRemoteService; |
| | | private readonly IOptions<NongYePayOptions> options = options; |
| | | private readonly IRepository<ThreeResourceLog, LogDbContextLocator> repThreeResourceLog = repThreeResourceLog; |
| | | |
| | |
| | | /// <returns></returns> |
| | | public async Task<NongYePayDownloadEreceiptResponse> DownloadEreceipt(NongYePayDownloadEreceiptRequest request) |
| | | { |
| | | var response = await Send<NongYePayDownloadEreceiptRequest, NongYePayDownloadEreceiptResponse>(request); |
| | | if (response != null |
| | | && response.RespSource == "0" |
| | | && response.Cmp != null |
| | | && response.Cmp.BatchFileName.IsNotNull()) |
| | | { |
| | | response.ZipFileName = $"{options.Value.FilePath}{response.Cmp.BatchFileName}"; |
| | | if (File.Exists(response.ZipFileName)) |
| | | { |
| | | using (var archive = ZipFile.OpenRead(response.ZipFileName)) |
| | | { |
| | | foreach (var entry in archive.Entries) |
| | | { |
| | | if (entry.FullName.EndsWith(".pdf")) |
| | | { |
| | | using (var stream = entry.Open()) |
| | | { |
| | | var ms = new MemoryStream(); |
| | | stream.CopyTo(ms); |
| | | ms.Position = 0; |
| | | response.Items.Add(new NongYePayDownloadEreceiptResponseItem |
| | | { |
| | | FileName = entry.FullName, |
| | | Stream = ms |
| | | }); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | return response; |
| | | return await SendWithZip<NongYePayDownloadEreceiptRequest, NongYePayDownloadEreceiptResponse>(request); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 实时下载电子回单 |
| | | /// </summary> |
| | | /// <param name="request"></param> |
| | | /// <returns></returns> |
| | | public async Task<NongYePayRealTimeDownloadEreceiptResponse> RealTimeDownloadEreceipt(NongYePayRealTimeDownloadEreceiptRequest request) |
| | | { |
| | | var response = await Send<NongYePayRealTimeDownloadEreceiptRequest, NongYePayRealTimeDownloadEreceiptResponse>(request); |
| | | if (response != null |
| | | && response.RespSource == "0" |
| | | && response.Cmp != null |
| | | && response.Cmp.BatchFileName.IsNotNull()) |
| | | { |
| | | response.ZipFileName = $"{options.Value.FilePath}{response.Cmp.BatchFileName}"; |
| | | if (File.Exists(response.ZipFileName)) |
| | | { |
| | | using (var archive = ZipFile.OpenRead(response.ZipFileName)) |
| | | { |
| | | foreach (var entry in archive.Entries) |
| | | { |
| | | if (entry.FullName.EndsWith(".pdf")) |
| | | { |
| | | using (var stream = entry.Open()) |
| | | { |
| | | var ms = new MemoryStream(); |
| | | stream.CopyTo(ms); |
| | | ms.Position = 0; |
| | | response.Items.Add(new NongYePayDownloadEreceiptResponseItem |
| | | { |
| | | FileName = entry.FullName, |
| | | Stream = ms |
| | | }); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | return response; |
| | | return await SendWithZip<NongYePayRealTimeDownloadEreceiptRequest, NongYePayRealTimeDownloadEreceiptResponse>(request); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | return $"{now:yyyyMMddHHmmssfff}{random}"; |
| | | } |
| | | |
| | | private async Task<List<NongYePayResponseZipFile>> GetZipFiles(NongYePayZipResponse response) |
| | | { |
| | | var list = new List<NongYePayResponseZipFile>(); |
| | | if (response != null |
| | | && response.RespSource == "0" |
| | | && response.Cmp != null |
| | | && response.Cmp.BatchFileName.IsNotNull()) |
| | | { |
| | | var memoryStream = new MemoryStream(); |
| | | if (options.Value.RemoteFileUrl.IsNotNull()) |
| | | { |
| | | response.ZipFileName = response.Cmp.BatchFileName; |
| | | var url = $"{options.Value.RemoteFileUrl}/api/file/download"; |
| | | var command = new DownloadFileCommand |
| | | { |
| | | Scene = "NongYePay", |
| | | Path = response.Cmp.BatchFileName |
| | | }; |
| | | var timestamp = DateTime.Now.ToTimeStamp(true); |
| | | var sign = $"POST|/api/file/download|{command.ToJson()}|{options.Value.RemoteFilePrivateKey}|{timestamp}".ToMD5Encrypt(); |
| | | using var stream = await httpRemoteService.PostAsStreamAsync(url, |
| | | builder => builder |
| | | .SetJsonContent(command) |
| | | .WithHeader("x-timestamp", timestamp, replace: true) |
| | | .WithHeader("x-sign", sign, replace: true)); |
| | | stream.CopyTo(memoryStream); |
| | | memoryStream.Position = 0; |
| | | } |
| | | else if (options.Value.FilePath.IsNotNull()) |
| | | { |
| | | response.ZipFileName = $"{options.Value.FilePath}{response.Cmp.BatchFileName}"; |
| | | if (File.Exists(response.ZipFileName)) |
| | | { |
| | | using var stream = File.OpenRead(response.ZipFileName); |
| | | stream.CopyTo(memoryStream); |
| | | memoryStream.Position = 0; |
| | | } |
| | | } |
| | | |
| | | using (var archive = new ZipArchive(memoryStream)) |
| | | { |
| | | foreach (var entry in archive.Entries) |
| | | { |
| | | if (entry.FullName.EndsWith(".pdf")) |
| | | { |
| | | using (var stream = entry.Open()) |
| | | { |
| | | var ms = new MemoryStream(); |
| | | stream.CopyTo(ms); |
| | | ms.Position = 0; |
| | | list.Add(new NongYePayResponseZipFile |
| | | { |
| | | FileName = entry.FullName, |
| | | Stream = ms |
| | | }); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | private async Task<List<T>> GetList<T>(NongYePayBaseResponse<T> response, ThreeResourceLog log) |
| | | where T : class, new() |
| | | { |
| | |
| | | && response.FileFlag == "1" |
| | | && response.Cmp.BatchFileName.IsNotNull()) |
| | | { |
| | | var fileName = $"{options.Value.FilePath}{response.Cmp.BatchFileName}"; |
| | | if (File.Exists(fileName)) |
| | | var lines = new List<string>(); |
| | | if (options.Value.RemoteFileUrl.IsNotNull()) |
| | | { |
| | | var url = $"{options.Value.RemoteFileUrl}/api/file/download"; |
| | | var command = new DownloadFileCommand |
| | | { |
| | | Scene = "NongYePay", |
| | | Path = response.Cmp.BatchFileName |
| | | }; |
| | | var timestamp = DateTime.Now.ToTimeStamp(true); |
| | | var sign = $"POST|/api/file/download|{command.ToJson()}|{options.Value.RemoteFilePrivateKey}|{timestamp}".ToMD5Encrypt(); |
| | | var buffer = await httpRemoteService.PostAsByteArrayAsync(url, |
| | | builder => builder |
| | | .SetJsonContent(command) |
| | | .WithHeader("x-timestamp", timestamp, replace: true) |
| | | .WithHeader("x-sign", sign, replace: true)); |
| | | var text = Encoding.GetEncoding("GBK").GetString(buffer); |
| | | lines = text.Split("\n").Where(it => it.IsNotNull()).ToList(); |
| | | } |
| | | else if (options.Value.FilePath.IsNotNull()) |
| | | { |
| | | var fileName = $"{options.Value.FilePath}{response.Cmp.BatchFileName}"; |
| | | if (File.Exists(fileName)) |
| | | { |
| | | var array = await File.ReadAllLinesAsync(fileName, Encoding.GetEncoding("GBK")); |
| | | lines = array.ToList(); |
| | | } |
| | | } |
| | | if (lines.IsNotNull()) |
| | | { |
| | | var props = typeof(T).GetProperties(); |
| | | var lines = await File.ReadAllLinesAsync(fileName, Encoding.GetEncoding("GBK")); |
| | | foreach (var line in lines) |
| | | { |
| | | var item = new T(); |
| | |
| | | } |
| | | list.Add(item); |
| | | } |
| | | |
| | | log.UpdatedTime = DateTimeOffset.Now; |
| | | var json = list.ToJson(); |
| | | log.Response += $"\n{json}"; |
| | | await repThreeResourceLog.UpdateNowAsync(log); |
| | | } |
| | | |
| | | log.UpdatedTime = DateTimeOffset.Now; |
| | | var json = list.ToJson(); |
| | | log.Response += $"\n{json}"; |
| | | await repThreeResourceLog.UpdateNowAsync(log); |
| | | } |
| | | return list; |
| | | } |
| | |
| | | where TResponseItem : class, new() |
| | | { |
| | | var response = await SendWithLog<TRequest, TResponse>(request); |
| | | response.response.Items = await GetList<TResponseItem>(response.response, response.log); |
| | | response.response.Items = await GetList(response.response, response.log); |
| | | return response.response; |
| | | } |
| | | |
| | | private async Task<TResponse> SendWithZip<TRequest, TResponse>(TRequest request) |
| | | where TRequest : NongYePayBaseRequest |
| | | where TResponse : NongYePayZipResponse, new() |
| | | { |
| | | var response = await SendWithLog<TRequest, TResponse>(request); |
| | | response.response.Items = await GetZipFiles(response.response); |
| | | return response.response; |
| | | } |
| | | |