133 lines
3.5 KiB
C#
133 lines
3.5 KiB
C#
using System.Reflection;
|
|
using System.Windows;
|
|
using Cardiopulmonarybypasssystems.Models;
|
|
using Cardiopulmonarybypasssystems.Services;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
|
|
namespace Cardiopulmonarybypasssystems.ViewModels;
|
|
|
|
public partial class StartupPasswordViewModel : ObservableObject
|
|
{
|
|
private readonly IPasswordAccessService _passwordAccessService;
|
|
|
|
public StartupPasswordViewModel(IPasswordAccessService passwordAccessService)
|
|
{
|
|
_passwordAccessService = passwordAccessService;
|
|
ProductName = "心肺转流检测系统";
|
|
SoftwareVersion = Assembly.GetExecutingAssembly().GetName().Version?.ToString(3) ?? "1.0.0";
|
|
RefreshStatus();
|
|
}
|
|
|
|
[ObservableProperty]
|
|
private string productName = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private string softwareVersion = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private string passwordText = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private string statusTitle = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private string statusDetail = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private string stageText = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private string expiresAtText = "--";
|
|
|
|
[ObservableProperty]
|
|
private string remainingText = "--";
|
|
|
|
[ObservableProperty]
|
|
private string footerHint = "规则:先使用 20 天,提前 3 天提醒;满 20 天输入密码后再使用 30 天,提前 3 天再次提醒。";
|
|
|
|
[ObservableProperty]
|
|
private bool canSubmit;
|
|
|
|
public bool CanContinue => _passwordAccessService.GetStatus().CanLaunch;
|
|
|
|
[RelayCommand]
|
|
private void Submit(Window? window)
|
|
{
|
|
if (!_passwordAccessService.TryUnlock(PasswordText, out _, out var message))
|
|
{
|
|
RefreshStatus();
|
|
StatusDetail = message;
|
|
return;
|
|
}
|
|
|
|
PasswordText = string.Empty;
|
|
RefreshStatus();
|
|
StatusDetail = message;
|
|
|
|
if (_passwordAccessService.GetStatus().CanLaunch && window is not null)
|
|
{
|
|
window.DialogResult = true;
|
|
window.Close();
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void Continue(Window? window)
|
|
{
|
|
if (!_passwordAccessService.GetStatus().CanLaunch || window is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
window.DialogResult = true;
|
|
window.Close();
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void Close(Window? window)
|
|
{
|
|
window?.Close();
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void AppendPasswordCharacter(string? value)
|
|
{
|
|
if (string.IsNullOrEmpty(value))
|
|
{
|
|
return;
|
|
}
|
|
|
|
PasswordText += value;
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void BackspacePassword()
|
|
{
|
|
if (string.IsNullOrEmpty(PasswordText))
|
|
{
|
|
return;
|
|
}
|
|
|
|
PasswordText = PasswordText[..^1];
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void ClearPassword()
|
|
{
|
|
PasswordText = string.Empty;
|
|
}
|
|
|
|
private void RefreshStatus()
|
|
{
|
|
var status = _passwordAccessService.GetStatus();
|
|
StatusTitle = status.StatusTitle;
|
|
StatusDetail = status.StatusDetail;
|
|
StageText = status.StageText;
|
|
ExpiresAtText = status.ExpiresAt?.ToString("yyyy-MM-dd HH:mm") ?? (status.IsPermanent ? "永久有效" : "--");
|
|
RemainingText = status.IsPermanent ? "永久" : status.RemainingDays > 0 ? $"{status.RemainingDays} 天" : "需输入密码";
|
|
CanSubmit = status.CanSubmitPassword;
|
|
OnPropertyChanged(nameof(CanContinue));
|
|
}
|
|
}
|