using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.Encodings.Web; using System.Text.Json; using System.Text.RegularExpressions; using System.Text.Unicode; using System.Threading.Tasks; using Avalonia.Controls; using Avalonia.Controls.Notifications; using Avalonia.Threading; using detect.gui.Services; using detect.gui.VWMS; using detect.gui.VWS; using Serilog; using Splat; using WebViewControl; using OfficeOpenXml; namespace detect.gui.Views; public class WebViewService(MainWindow? w) { public MainWindow? Self { get; set; } = w; [Obsolete("过时的")] public async Task ImportExcel() { var dialog = new OpenFileDialog { Title = "打开文件", AllowMultiple = false, Filters = [ new FileDialogFilter { Name = "Excel Files", Extensions = { "*.xls; *.xlsx" } } ] }; var result = await dialog.ShowAsync(Self!); if (result is not { Length: 1 }) return null; try { return ConvertExcelToJson(result[0]); } catch { return null; } } public static string ConvertExcelToJson(string excelFilePath) { var fileInfo = new FileInfo(excelFilePath); using var package = new ExcelPackage(fileInfo); var worksheet = package.Workbook.Worksheets[0]; var rowCount = worksheet.Dimension.Rows; var colCount = worksheet.Dimension.Columns; var data = new List>(); var headers = Enumerable.Range(1, colCount) .Select(col => worksheet.Cells[1, col].Text) .ToList(); var keys = new List() { "wallType", "direction", "sn", "code", "type", "x", "y", "center", "angle" }; for (var row = 2; row <= rowCount; row++) { var rowData = new Dictionary(); for (var col = 1; col <= colCount; col++) { rowData[keys[col - 1]] = worksheet.Cells[row, col].Text; } if (string.IsNullOrWhiteSpace(rowData[keys[0]])) continue; try { var w = "0"; var h = "0"; var type = OCRService.ReplaceChars(rowData["type"]); if (string.IsNullOrWhiteSpace(type)) continue; type = Regex.Replace(type, "[A-Z]", string.Empty); if (type.Contains('-', StringComparison.CurrentCultureIgnoreCase)) { type = type.Split("-")[0]; if (type.Contains('x', StringComparison.CurrentCultureIgnoreCase)) { w = type.Split("x")[0]; h = type.Split("x")[1]; } else { w = type; h = type; } } else if (type.Contains('x', StringComparison.CurrentCultureIgnoreCase)) { w = type.Split("x")[0]; h = type.Split("x")[1]; } else continue; rowData["w"] = w; rowData["h"] = h; data.Add(rowData); } catch {} } return JsonSerializer.Serialize(data, new JsonSerializerOptions { WriteIndented = true, Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) }); } public Task SetIsLoading(bool isLoading) { Locator.Current.GetService()!.IsLoading = isLoading; Dispatcher.UIThread.InvokeAsync(() => { var webView = Locator.Current.GetService()!.Get("WebView"); if (!webView.IsVisible) webView.IsVisible = Locator.Current.GetService()!.CurrentUser != null; }); return Task.FromResult(default(object)); } public Task SetMessage(string messageText, string messageType) { Locator.Current.GetService()!.Message = messageType switch { "success" => new MessageItem(messageText, NotificationType.Success), "information" => new MessageItem(messageText, NotificationType.Information), "error" => new MessageItem(messageText, NotificationType.Error), _ => new MessageItem(messageText, NotificationType.Warning) }; return Task.CompletedTask; } public Task WriteLog(string messageTitle, string messageText) { Log.Information("{MessageTitle}: {MessageText}", messageTitle, messageText); return Task.CompletedTask; } }