diff --git a/Ramitta/CryptoHelper.cs b/Ramitta/CryptoHelper.cs index 57ecbcd..454026d 100644 --- a/Ramitta/CryptoHelper.cs +++ b/Ramitta/CryptoHelper.cs @@ -1,10 +1,6 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; +using System.IO; using System.Security.Cryptography; using System.Text; -using System.Threading.Tasks; using System.Windows; namespace Ramitta.lib @@ -156,7 +152,7 @@ namespace Ramitta.lib return Convert.ToBase64String(signature); } } - catch (Exception ex) + catch (Exception) { return null; } @@ -179,7 +175,7 @@ namespace Ramitta.lib return Encoding.UTF8.GetString(decryptedBytes); } } - catch (Exception ex) + catch (Exception) { return null; } @@ -251,7 +247,7 @@ namespace Ramitta.lib return Convert.ToBase64String(signature); } } - catch (Exception ex) + catch (Exception) { return null; } diff --git a/Ramitta/Database/Neo4j.cs b/Ramitta/Database/Neo4j.cs index 4edf68b..910d21f 100644 --- a/Ramitta/Database/Neo4j.cs +++ b/Ramitta/Database/Neo4j.cs @@ -1,10 +1,4 @@ using Neo4j.Driver; -using NPOI; -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Text; -using System.Threading.Tasks; namespace Ramitta { @@ -58,7 +52,8 @@ namespace Ramitta string nodeType2, Dictionary nodeProperties2, // 使用字典替代单独的参数 ArrowDirection arrow = ArrowDirection.Right, Dictionary relationshipProperties = null // 可选字典参数 - ){ + ) + { using (var session = _driver.AsyncSession()) { // 构建查询的 MATCH 部分,动态地使用字典中的键值对来构造节点的属性 @@ -97,7 +92,7 @@ namespace Ramitta // await neo4jService.GetRelatedNodesAsync("文件", properties); public async Task>> GetRelatedNodesAsync( string nodeLabel, - Dictionary? nodeProperties=null) + Dictionary? nodeProperties = null) { string query; if (nodeProperties == null) diff --git a/Ramitta/Database/PostgreSQL.cs b/Ramitta/Database/PostgreSQL.cs index 9906c27..dabc074 100644 --- a/Ramitta/Database/PostgreSQL.cs +++ b/Ramitta/Database/PostgreSQL.cs @@ -1,10 +1,5 @@ using Npgsql; -using System; -using System.Collections.Generic; using System.Diagnostics; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Ramitta { @@ -145,7 +140,7 @@ namespace Ramitta { for (int i = 0; i < queries.Count; i++) { - using (var cmd = new NpgsqlCommand(queries[i], conn, (NpgsqlTransaction)transaction)) + using (var cmd = new NpgsqlCommand(queries[i], conn, transaction)) { var parameters = parametersList[i]; if (parameters != null) diff --git a/Ramitta/Database/SQLite.cs b/Ramitta/Database/SQLite.cs index d0c1d87..2670bb3 100644 --- a/Ramitta/Database/SQLite.cs +++ b/Ramitta/Database/SQLite.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Data; +using System.Data; using System.Data.SQLite; namespace Ramitta diff --git a/Ramitta/Excel.cs b/Ramitta/Excel.cs index a630795..851e898 100644 --- a/Ramitta/Excel.cs +++ b/Ramitta/Excel.cs @@ -1,11 +1,6 @@ using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; -using System; -using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Ramitta.lib { @@ -85,8 +80,8 @@ namespace Ramitta.lib } public static Dictionary> ReadExcelAsDictCol( - string filePath, - object sheetName = null, + string filePath, + object sheetName = null, List headerInit = null) { var result = new Dictionary>(); @@ -166,23 +161,55 @@ namespace Ramitta.lib return true; } - public static ICell? getRowCell(ISheet sheet, int rowIndex, int cellIndex) + public static ICell? getRowCell(ISheet sheet, int rowIndex, object cellIndex) { + int actualCellIndex = 0; + if (cellIndex is int intIndex) + { + // 如果输入是数字,直接使用 + actualCellIndex = intIndex; + } + else if (cellIndex is string strIndex) + { + actualCellIndex = ColToIndex(strIndex); + } + else + { + return null; + } + IRow row = sheet.GetRow(rowIndex); if (row == null) return null; - ICell cell = row.GetCell(cellIndex); + ICell cell = row.GetCell(actualCellIndex); if (cell == null) return null; return cell; } - public static String? getRowCellStr(ISheet sheet, int rowIndex, int cellIndex) + + public static String? getRowCellStr(ISheet sheet, int rowIndex, object cellIndex) { - var cellValue = getRowCell(sheet, rowIndex, cellIndex)?.ToString(); + int actualCellIndex = 0; + if (cellIndex is int intIndex) + { + // 如果输入是数字,直接使用 + actualCellIndex = intIndex; + } + else if (cellIndex is string strIndex) + { + actualCellIndex = ColToIndex(strIndex); + } + else + { + return null; + } + + var cellValue = getRowCell(sheet, rowIndex, actualCellIndex)?.ToString(); return string.IsNullOrWhiteSpace(cellValue) ? null : cellValue; } - // 简短版本 + // 简短版本 + // 列名字转为列号 public static int ColToIndex(string col) { return col.ToUpper().Aggregate(0, (cur, ch) => cur * 26 + (ch - 'A')); diff --git a/Ramitta/HttpHelper.cs b/Ramitta/HttpHelper.cs new file mode 100644 index 0000000..8019236 --- /dev/null +++ b/Ramitta/HttpHelper.cs @@ -0,0 +1,77 @@ +using System.IO; +using System.Net; +using System.Text; + +namespace Ramitta.lib +{ + public static class HttpHelper + { + // 异步发送HTTP GET请求 + public static async Task SendHttpGetAsync(string url, int timeout = 10000) + { + try + { + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); + request.Method = "GET"; + request.Timeout = timeout; + request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"; + + using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync()) + using (Stream stream = response.GetResponseStream()) + using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) + { + return await reader.ReadToEndAsync(); + } + } + catch (Exception ex) + { + throw new Exception($"GET请求失败: {ex.Message}"); + } + } + + public static async Task SendHttpPostAsync(string url, string jsonData, int timeout = 10000) + { + try + { + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); + request.Method = "POST"; + request.Timeout = timeout; + request.ContentType = "application/json"; + request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"; + + byte[] data = Encoding.UTF8.GetBytes(jsonData); + request.ContentLength = data.Length; + + using (Stream requestStream = await request.GetRequestStreamAsync()) + { + await requestStream.WriteAsync(data, 0, data.Length); + } + + using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync()) + using (Stream responseStream = response.GetResponseStream()) + using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8)) + { + return await reader.ReadToEndAsync(); + } + } + catch (WebException ex) + { + WebResponse errorResponse = ex.Response; + if (errorResponse is HttpWebResponse httpErrorResponse) + { + using (Stream errorStream = httpErrorResponse.GetResponseStream()) + using (StreamReader errorReader = new StreamReader(errorStream, Encoding.UTF8)) + { + string errorContent = await errorReader.ReadToEndAsync(); + throw new Exception($"HTTP错误 {(int)httpErrorResponse.StatusCode} ({httpErrorResponse.StatusCode}): {errorContent}"); + } + } + throw new Exception("网络错误: " + ex.Message); + } + catch (Exception ex) + { + throw new Exception("POST请求失败: " + ex.Message); + } + } + } +} diff --git a/Ramitta/Ramitta.cs b/Ramitta/Ramitta.cs index c9b1b5c..6cc8830 100644 --- a/Ramitta/Ramitta.cs +++ b/Ramitta/Ramitta.cs @@ -1,16 +1,9 @@ -using Microsoft.VisualBasic; -using NPOI.SS.UserModel; -using NPOI.XSSF.UserModel; -using System; -using System.Collections.Generic; using System.Diagnostics; using System.IO; -using System.Linq; using System.Net; using System.Net.Sockets; using System.Security.Cryptography; using System.Text; -using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Media; @@ -32,13 +25,13 @@ namespace Ramitta.lib #endregion #region Ժ - public static void DebugBar(Label obj, String? text, SolidColorBrush? color=null) + public static void DebugBar(Label obj, String? text, SolidColorBrush? color = null) { if (obj == null) return; // ͨDispatcherȷUIִ߳ obj.Dispatcher.Invoke(() => { - if (text!=null)obj.Content = text; + if (text != null) obj.Content = text; if (color != null) obj.Background = color; @@ -122,37 +115,6 @@ namespace Ramitta.lib } #endregion - #region - public static Dictionary? Startupe; - public static Dictionary ParseCommandLineArgs(string[] args) - { - var arguments = new Dictionary(StringComparer.OrdinalIgnoreCase); - - for (int i = 0; i < args.Length; i++) - { - string arg = args[i]; - - // ǷDz-/ͷ - if (arg.StartsWith("-") || arg.StartsWith("/")) - { - string key = arg.TrimStart('-', '/'); - string value = "true"; // ĬֵΪtrueʾش - - // һǷֵ-/ͷ - if (i + 1 < args.Length && !args[i + 1].StartsWith("-") && !args[i + 1].StartsWith("/")) - { - value = args[i + 1]; - i++; // һΪѾֵ - } - - arguments[key] = value; - } - } - - return arguments; - } - #endregion - #region ļ public static bool IsPathExist(string databasePath) { @@ -163,7 +125,7 @@ namespace Ramitta.lib return Directory.Exists(databasePath); } - public static void FileWrite(string outputPath, string outputText, bool createDirectories = true,bool fileAdd = false) + public static void FileWrite(string outputPath, string outputText, bool createDirectories = true, bool fileAdd = false) { string directory = Path.GetDirectoryName(outputPath); if (createDirectories && !Directory.Exists(directory)) @@ -182,7 +144,7 @@ namespace Ramitta.lib { try { - return File.ReadAllText(outputPath); + return File.ReadAllText(outputPath); } catch { @@ -262,7 +224,7 @@ namespace Ramitta.lib #endregion #region - public static async Task RunExternalCommand(string? applicationPath = "explorer.exe", string arguments="", bool UseShellExecute = false, bool CreateNoWindow = false) + public static async Task RunExternalCommand(string? applicationPath = "explorer.exe", string arguments = "", bool UseShellExecute = false, bool CreateNoWindow = false) { ProcessStartInfo startInfo = new ProcessStartInfo { @@ -289,7 +251,7 @@ namespace Ramitta.lib } - catch (Exception ex) + catch (Exception) { throw; } @@ -335,7 +297,7 @@ namespace Ramitta.lib return output; // ر׼ } } - catch (Exception ex) + catch (Exception) { // Լ¼쳣 throw; // ׳쳣Ա߲ diff --git a/Ramitta/SerialPortHandler.cs b/Ramitta/SerialPortHandler.cs index a391926..4225a4a 100644 --- a/Ramitta/SerialPortHandler.cs +++ b/Ramitta/SerialPortHandler.cs @@ -1,6 +1,4 @@ -using System; -using System.IO.Ports; -using System.Threading; +using System.IO.Ports; namespace Ramitta.lib diff --git a/Ramitta/Utils/Win32.cs b/Ramitta/Utils/Win32.cs index 84b67fe..d909fa1 100644 --- a/Ramitta/Utils/Win32.cs +++ b/Ramitta/Utils/Win32.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; +using System.Runtime.InteropServices; namespace Ramitta.Utils { diff --git a/Ramitta/Utils/WindowTopArea.cs b/Ramitta/Utils/WindowTopArea.cs index 03ea9d6..e1d9b9d 100644 --- a/Ramitta/Utils/WindowTopArea.cs +++ b/Ramitta/Utils/WindowTopArea.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows; +using System.Windows; using System.Windows.Input; namespace Ramitta.Utils @@ -47,7 +42,6 @@ namespace Ramitta.Utils Window win = Window.GetWindow(this); ResizePosition ResPosition = ResizePosition.None; int Resizer = 10; - int ResizerSpeed = 10; win.MouseMove += new MouseEventHandler( delegate (object target, MouseEventArgs args) { diff --git a/Ramitta/winDataGrid.xaml.cs b/Ramitta/winDataGrid.xaml.cs index da9adfc..dd03e4b 100644 --- a/Ramitta/winDataGrid.xaml.cs +++ b/Ramitta/winDataGrid.xaml.cs @@ -1,13 +1,10 @@ using Newtonsoft.Json.Linq; -using System; -using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Media; -using static Ramitta.winDataGrid; namespace Ramitta { @@ -20,7 +17,7 @@ namespace Ramitta > Rows { get; private set; } - public Dictionary ColumnsName=new(); + public Dictionary ColumnsName = new(); public winDataGrid() { @@ -59,7 +56,7 @@ namespace Ramitta public void AddColumn(string columnName, ColumnType columnType) { DataGridTemplateColumn column = new DataGridTemplateColumn(); - FrameworkElementFactory? elementFactory=null; + FrameworkElementFactory? elementFactory = null; ColumnsName.Add(columnName, columnType); switch (columnType) @@ -90,21 +87,23 @@ namespace Ramitta SetBindingToProperty(elementFactory, TextBox.BackgroundProperty, $"[{columnName}].Background"); break; } - DataTemplate dataTemplate = new DataTemplate() { VisualTree=elementFactory}; - + DataTemplate dataTemplate = new DataTemplate() { VisualTree = elementFactory }; + column.Header = columnName; column.CellTemplate = dataTemplate; column.CellEditingTemplate = dataTemplate; - xDataGrid.Columns.Add( column ); + xDataGrid.Columns.Add(column); } - - public Dictionary AddRow(){ + + public Dictionary AddRow() + { var keys = ColumnsName.Keys.ToList(); var row = new Dictionary { }; - foreach (var key in keys) { + foreach (var key in keys) + { switch (ColumnsName[key]) { @@ -118,7 +117,7 @@ namespace Ramitta row.Add(key, new ComboBox()); break; case ColumnType.TextBox: - row.Add(key, new TextBox()); + row.Add(key, new TextBox()); break; case ColumnType.Label: row.Add(key, new Label()); @@ -130,7 +129,8 @@ namespace Ramitta return row; } - public void Clear() { + public void Clear() + { Rows.Clear(); } diff --git a/Ramitta/winTitleBar.xaml.cs b/Ramitta/winTitleBar.xaml.cs index afc0d42..0706735 100644 --- a/Ramitta/winTitleBar.xaml.cs +++ b/Ramitta/winTitleBar.xaml.cs @@ -1,5 +1,4 @@ -using System; -using System.Windows; +using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; diff --git a/Ramitta/winTreeList.xaml.cs b/Ramitta/winTreeList.xaml.cs index 41ff423..1950ea0 100644 --- a/Ramitta/winTreeList.xaml.cs +++ b/Ramitta/winTreeList.xaml.cs @@ -1,26 +1,10 @@ using Newtonsoft.Json.Linq; -using System; -using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; -using System.Linq; -using System.Text; -using System.Threading.Channels; -using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Documents; -using System.Windows.Input; using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Navigation; -using System.Windows.Shapes; -using System.Windows.Threading; -using System.Xml.Linq; -using static System.Net.Mime.MediaTypeNames; -using static System.Runtime.InteropServices.JavaScript.JSType; namespace Ramitta { @@ -104,9 +88,9 @@ namespace Ramitta return node; } - public RadioButtonTreeNode AddRadioButtonNode(string text, string? tag = null,string? GroupName = null, bool isChecked = false, TreeNode? parent = null) + public RadioButtonTreeNode AddRadioButtonNode(string text, string? tag = null, string? GroupName = null, bool isChecked = false, TreeNode? parent = null) { - + var RadioButton = new RadioButton() { Visibility = Visibility.Visible, @@ -285,8 +269,7 @@ namespace Ramitta // 清空现有数据 Clear(); - if (string.IsNullOrWhiteSpace(jsonContent)) - return; + if (string.IsNullOrWhiteSpace(jsonContent)) return; try { @@ -320,7 +303,7 @@ namespace Ramitta { case nameof(LabelTreeNode): newNode = CreateLabelTreeNode(nodeObject, text); - + break; case nameof(CheckboxTreeNode): @@ -437,7 +420,7 @@ namespace Ramitta ExpandNode(node); } } - public void CollapseAll(object? sender=null, RoutedEventArgs? e=null) + public void CollapseAll(object? sender = null, RoutedEventArgs? e = null) { foreach (var node in Nodes) { @@ -489,7 +472,7 @@ namespace Ramitta parent.Children.Add(node); } } - private IEnumerable GetAllNodes(IEnumerable? nodes=null) + private IEnumerable GetAllNodes(IEnumerable? nodes = null) { if (nodes == null) nodes = null; foreach (var node in nodes)