From 960b33fa24c47a330e51a2c24859d681ae62caeb Mon Sep 17 00:00:00 2001
From: huangxiaoqiang <huangxiaoqiang@hnkhzn.com>
Date: 星期四, 16 四月 2026 10:09:49 +0800
Subject: [PATCH] 重构任务与库存模型,增强日志管理与区域接口
---
Code Management/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/System/Sys_LogController.cs | 283 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 280 insertions(+), 3 deletions(-)
diff --git a/Code Management/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/System/Sys_LogController.cs b/Code Management/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/System/Sys_LogController.cs
index 0e24549..aabc587 100644
--- a/Code Management/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/System/Sys_LogController.cs
+++ b/Code Management/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/System/Sys_LogController.cs
@@ -1,23 +1,26 @@
锘縰sing Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
+using System.Text;
using WIDESEAWCS_Core;
using WIDESEAWCS_ISystemServices;
using WIDESEAWCS_Model.Models;
-namespace WIDESEAWCS_Server.Controllers.System
+namespace WIDESEAWMSServer.Controllers
{
[Route("api/Sys_Log")]
[ApiController]
public class Sys_LogController : ApiBaseController<ISys_LogService, Sys_Log>
{
+ private const int MAX_FILE_SIZE_MB = 50;
+ private const int MAX_RETRY_COUNT = 3;
+ private const int RETRY_DELAY_MS = 100;
+ private static readonly string[] ALLOWED_FILE_TYPES = { ".txt", ".log", ".csv", ".json", ".xml" };
private readonly IHttpContextAccessor _httpContextAccessor;
public Sys_LogController(ISys_LogService service, IHttpContextAccessor httpContextAccessor) : base(service)
{
_httpContextAccessor = httpContextAccessor;
}
-
-
[HttpPost, Route("GetLogList"), AllowAnonymous]
public WebResponseContent GetLogList()
@@ -30,6 +33,280 @@
{
return Service.GetLogData(parm);
}
+
+ [HttpPost, Route("GetLogName"), AllowAnonymous]
+ public WebResponseContent GetLogName()
+ {
+ return Service.GetLogName();
+ }
+
+ [HttpPost, Route("GetLog"), AllowAnonymous]
+ public WebResponseContent GetLog(string fileName)
+ {
+ return Service.GetLog(fileName);
+ }
+
+ [HttpPost, HttpGet, Route("DownLoadLog"), AllowAnonymous]
+ public virtual async Task<ActionResult> DownLoadLog(string fileName)
+ {
+ try
+ {
+ // 1. 鍙傛暟楠岃瘉
+ if (string.IsNullOrWhiteSpace(fileName))
+ {
+ return BadRequest("鏂囦欢鍚嶄笉鑳戒负绌�");
+ }
+
+ // 瀹夊叏鎬ф鏌ワ細闃叉璺緞閬嶅巻鏀诲嚮
+ if (fileName.Contains("..") || Path.IsPathRooted(fileName))
+ {
+ return BadRequest("鏃犳晥鐨勬枃浠跺悕");
+ }
+
+ //string logDirectory = Path.Combine(AppContext.BaseDirectory, "logs");
+ string logDirectory = Path.Combine(AppContext.BaseDirectory);
+
+ if (!Directory.Exists(logDirectory))
+ {
+ Directory.CreateDirectory(logDirectory);
+ }
+
+ string filePath = Path.Combine(logDirectory, fileName);
+
+ if (Directory.Exists(filePath))
+ {
+ return NotFound($"鏂囦欢 {fileName} 涓嶅瓨鍦�");
+ }
+
+ string extension = Path.GetExtension(fileName).ToLowerInvariant();
+ if (!IsAllowedFileType(extension))
+ {
+ return BadRequest($"涓嶆敮鎸佺殑鏂囦欢绫诲瀷: {extension}");
+ }
+
+ FileInfo fileInfo = new FileInfo(filePath);
+
+ if (fileInfo.Length > MAX_FILE_SIZE_MB * 1024 * 1024)
+ {
+ return BadRequest($"鏂囦欢杩囧ぇ锛岃秴杩噞MAX_FILE_SIZE_MB}MB闄愬埗");
+ }
+
+ // 鏂规1锛氫娇鐢ㄩ噸璇曟満鍒� + 鍏变韩璇诲彇锛堟帹鑽愶級
+ byte[] fileBytes = await ReadFileWithRetryAsync(filePath);
+
+ if (fileBytes == null)
+ {
+ return StatusCode(500, "鏂囦欢琚崰鐢紝鏃犳硶涓嬭浇锛岃绋嶅悗閲嶈瘯");
+ }
+
+ string contentType = GetContentType(extension);
+
+ // 璁剧疆涓嬭浇澶�
+ Response.Headers.Add("Content-Disposition", $"attachment; filename=\"{System.Web.HttpUtility.UrlEncode(fileName, Encoding.UTF8)}\"");
+ Response.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate");
+ Response.Headers.Add("Pragma", "no-cache");
+ Response.Headers.Add("Expires", "0");
+
+ return File(fileBytes, contentType, fileName);
+ }
+ catch (UnauthorizedAccessException)
+ {
+ return StatusCode(403, "娌℃湁璁块棶璇ユ枃浠剁殑鏉冮檺");
+ }
+ catch (PathTooLongException)
+ {
+ return BadRequest("鏂囦欢璺緞杩囬暱");
+ }
+ catch (IOException ex)
+ {
+ if (IsFileLockedException(ex))
+ {
+ return StatusCode(500, "鏂囦欢琚攣瀹氾紝鍙兘姝e湪琚郴缁熷啓鍏ワ紝璇风◢鍚庡啀璇�");
+ }
+ return StatusCode(500, $"鏂囦欢璇诲彇澶辫触: {ex.Message}");
+ }
+ catch (Exception ex)
+ {
+ // 璁板綍寮傚父鏃ュ織锛堣繖閲岀畝鍖栦负杩斿洖锛屽疄闄呴」鐩腑搴旇璁板綍鍒版棩蹇楃郴缁燂級
+ return StatusCode(500, $"鏈嶅姟鍣ㄥ唴閮ㄩ敊璇�: {ex.Message}");
+ }
+ }
+
+ /// <summary>
+ /// 甯﹂噸璇曟満鍒剁殑鏂囦欢璇诲彇鏂规硶
+ /// </summary>
+ private async Task<byte[]> ReadFileWithRetryAsync(string filePath)
+ {
+ for (int attempt = 0; attempt < MAX_RETRY_COUNT; attempt++)
+ {
+ try
+ {
+ // 浣跨敤 FileShare.ReadWrite 鍏佽鍏朵粬杩涚▼鍚屾椂璇诲彇鍜屽啓鍏�
+ // 浣跨敤寮傛璇诲彇鎻愰珮鎬ц兘
+ using (var fileStream = new FileStream(
+ filePath,
+ FileMode.Open,
+ FileAccess.Read,
+ FileShare.ReadWrite | FileShare.Delete, // 鍏佽鍒犻櫎鎿嶄綔
+ bufferSize: 4096,
+ useAsync: true))
+ {
+ using (var memoryStream = new MemoryStream())
+ {
+ await fileStream.CopyToAsync(memoryStream);
+ return memoryStream.ToArray();
+ }
+ }
+ }
+ catch (IOException) when (attempt < MAX_RETRY_COUNT - 1)
+ {
+ // 濡傛灉涓嶆槸鏈�鍚庝竴娆¢噸璇曪紝绛夊緟涓�娈垫椂闂�
+ await Task.Delay(RETRY_DELAY_MS * (attempt + 1));
+ }
+ catch (IOException ex)
+ {
+ // 鏈�鍚庝竴娆″皾璇曚篃澶辫触浜�
+ throw;
+ }
+ }
+ return null;
+ }
+
+ /// <summary>
+ /// 鍒ゆ柇鏄惁涓烘枃浠惰閿佸畾鐨勫紓甯�
+ /// </summary>
+ private bool IsFileLockedException(IOException ex)
+ {
+ int errorCode = ex.HResult & 0xFFFF;
+ return errorCode == 32 || errorCode == 33; // ERROR_SHARING_VIOLATION or ERROR_LOCK_VIOLATION
+ }
+
+ /// <summary>
+ /// 妫�鏌ユ枃浠剁被鍨嬫槸鍚﹀厑璁�
+ /// </summary>
+ private bool IsAllowedFileType(string extension)
+ {
+ return ALLOWED_FILE_TYPES.Contains(extension);
+ }
+
+ /// <summary>
+ /// 鑾峰彇Content-Type
+ /// </summary>
+ private string GetContentType(string extension)
+ {
+ return extension.ToLowerInvariant() switch
+ {
+ ".txt" => "text/plain; charset=utf-8",
+ ".log" => "text/plain; charset=utf-8",
+ ".csv" => "text/csv; charset=utf-8",
+ ".json" => "application/json; charset=utf-8",
+ ".xml" => "application/xml; charset=utf-8",
+ _ => "application/octet-stream"
+ };
+ }
+
+ /// <summary>
+ /// 澶囬�夋柟妗堬細鍒涘缓涓存椂鍓湰涓嬭浇锛堟渶瀹夊叏锛屼絾鎬ц兘绋嶅樊锛�
+ /// </summary>
+ [HttpPost, HttpGet, Route("DownLoadLogCopy"), AllowAnonymous]
+ public virtual async Task<ActionResult> DownLoadLogCopy(string fileName)
+ {
+ try
+ {
+ // 鍙傛暟楠岃瘉锛堝悓涓婏級
+ if (string.IsNullOrWhiteSpace(fileName))
+ {
+ return BadRequest("鏂囦欢鍚嶄笉鑳戒负绌�");
+ }
+
+ string logDirectory = Path.Combine(AppContext.BaseDirectory, "logs");
+ string filePath = Path.Combine(logDirectory, fileName);
+
+ if (Directory.Exists(filePath))
+ {
+ return NotFound($"鏂囦欢 {fileName} 涓嶅瓨鍦�");
+ }
+
+ // 鐢熸垚涓存椂鏂囦欢鍚�
+ string tempFileName = $"{Path.GetFileNameWithoutExtension(fileName)}_{Guid.NewGuid():N}{Path.GetExtension(fileName)}";
+ string tempFilePath = Path.Combine(Path.GetTempPath(), tempFileName);
+
+ try
+ {
+ // 灏濊瘯澶嶅埗鏂囦欢鍒颁复鏃朵綅缃紙浣跨敤閲嶈瘯鏈哄埗锛�
+ bool copySuccess = false;
+ for (int attempt = 0; attempt < MAX_RETRY_COUNT; attempt++)
+ {
+ try
+ {
+ //Directory.GetFiles.Copy(filePath, tempFilePath, false);
+ copySuccess = true;
+ break;
+ }
+ catch (IOException) when (attempt < MAX_RETRY_COUNT - 1)
+ {
+ await Task.Delay(RETRY_DELAY_MS * (attempt + 1));
+ }
+ }
+
+ if (!copySuccess)
+ {
+ return StatusCode(500, "鏃犳硶澶嶅埗鏂囦欢锛屽彲鑳借鍏朵粬杩涚▼鍗犵敤");
+ }
+
+ // 浠庝复鏃舵枃浠惰鍙�
+ byte[] fileBytes;
+ using (FileStream tempStream = new FileStream(tempFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
+ {
+ using (MemoryStream memoryStream = new MemoryStream())
+ {
+ await tempStream.CopyToAsync(memoryStream);
+ fileBytes = memoryStream.ToArray();
+ }
+ }
+
+ string extension = Path.GetExtension(fileName).ToLowerInvariant();
+ string contentType = GetContentType(extension);
+
+ // 杩斿洖鏂囦欢鍚庢竻鐞嗕复鏃舵枃浠�
+ var result = File(fileBytes, contentType, fileName);
+
+ // 寮傛娓呯悊涓存椂鏂囦欢
+ _ = Task.Run(() =>
+ {
+ try
+ {
+ Directory.Delete(tempFilePath);
+ }
+ catch
+ {
+ // 蹇界暐鍒犻櫎澶辫触
+ }
+ });
+
+ return result;
+ }
+ finally
+ {
+ // 纭繚涓存椂鏂囦欢琚竻鐞�
+ if (Directory.Exists(tempFilePath))
+ {
+ try
+ {
+ Directory.Delete(tempFilePath);
+ }
+ catch
+ {
+ // 蹇界暐鍒犻櫎澶辫触
+ }
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ return StatusCode(500, $"鏈嶅姟鍣ㄥ唴閮ㄩ敊璇�: {ex.Message}");
+ }
+ }
}
}
--
Gitblit v1.9.3