125 lines
4.3 KiB
C#
125 lines
4.3 KiB
C#
|
|
using Npgsql.Replication.PgOutput.Messages;
|
|||
|
|
using System.Windows;
|
|||
|
|
using System.Windows.Input;
|
|||
|
|
using YY.Admin.Core.FluentValidation;
|
|||
|
|
using YY.Admin.Services.Service.AutoUpdate;
|
|||
|
|
using YY.Admin.ViewModels;
|
|||
|
|
using Window = HandyControl.Controls.Window;
|
|||
|
|
namespace YY.Admin.Views
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// LoginWindow.xaml 的交互逻辑
|
|||
|
|
/// </summary>
|
|||
|
|
public partial class LoginWindow : Window
|
|||
|
|
{
|
|||
|
|
private readonly IAutoUpdateService _autoUpdateService;
|
|||
|
|
public LoginWindow(IAutoUpdateService autoUpdateService)
|
|||
|
|
{
|
|||
|
|
InitializeComponent();
|
|||
|
|
_autoUpdateService = autoUpdateService;
|
|||
|
|
// 窗口加载完成后检查更新
|
|||
|
|
Loaded += async (s, e) => await CheckForUpdatesAsync();
|
|||
|
|
// 当 DataContext 设置后附加验证
|
|||
|
|
DataContextChanged += OnDataContextChanged;
|
|||
|
|
}
|
|||
|
|
private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
|
|||
|
|
{
|
|||
|
|
if (e.NewValue != null)
|
|||
|
|
{
|
|||
|
|
// 附加验证到 UI 控件
|
|||
|
|
FluentValidationHelper.Attach(LoginForm, typeof(LoginWindowViewModel));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
private async Task CheckForUpdatesAsync()
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
// 延迟一点时间,确保UI已经加载完成
|
|||
|
|
await Task.Delay(1000);
|
|||
|
|
|
|||
|
|
var hasUpdate = await _autoUpdateService.CheckForUpdatesAsync();
|
|||
|
|
if (hasUpdate)
|
|||
|
|
{
|
|||
|
|
await ShowUpdateDialogAsync();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
// 更新检查失败不影响登录
|
|||
|
|
System.Diagnostics.Debug.WriteLine($"更新检查异常: {ex.Message}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
private async Task ShowUpdateDialogAsync()
|
|||
|
|
{
|
|||
|
|
await Dispatcher.InvokeAsync(async () =>
|
|||
|
|
{
|
|||
|
|
// 检查窗口是否仍然打开
|
|||
|
|
if (!IsLoaded || !IsVisible || Visibility == Visibility.Collapsed)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
var versionInfo =await _autoUpdateService.GetVersionInfo();
|
|||
|
|
if (versionInfo == null) return;
|
|||
|
|
|
|||
|
|
var currentVersion = _autoUpdateService.GetCurrentVersion();
|
|||
|
|
|
|||
|
|
var updateWindow = new UpdateWindow
|
|||
|
|
{
|
|||
|
|
CurrentVersion = currentVersion,
|
|||
|
|
LatestVersion = versionInfo.LatestVersion,
|
|||
|
|
PublishDate = versionInfo.PublishDate,
|
|||
|
|
Changelog = versionInfo.Changelog,
|
|||
|
|
DownloadUrl = versionInfo.DownloadUrl,
|
|||
|
|
ApplicationName = versionInfo.ApplicationName,
|
|||
|
|
IsMandatory = versionInfo.Mandatory,
|
|||
|
|
Owner = this
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
updateWindow.UpdateRequested += (downloadUrl) =>
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
_autoUpdateService.StartUpdate(downloadUrl);
|
|||
|
|
Application.Current.Shutdown();
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
MessageBox.Show($"更新启动失败: {ex.Message}", "更新错误",
|
|||
|
|
MessageBoxButton.OK, MessageBoxImage.Error);
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
updateWindow.ShowDialog();
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
private void LoginWindow_Closed(object? sender, EventArgs e)
|
|||
|
|
{
|
|||
|
|
Application.Current.Shutdown();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void Window_KeyDown(object sender, KeyEventArgs e)
|
|||
|
|
{
|
|||
|
|
if (e.Key == Key.Enter)
|
|||
|
|
{
|
|||
|
|
Submit_Click(sender, e);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void Submit_Click(object sender, RoutedEventArgs e)
|
|||
|
|
{
|
|||
|
|
var vm = (LoginWindowViewModel)DataContext;
|
|||
|
|
vm.LoginMessage = string.Empty;
|
|||
|
|
bool valid = FluentValidationHelper.ValidateAll(LoginForm, vm.LoginInput!, vm.LoginInputValidator);
|
|||
|
|
if (valid)
|
|||
|
|
{
|
|||
|
|
if (vm.LoginCommand.CanExecute())
|
|||
|
|
{
|
|||
|
|
vm.LoginCommand.Execute();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
// 阻止事件继续冒泡
|
|||
|
|
e.Handled = true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|