From 9ce02d17cafc6b8dab49b16fa20fdca4c978bb5b Mon Sep 17 00:00:00 2001
From: huangxiaoqiang <huangxiaoqiang@hnkhzn.com>
Date: 星期六, 13 十二月 2025 09:33:30 +0800
Subject: [PATCH] 新增文本日志Web端显示

---
 项目代码/WMS无仓储版/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/System/Sys_LogController.cs |  178 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 177 insertions(+), 1 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/System/Sys_LogController.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/System/Sys_LogController.cs"
index 3cd2f46..3a72445 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/System/Sys_LogController.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/System/Sys_LogController.cs"
@@ -1,5 +1,8 @@
-锘縰sing Microsoft.AspNetCore.Http;
+锘縰sing Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
+using System.Text;
+using WIDESEA_Core;
 using WIDESEA_Core.BaseController;
 using WIDESEA_ISystemService;
 using WIDESEA_Model.Models;
@@ -16,5 +19,178 @@
         public Sys_LogController(ISys_LogService service) : base(service)
         {
         }
+        [HttpPost, Route("GetLogName"), AllowAnonymous]
+        public WebResponseContent GetLogName()
+        {
+            WebResponseContent content = new WebResponseContent();
+            try
+            {
+                List<object> data = new List<object>();
+                DirectoryInfo folder = new DirectoryInfo(AppContext.BaseDirectory + "\\logs\\");
+                DirectoryInfo[] firstDirectoryInfos = folder.GetDirectories().OrderByDescending(x => x.CreationTime).ToArray();
+                int k = 2020;
+                for (int i = 0; i < firstDirectoryInfos.Length; i++)
+                {
+                    if (firstDirectoryInfos[i].Name != "Info")
+                    {
+                        FileInfo[] nextFileInfos = firstDirectoryInfos[i].GetFiles();
+                        List<object> values = new List<object>();
+                        for (int j = 0; j < nextFileInfos.Length; j++)
+                        {
+                            values.Add(new { label = nextFileInfos[j].Name, id = k, hidden = true, fatherNode = firstDirectoryInfos[i].Name });
+                            k++;
+                        }
+                        data.Add(new { label = firstDirectoryInfos[i].Name, children = values, id = i, hidden = false });
+                    }
+                }
+
+                FileInfo[] nextFileInfo = folder.GetFiles();
+                List<object> value = new List<object>();
+                for (int j = 0; j < nextFileInfo.Length; j++)
+                {
+                    value.Add(new { label = nextFileInfo[j].Name, id = k, hidden = true, fatherNode = folder.Name });
+                    k++;
+                }
+                data.Add(new { label = folder.Name, children = value, id = 1, hidden = false });
+
+                return WebResponseContent.Instance.OK(data: data);
+            }
+            catch (Exception ex)
+            {
+                return WebResponseContent.Instance.Error(ex.Message);
+            }
+        }
+
+        [HttpPost, Route("GetLog"), AllowAnonymous]
+        public WebResponseContent GetLog(string fileName)
+        {
+            WebResponseContent content = new WebResponseContent();
+            try
+            {
+                List<FileInfo> files = new List<FileInfo>();
+                DirectoryInfo folder = new DirectoryInfo(AppContext.BaseDirectory + "\\logs\\");
+                DirectoryInfo[] firstDirectoryInfos = folder.GetDirectories();
+                for (int i = 0; i < firstDirectoryInfos.Length; i++)
+                {
+                    FileInfo[] nextFileInfos = firstDirectoryInfos[i].GetFiles();
+                    files.AddRange(nextFileInfos);
+                }
+
+                FileInfo[] nextFileInfo = folder.GetFiles();
+                files.AddRange(nextFileInfo);
+                if (files.Count > 0)
+                {
+                    FileInfo file = files.Where(x => x.Name == fileName).FirstOrDefault();
+                    using StreamReader stream = new StreamReader(file.FullName);
+                    StringBuilder text = new StringBuilder();
+                    List<string> lines = new List<string>();
+                    int i = 0;
+                    while (stream.Peek() >= 0)
+                    {
+                        var line = stream.ReadLine();
+                        lines.Add(line);
+                    }
+
+                    content = WebResponseContent.Instance.OK(data: lines);
+                }
+                else
+                {
+                    content = WebResponseContent.Instance.Error($"鏈壘鍒版棩蹇楁枃浠�,銆恵fileName}銆�");
+                }
+            }
+            catch (Exception ex)
+            {
+                content = WebResponseContent.Instance.Error($"鎵撳紑鏃ュ織鏂囦欢閿欒,{ex.Message}");
+            }
+            return content;
+        }
+        [HttpPost, HttpGet, Route("DownLoadLog"), AllowAnonymous]
+        public virtual ActionResult DownLoadLog(string fileName)
+        {
+            try
+            {
+                // 1. 鍙傛暟楠岃瘉
+                if (string.IsNullOrWhiteSpace(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 > 50 * 1024 * 1024) // 50MB闄愬埗
+                {
+                    return BadRequest("鏂囦欢杩囧ぇ锛屾棤娉曚笅杞�");
+                }
+
+                byte[] fileBytes;
+                using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
+                {
+                    using (MemoryStream memoryStream = new MemoryStream())
+                    {
+                        fileStream.CopyTo(memoryStream);
+                        fileBytes = memoryStream.ToArray();
+                    }
+                }
+
+                string contentType = GetContentType(extension);
+
+                return File(fileBytes, contentType, fileName);
+            }
+            catch (UnauthorizedAccessException)
+            {
+                return StatusCode(403, "娌℃湁璁块棶璇ユ枃浠剁殑鏉冮檺");
+            }
+            catch (PathTooLongException)
+            {
+                return BadRequest("鏂囦欢璺緞杩囬暱");
+            }
+            catch (IOException ex)
+            {
+                return StatusCode(500, "鏂囦欢璇诲彇澶辫触锛屽彲鑳芥鍦ㄨ鍏朵粬杩涚▼浣跨敤");
+            }
+            catch (Exception ex)
+            {
+                return StatusCode(500, $"鏈嶅姟鍣ㄥ唴閮ㄩ敊璇�: {ex.Message}");
+            }
+        }
+        private bool IsAllowedFileType(string extension)
+        {
+            var allowedTypes = new[] { ".txt", ".log", ".csv", ".json", ".xml" };
+            return allowedTypes.Contains(extension);
+        }
+
+        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"
+            };
+        }
     }
 }

--
Gitblit v1.9.3