215 lines
8.3 KiB
C#
215 lines
8.3 KiB
C#
|
|
using Avalonia.Controls;
|
|||
|
|
using Avalonia.Media;
|
|||
|
|
using Footwear_Test_methodsfor_wholeshoe_Slipresistanceperformance.Models;
|
|||
|
|
using Footwear_Test_methodsfor_wholeshoe_Slipresistanceperformance.Services;
|
|||
|
|
using Serilog;
|
|||
|
|
using System;
|
|||
|
|
|
|||
|
|
namespace Footwear_Test_methodsfor_wholeshoe_Slipresistanceperformance.Views
|
|||
|
|
{
|
|||
|
|
public enum LicenseWindowMode
|
|||
|
|
{
|
|||
|
|
Initialization,
|
|||
|
|
Unlock,
|
|||
|
|
Administration,
|
|||
|
|
Blocked
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public partial class LicenseWindow : Window
|
|||
|
|
{
|
|||
|
|
private readonly MachineLicenseService licenseService;
|
|||
|
|
private readonly LicenseWindowMode mode;
|
|||
|
|
private bool adminAuthenticated;
|
|||
|
|
|
|||
|
|
public bool Succeeded { get; private set; }
|
|||
|
|
|
|||
|
|
public LicenseWindow() : this(new MachineLicenseService(), LicenseWindowMode.Blocked, "授权窗口需要由软件启动流程打开。")
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public LicenseWindow(MachineLicenseService licenseService, LicenseWindowMode mode, string message = "")
|
|||
|
|
{
|
|||
|
|
this.licenseService = licenseService;
|
|||
|
|
this.mode = mode;
|
|||
|
|
InitializeComponent();
|
|||
|
|
|
|||
|
|
MessageText.Text = message;
|
|||
|
|
InitializationPanel.IsVisible = mode == LicenseWindowMode.Initialization;
|
|||
|
|
UnlockPanel.IsVisible = mode == LicenseWindowMode.Unlock;
|
|||
|
|
AdminLoginPanel.IsVisible = mode == LicenseWindowMode.Administration;
|
|||
|
|
CancelButton.Content = mode == LicenseWindowMode.Administration ? "关闭" : "退出";
|
|||
|
|
|
|||
|
|
switch (mode)
|
|||
|
|
{
|
|||
|
|
case LicenseWindowMode.Initialization:
|
|||
|
|
TitleText.Text = "首次使用授权设置";
|
|||
|
|
PrimaryButton.Content = "保存并开始使用";
|
|||
|
|
break;
|
|||
|
|
case LicenseWindowMode.Unlock:
|
|||
|
|
TitleText.Text = "软件使用时效已到";
|
|||
|
|
PrimaryButton.Content = "校验并继续使用";
|
|||
|
|
break;
|
|||
|
|
case LicenseWindowMode.Administration:
|
|||
|
|
TitleText.Text = "时效授权管理";
|
|||
|
|
PrimaryButton.Content = "验证管理密码";
|
|||
|
|
break;
|
|||
|
|
default:
|
|||
|
|
TitleText.Text = "软件授权已锁定";
|
|||
|
|
PrimaryButton.IsVisible = false;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void OnPrimaryClick(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
|
|||
|
|
{
|
|||
|
|
ErrorText.Foreground = Brushes.Crimson;
|
|||
|
|
ErrorText.Text = string.Empty;
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
switch (mode)
|
|||
|
|
{
|
|||
|
|
case LicenseWindowMode.Initialization:
|
|||
|
|
InitializeLicense();
|
|||
|
|
break;
|
|||
|
|
case LicenseWindowMode.Unlock:
|
|||
|
|
UnlockLicense();
|
|||
|
|
break;
|
|||
|
|
case LicenseWindowMode.Administration:
|
|||
|
|
HandleAdministration();
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
Log.Warning(ex, "授权窗口操作失败:Mode={Mode}", mode);
|
|||
|
|
ErrorText.Text = ex.Message;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void InitializeLicense()
|
|||
|
|
{
|
|||
|
|
RequireMatch(InitialAdminPassword.Text, InitialAdminConfirm.Text, "管理密码");
|
|||
|
|
RequireMatch(InitialFirstPassword.Text, InitialFirstConfirm.Text, "第一次时效密码");
|
|||
|
|
RequireMatch(InitialSecondPassword.Text, InitialSecondConfirm.Text, "第二次时效密码");
|
|||
|
|
licenseService.Initialize(
|
|||
|
|
InitialAdminPassword.Text ?? string.Empty,
|
|||
|
|
InitialFirstPassword.Text ?? string.Empty,
|
|||
|
|
InitialSecondPassword.Text ?? string.Empty,
|
|||
|
|
ReadMonths(InitialFirstMonths),
|
|||
|
|
ReadMonths(InitialSecondMonths));
|
|||
|
|
Succeeded = true;
|
|||
|
|
Close();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void UnlockLicense()
|
|||
|
|
{
|
|||
|
|
if (!licenseService.UnlockCurrentStage(UnlockPassword.Text ?? string.Empty))
|
|||
|
|
{
|
|||
|
|
ErrorText.Text = "时效密码不正确。";
|
|||
|
|
UnlockPassword.Text = string.Empty;
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Succeeded = true;
|
|||
|
|
Close();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void HandleAdministration()
|
|||
|
|
{
|
|||
|
|
if (!adminAuthenticated)
|
|||
|
|
{
|
|||
|
|
if (!licenseService.VerifyAdminPassword(AdminLoginPassword.Text ?? string.Empty))
|
|||
|
|
{
|
|||
|
|
ErrorText.Text = "管理密码不正确。";
|
|||
|
|
AdminLoginPassword.Text = string.Empty;
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
adminAuthenticated = true;
|
|||
|
|
AdminLoginPanel.IsVisible = false;
|
|||
|
|
AdminSettingsPanel.IsVisible = true;
|
|||
|
|
PrimaryButton.Content = "保存设置";
|
|||
|
|
LoadAdminValues();
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var newAdminPassword = ReadOptionalMatchingPassword(NewAdminPassword.Text, NewAdminConfirm.Text, "新管理密码");
|
|||
|
|
var newFirstPassword = ReadOptionalMatchingPassword(NewFirstPassword.Text, NewFirstConfirm.Text, "新第一次时效密码");
|
|||
|
|
var newSecondPassword = ReadOptionalMatchingPassword(NewSecondPassword.Text, NewSecondConfirm.Text, "新第二次时效密码");
|
|||
|
|
licenseService.UpdateSettings(
|
|||
|
|
newAdminPassword,
|
|||
|
|
newFirstPassword,
|
|||
|
|
newSecondPassword,
|
|||
|
|
ReadMonths(AdminFirstMonths),
|
|||
|
|
ReadMonths(AdminSecondMonths));
|
|||
|
|
LicenseStatusText.Text = licenseService.DescribeCurrent();
|
|||
|
|
ErrorText.Foreground = Avalonia.Media.Brushes.ForestGreen;
|
|||
|
|
ErrorText.Text = "时效授权设置已保存。";
|
|||
|
|
NewAdminPassword.Text = string.Empty;
|
|||
|
|
NewAdminConfirm.Text = string.Empty;
|
|||
|
|
NewFirstPassword.Text = string.Empty;
|
|||
|
|
NewFirstConfirm.Text = string.Empty;
|
|||
|
|
NewSecondPassword.Text = string.Empty;
|
|||
|
|
NewSecondConfirm.Text = string.Empty;
|
|||
|
|
Succeeded = true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void LoadAdminValues()
|
|||
|
|
{
|
|||
|
|
var data = licenseService.Current ?? throw new InvalidOperationException("授权状态未加载。");
|
|||
|
|
AdminFirstMonths.Value = data.FirstPeriodMonths;
|
|||
|
|
AdminSecondMonths.Value = data.SecondPeriodMonths;
|
|||
|
|
LicenseStatusText.Text = licenseService.DescribeCurrent();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void OnRestartTimingClick(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
|
|||
|
|
{
|
|||
|
|
ConfirmationPanel.IsVisible = true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void OnConfirmRestartClick(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
licenseService.RestartTiming();
|
|||
|
|
ConfirmationPanel.IsVisible = false;
|
|||
|
|
LicenseStatusText.Text = licenseService.DescribeCurrent();
|
|||
|
|
ErrorText.Foreground = Avalonia.Media.Brushes.ForestGreen;
|
|||
|
|
ErrorText.Text = "已从当前时间重新开始第一阶段计时。";
|
|||
|
|
Succeeded = true;
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
ErrorText.Text = ex.Message;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void OnCancelRestartClick(object? sender, Avalonia.Interactivity.RoutedEventArgs e) =>
|
|||
|
|
ConfirmationPanel.IsVisible = false;
|
|||
|
|
|
|||
|
|
private void OnCancelClick(object? sender, Avalonia.Interactivity.RoutedEventArgs e) => Close();
|
|||
|
|
|
|||
|
|
private static int ReadMonths(NumericUpDown input) =>
|
|||
|
|
decimal.ToInt32(input.Value ?? throw new ArgumentException("请输入有效的时效月数。"));
|
|||
|
|
|
|||
|
|
private static void RequireMatch(string? password, string? confirmation, string label)
|
|||
|
|
{
|
|||
|
|
if (!string.Equals(password, confirmation, StringComparison.Ordinal))
|
|||
|
|
{
|
|||
|
|
throw new ArgumentException($"{label}与确认输入不一致。");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static string? ReadOptionalMatchingPassword(string? password, string? confirmation, string label)
|
|||
|
|
{
|
|||
|
|
if (string.IsNullOrWhiteSpace(password) && string.IsNullOrWhiteSpace(confirmation))
|
|||
|
|
{
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
RequireMatch(password, confirmation, label);
|
|||
|
|
return password;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|