detect/detect.gui/Views/WebViewService.cs

137 lines
4.7 KiB
C#
Raw Normal View History

2025-05-08 11:41:35 +08:00
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;
2024-11-21 16:28:36 +08:00
using Avalonia.Controls;
2024-11-26 15:44:31 +08:00
using Avalonia.Controls.Notifications;
2024-11-27 15:20:42 +08:00
using Avalonia.Threading;
2025-05-08 11:41:35 +08:00
using detect.gui.Services;
2024-11-26 15:44:31 +08:00
using detect.gui.VWMS;
2024-11-27 15:20:42 +08:00
using detect.gui.VWS;
2025-05-08 11:41:35 +08:00
using Serilog;
2024-11-26 15:44:31 +08:00
using Splat;
2024-11-27 15:20:42 +08:00
using WebViewControl;
2025-05-08 11:41:35 +08:00
using OfficeOpenXml;
2024-11-21 16:28:36 +08:00
namespace detect.gui.Views;
2024-12-27 13:49:37 +08:00
public class WebViewService(MainWindow? w)
2024-11-21 16:28:36 +08:00
{
2024-12-27 13:49:37 +08:00
public MainWindow? Self { get; set; } = w;
2024-11-21 16:28:36 +08:00
2025-05-08 11:41:35 +08:00
[Obsolete("过时的")]
public async Task<string?> 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;
}
}
2024-11-21 16:28:36 +08:00
2025-05-08 11:41:35 +08:00
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<Dictionary<string, string>>();
var headers = Enumerable.Range(1, colCount)
.Select(col => worksheet.Cells[1, col].Text)
.ToList();
var keys = new List<string>() { "wallType", "direction", "sn", "code", "type", "x", "y", "center", "angle" };
for (var row = 2; row <= rowCount; row++)
{
var rowData = new Dictionary<string, string>();
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) });
}
2024-11-26 15:44:31 +08:00
public Task SetIsLoading(bool isLoading)
{
Locator.Current.GetService<MainWindowModel>()!.IsLoading = isLoading;
2024-11-27 15:20:42 +08:00
Dispatcher.UIThread.InvokeAsync(() =>
{
var webView = Locator.Current.GetService<MainWindow>()!.Get<WebView>("WebView");
if (!webView.IsVisible) webView.IsVisible = Locator.Current.GetService<MainWindowModel>()!.CurrentUser != null;
});
return Task.FromResult(default(object));
2024-11-26 15:44:31 +08:00
}
public Task SetMessage(string messageText, string messageType)
{
Locator.Current.GetService<MainWindowModel>()!.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;
}
2025-05-08 11:41:35 +08:00
public Task WriteLog(string messageTitle, string messageText)
{
Log.Information("{MessageTitle}: {MessageText}", messageTitle, messageText);
return Task.CompletedTask;
}
2024-11-21 16:28:36 +08:00
}