diff --git a/client/Reader/MainForm.Designer.cs b/client/Reader/MainForm.Designer.cs index 6008009..a90182d 100644 --- a/client/Reader/MainForm.Designer.cs +++ b/client/Reader/MainForm.Designer.cs @@ -17,13 +17,12 @@ namespace Reader txtFolder = new TextBox(); btnBrowse = new Button(); btnRead = new Button(); - dgv = new DataGridView(); - Filename = new DataGridViewTextBoxColumn(); - eryi_ID = new DataGridViewTextBoxColumn(); - 下载者用户名 = new DataGridViewTextBoxColumn(); - 暗码写入时间 = new DataGridViewTextBoxColumn(); - ((System.ComponentModel.ISupportInitialize)dgv).BeginInit(); - SuspendLayout(); + dgv = new DataGridView(); + Filename = new DataGridViewTextBoxColumn(); + eryi_ID = new DataGridViewTextBoxColumn(); + 下载者用户名 = new DataGridViewTextBoxColumn(); + ((System.ComponentModel.ISupportInitialize)dgv).BeginInit(); + SuspendLayout(); // // txtFolder // @@ -55,15 +54,16 @@ namespace Reader // dgv // dgv.AllowUserToAddRows = false; - dgv.AllowUserToDeleteRows = false; - dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; - dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - dgv.Columns.AddRange(new DataGridViewColumn[] { Filename, eryi_ID, 下载者用户名, 暗码写入时间 }); - dgv.Location = new Point(12, 59); - dgv.Name = "dgv"; - dgv.ReadOnly = true; - dgv.Size = new Size(748, 332); - dgv.TabIndex = 2; + dgv.AllowUserToDeleteRows = false; + dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; + dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dgv.Columns.AddRange(new DataGridViewColumn[] { Filename, eryi_ID, 下载者用户名 }); + dgv.Location = new Point(12, 59); + dgv.Name = "dgv"; + dgv.ReadOnly = true; + dgv.Size = new Size(748, 332); + dgv.TabIndex = 2; + dgv.CellClick += dgv_CellClick; // // Filename // @@ -87,22 +87,13 @@ namespace Reader // 下载者用户名 // 下载者用户名.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader; - 下载者用户名.HeaderText = "下载者用户名"; - 下载者用户名.Name = "下载者用户名"; - 下载者用户名.ReadOnly = true; - 下载者用户名.Width = 105; - // - // 暗码写入时间 - // - 暗码写入时间.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader; - 暗码写入时间.FillWeight = 300F; - 暗码写入时间.HeaderText = "暗码写入时间"; - 暗码写入时间.Name = "暗码写入时间"; - 暗码写入时间.ReadOnly = true; - 暗码写入时间.Width = 105; - // - // MainForm - // + 下载者用户名.HeaderText = "下载者用户名"; + 下载者用户名.Name = "下载者用户名"; + 下载者用户名.ReadOnly = true; + 下载者用户名.Width = 105; + // + // MainForm + // ClientSize = new Size(791, 419); Controls.Add(txtFolder); Controls.Add(btnBrowse); @@ -121,10 +112,9 @@ namespace Reader private System.Windows.Forms.TextBox txtFolder; private System.Windows.Forms.Button btnBrowse; private System.Windows.Forms.Button btnRead; - private System.Windows.Forms.DataGridView dgv; - private DataGridViewTextBoxColumn Filename; - private DataGridViewTextBoxColumn eryi_ID; - private DataGridViewTextBoxColumn 下载者用户名; - private DataGridViewTextBoxColumn 暗码写入时间; + private System.Windows.Forms.DataGridView dgv; + private DataGridViewTextBoxColumn Filename; + private DataGridViewTextBoxColumn eryi_ID; + private DataGridViewTextBoxColumn 下载者用户名; } } diff --git a/client/Reader/MainForm.cs b/client/Reader/MainForm.cs index 88f959d..8dd7eb9 100644 --- a/client/Reader/MainForm.cs +++ b/client/Reader/MainForm.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Net.Http; @@ -12,16 +14,20 @@ namespace Reader { public partial class MainForm : Form { - private static readonly HttpClient http = new HttpClient(); - private static readonly string ServerUrl; - - static MainForm() - { - var configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json"); - var jsonContent = System.IO.File.ReadAllText(configPath); - var start = jsonContent.IndexOf('"', jsonContent.IndexOf("ServerUrl") + 10); - var end = jsonContent.IndexOf('"', start + 1); - ServerUrl = jsonContent.Substring(start + 1, end - start - 1).Trim(); + private static readonly HttpClient http = new HttpClient(); + private static readonly string ServerUrl; + private static readonly HashSet DocumentExtensions = new(StringComparer.OrdinalIgnoreCase) + { + ".doc", ".docx", ".pdf", ".ppt", ".pptx" + }; + + static MainForm() + { + var configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json"); + var jsonContent = System.IO.File.ReadAllText(configPath); + var start = jsonContent.IndexOf('"', jsonContent.IndexOf("ServerUrl") + 10); + var end = jsonContent.IndexOf('"', start + 1); + ServerUrl = jsonContent.Substring(start + 1, end - start - 1).Trim(); } public MainForm() @@ -47,25 +53,29 @@ namespace Reader return; } dgv.Rows.Clear(); - var files = Directory.GetFiles(folder, "*", SearchOption.AllDirectories); - var shaList = new System.Collections.Generic.List(); - var fileMap = new System.Collections.Generic.Dictionary(); + var files = Directory + .GetFiles(folder, "*", SearchOption.AllDirectories) + .Where(IsSupportedDocument) + .OrderBy(Path.GetFileName, StringComparer.CurrentCultureIgnoreCase) + .ToList(); + var fileEntries = new List<(string FilePath, string Sha)>(); + var shaList = new List(); foreach (var f in files) { try { var sha = await Task.Run(() => ComputeSha256(f)); + fileEntries.Add((f, sha)); shaList.Add(sha); - fileMap[sha] = f; } catch { } } - if (shaList.Count == 0) + if (fileEntries.Count == 0) { - MessageBox.Show("没有找到文件。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); + MessageBox.Show("没有找到支持的文档文件。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } - var payload = new { shas = shaList }; + var payload = new { shas = shaList.Distinct().ToList() }; var content = new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json"); var resp = await http.PostAsync(ServerUrl + "/read", content); if (!resp.IsSuccessStatusCode) @@ -77,23 +87,26 @@ namespace Reader using var doc = JsonDocument.Parse(text); var root = doc.RootElement; var data = root.GetProperty("data"); - foreach (var sha in shaList) + foreach (var entry in fileEntries) { - var fileName = Path.GetFileName(fileMap[sha]); - if (data.TryGetProperty(sha, out var item) && item.ValueKind != JsonValueKind.Null) + var fileName = Path.GetFileName(entry.FilePath); + string up = "无"; + string down = "无"; + if (data.TryGetProperty(entry.Sha, out var item) && item.ValueKind != JsonValueKind.Null) { - var up = item.GetProperty("upload_code").GetString() ?? ""; - var down = item.GetProperty("download_code").GetString() ?? ""; - var created = item.GetProperty("created_at").GetString() ?? ""; - dgv.Rows.Add(fileName, up, down, created); - } - else - { - dgv.Rows.Add(fileName, "无", "无", "无"); + up = item.GetProperty("upload_code").GetString() ?? ""; + down = item.GetProperty("download_code").GetString() ?? ""; } + var rowIndex = dgv.Rows.Add(fileName, up, down); + dgv.Rows[rowIndex].Tag = entry.FilePath; } } + private static bool IsSupportedDocument(string filePath) + { + return DocumentExtensions.Contains(Path.GetExtension(filePath)); + } + private static string ComputeSha256(string filePath) { using var sha = SHA256.Create(); @@ -104,32 +117,62 @@ namespace Reader return sb.ToString(); } - private void MainForm_Load(object sender, EventArgs e) - { - // 1. 去除自动尺寸模式的限制,改用手动指定 - dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None; - - // 2. 计算每一列的理想宽度 - // 假设您有 N 列,除了最后一列,其余列平分空间 - int columnCount = dgv.Columns.Count; - int totalWidth = dgv.ClientSize.Width; // 获取控件当前宽度 - - // 3. 遍历每一列 - for (int i = 0; i < columnCount; i++) - { - // 这里做一个简单的平均分配逻辑 - double width = totalWidth / columnCount; - - // 设置列宽,注意:不能小于最小宽度 - dgv.Columns[i].Width = (int)Math.Ceiling(width); - } - - // 如果最后一列想稍微宽一点,或者确保宽度总和等于控件宽度,可以做微调 - int remaining = totalWidth - dgv.Columns.Cast() - .Take(columnCount - 1) - .Sum(col => col.Width); - - dgv.Columns[columnCount - 1].Width = remaining; - } + private void MainForm_Load(object sender, EventArgs e) + { + dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None; + dgv.Columns["Filename"].Width = 470; + dgv.Columns["eryi_ID"].Width = 120; + dgv.Columns["下载者用户名"].Width = 130; + } + + private void dgv_CellClick(object sender, DataGridViewCellEventArgs e) + { + HandleGridClick(e); + } + + private void HandleGridClick(DataGridViewCellEventArgs e) + { + if (e.RowIndex < 0 || e.ColumnIndex < 0) + { + return; + } + + var row = dgv.Rows[e.RowIndex]; + var columnName = dgv.Columns[e.ColumnIndex].Name; + if (columnName == "Filename") + { + var filePath = row.Tag as string; + if (!string.IsNullOrWhiteSpace(filePath) && File.Exists(filePath)) + { + OpenWithShell(filePath); + } + return; + } + + if (columnName == "eryi_ID") + { + var code = row.Cells["eryi_ID"].Value?.ToString()?.Trim() ?? ""; + if (!string.IsNullOrWhiteSpace(code) && code != "无") + { + OpenWithShell($"https://www.21cnjy.com/H/3/309824/{code}.shtml"); + } + } + } + + private static void OpenWithShell(string target) + { + try + { + Process.Start(new ProcessStartInfo + { + FileName = target, + UseShellExecute = true + }); + } + catch (Exception ex) + { + MessageBox.Show("打开失败: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } } }