143 lines
5.0 KiB
C#
143 lines
5.0 KiB
C#
|
|
using System.Collections.ObjectModel;
|
||
|
|
using System.Windows;
|
||
|
|
using Prism.Commands;
|
||
|
|
using YY.Admin.Core.Entity;
|
||
|
|
using YY.Admin.Core.Services;
|
||
|
|
using YY.Admin.Services.Service.Print;
|
||
|
|
using YY.Admin.Core;
|
||
|
|
|
||
|
|
namespace YY.Admin.ViewModels.Print;
|
||
|
|
|
||
|
|
public class PrintSettingsViewModel : BaseViewModel
|
||
|
|
{
|
||
|
|
private readonly IPrintDotService _printDotService;
|
||
|
|
private readonly IPrintTemplateService _printTemplateService;
|
||
|
|
|
||
|
|
// ── 连接设置 ──────────────────────────────────────────────────────────
|
||
|
|
|
||
|
|
private string _wsUrl = "ws://127.0.0.1:1122/ws";
|
||
|
|
public string WsUrl
|
||
|
|
{
|
||
|
|
get => _wsUrl;
|
||
|
|
set => SetProperty(ref _wsUrl, value);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ── 打印机列表 ──────────────────────────────────────────────────────────
|
||
|
|
|
||
|
|
public ObservableCollection<PrintDotPrinter> Printers { get; } = new();
|
||
|
|
|
||
|
|
private PrintDotPrinter? _selectedPrinter;
|
||
|
|
public PrintDotPrinter? SelectedPrinter
|
||
|
|
{
|
||
|
|
get => _selectedPrinter;
|
||
|
|
set => SetProperty(ref _selectedPrinter, value);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ── 模板列表 ────────────────────────────────────────────────────────────
|
||
|
|
|
||
|
|
public ObservableCollection<PrintTemplate> Templates { get; } = new();
|
||
|
|
|
||
|
|
// ── 状态 ────────────────────────────────────────────────────────────────
|
||
|
|
|
||
|
|
private bool _isBusy;
|
||
|
|
public bool IsBusy
|
||
|
|
{
|
||
|
|
get => _isBusy;
|
||
|
|
set => SetProperty(ref _isBusy, value);
|
||
|
|
}
|
||
|
|
|
||
|
|
private string _statusMessage = string.Empty;
|
||
|
|
public string StatusMessage
|
||
|
|
{
|
||
|
|
get => _statusMessage;
|
||
|
|
set => SetProperty(ref _statusMessage, value);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ── 命令 ────────────────────────────────────────────────────────────────
|
||
|
|
|
||
|
|
public DelegateCommand TestConnectionCommand { get; }
|
||
|
|
public DelegateCommand SaveCommand { get; }
|
||
|
|
public DelegateCommand RefreshTemplatesCommand { get; }
|
||
|
|
|
||
|
|
public PrintSettingsViewModel(
|
||
|
|
IPrintDotService printDotService,
|
||
|
|
IPrintTemplateService printTemplateService,
|
||
|
|
IContainerExtension container,
|
||
|
|
IRegionManager regionManager) : base(container, regionManager)
|
||
|
|
{
|
||
|
|
_printDotService = printDotService;
|
||
|
|
_printTemplateService = printTemplateService;
|
||
|
|
|
||
|
|
TestConnectionCommand = new DelegateCommand(async () => await TestConnectionAsync());
|
||
|
|
SaveCommand = new DelegateCommand(SaveSettings);
|
||
|
|
RefreshTemplatesCommand = new DelegateCommand(async () => await RefreshTemplatesAsync());
|
||
|
|
|
||
|
|
// 加载已保存的设置
|
||
|
|
var saved = PrintDotSettings.Load();
|
||
|
|
WsUrl = saved.WsUrl;
|
||
|
|
if (!string.IsNullOrWhiteSpace(saved.SelectedPrinter))
|
||
|
|
_selectedPrinter = new PrintDotPrinter(saved.SelectedPrinter, false);
|
||
|
|
}
|
||
|
|
|
||
|
|
private async Task TestConnectionAsync()
|
||
|
|
{
|
||
|
|
IsBusy = true;
|
||
|
|
StatusMessage = "正在连接...";
|
||
|
|
Printers.Clear();
|
||
|
|
try
|
||
|
|
{
|
||
|
|
// 临时用当前输入的 URL 测试
|
||
|
|
PrintDotSettings.Current!.WsUrl = WsUrl;
|
||
|
|
var list = await _printDotService.GetPrintersAsync();
|
||
|
|
foreach (var p in list) Printers.Add(p);
|
||
|
|
|
||
|
|
// 还原保存的已选打印机
|
||
|
|
var saved = PrintDotSettings.Load();
|
||
|
|
var match = list.FirstOrDefault(p => p.Name == saved.SelectedPrinter);
|
||
|
|
SelectedPrinter = match ?? (list.Count > 0 ? list[0] : null);
|
||
|
|
|
||
|
|
StatusMessage = $"连接成功,共 {list.Count} 台打印机";
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
StatusMessage = $"连接失败:{ex.Message}";
|
||
|
|
}
|
||
|
|
finally
|
||
|
|
{
|
||
|
|
IsBusy = false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void SaveSettings()
|
||
|
|
{
|
||
|
|
var settings = new PrintDotSettings
|
||
|
|
{
|
||
|
|
WsUrl = WsUrl.Trim(),
|
||
|
|
SelectedPrinter = SelectedPrinter?.Name ?? string.Empty
|
||
|
|
};
|
||
|
|
settings.Save();
|
||
|
|
StatusMessage = "设置已保存";
|
||
|
|
MessageBox.Show("PrintDot 设置已保存", "保存成功", MessageBoxButton.OK, MessageBoxImage.Information);
|
||
|
|
}
|
||
|
|
|
||
|
|
private async Task RefreshTemplatesAsync()
|
||
|
|
{
|
||
|
|
IsBusy = true;
|
||
|
|
Templates.Clear();
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var list = await _printTemplateService.ListAsync();
|
||
|
|
foreach (var t in list) Templates.Add(t);
|
||
|
|
StatusMessage = $"已加载 {list.Count} 个打印模板";
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
StatusMessage = $"加载模板失败:{ex.Message}";
|
||
|
|
}
|
||
|
|
finally
|
||
|
|
{
|
||
|
|
IsBusy = false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|