优化暗码读取器
This commit is contained in:
14
client/Reader/MainForm.Designer.cs
generated
14
client/Reader/MainForm.Designer.cs
generated
@@ -21,7 +21,6 @@ namespace Reader
|
||||
Filename = new DataGridViewTextBoxColumn();
|
||||
eryi_ID = new DataGridViewTextBoxColumn();
|
||||
下载者用户名 = new DataGridViewTextBoxColumn();
|
||||
暗码写入时间 = new DataGridViewTextBoxColumn();
|
||||
((System.ComponentModel.ISupportInitialize)dgv).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@@ -58,12 +57,13 @@ namespace Reader
|
||||
dgv.AllowUserToDeleteRows = false;
|
||||
dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
|
||||
dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
|
||||
dgv.Columns.AddRange(new DataGridViewColumn[] { Filename, eryi_ID, 下载者用户名, 暗码写入时间 });
|
||||
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
|
||||
//
|
||||
@@ -92,15 +92,6 @@ namespace Reader
|
||||
下载者用户名.ReadOnly = true;
|
||||
下载者用户名.Width = 105;
|
||||
//
|
||||
// 暗码写入时间
|
||||
//
|
||||
暗码写入时间.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
|
||||
暗码写入时间.FillWeight = 300F;
|
||||
暗码写入时间.HeaderText = "暗码写入时间";
|
||||
暗码写入时间.Name = "暗码写入时间";
|
||||
暗码写入时间.ReadOnly = true;
|
||||
暗码写入时间.Width = 105;
|
||||
//
|
||||
// MainForm
|
||||
//
|
||||
ClientSize = new Size(791, 419);
|
||||
@@ -125,6 +116,5 @@ namespace Reader
|
||||
private DataGridViewTextBoxColumn Filename;
|
||||
private DataGridViewTextBoxColumn eryi_ID;
|
||||
private DataGridViewTextBoxColumn 下载者用户名;
|
||||
private DataGridViewTextBoxColumn 暗码写入时间;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
@@ -14,6 +16,10 @@ namespace Reader
|
||||
{
|
||||
private static readonly HttpClient http = new HttpClient();
|
||||
private static readonly string ServerUrl;
|
||||
private static readonly HashSet<string> DocumentExtensions = new(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
".doc", ".docx", ".pdf", ".ppt", ".pptx"
|
||||
};
|
||||
|
||||
static 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<string>();
|
||||
var fileMap = new System.Collections.Generic.Dictionary<string, string>();
|
||||
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<string>();
|
||||
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,21 +87,24 @@ 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);
|
||||
up = item.GetProperty("upload_code").GetString() ?? "";
|
||||
down = item.GetProperty("download_code").GetString() ?? "";
|
||||
}
|
||||
else
|
||||
{
|
||||
dgv.Rows.Add(fileName, "无", "无", "无");
|
||||
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)
|
||||
@@ -106,30 +119,60 @@ namespace Reader
|
||||
|
||||
private void MainForm_Load(object sender, EventArgs e)
|
||||
{
|
||||
// 1. 去除自动尺寸模式的限制,改用手动指定
|
||||
dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
|
||||
dgv.Columns["Filename"].Width = 470;
|
||||
dgv.Columns["eryi_ID"].Width = 120;
|
||||
dgv.Columns["下载者用户名"].Width = 130;
|
||||
}
|
||||
|
||||
// 2. 计算每一列的理想宽度
|
||||
// 假设您有 N 列,除了最后一列,其余列平分空间
|
||||
int columnCount = dgv.Columns.Count;
|
||||
int totalWidth = dgv.ClientSize.Width; // 获取控件当前宽度
|
||||
|
||||
// 3. 遍历每一列
|
||||
for (int i = 0; i < columnCount; i++)
|
||||
private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
|
||||
{
|
||||
// 这里做一个简单的平均分配逻辑
|
||||
double width = totalWidth / columnCount;
|
||||
|
||||
// 设置列宽,注意:不能小于最小宽度
|
||||
dgv.Columns[i].Width = (int)Math.Ceiling(width);
|
||||
HandleGridClick(e);
|
||||
}
|
||||
|
||||
// 如果最后一列想稍微宽一点,或者确保宽度总和等于控件宽度,可以做微调
|
||||
int remaining = totalWidth - dgv.Columns.Cast<DataGridViewColumn>()
|
||||
.Take(columnCount - 1)
|
||||
.Sum(col => col.Width);
|
||||
private void HandleGridClick(DataGridViewCellEventArgs e)
|
||||
{
|
||||
if (e.RowIndex < 0 || e.ColumnIndex < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
dgv.Columns[columnCount - 1].Width = remaining;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user