SoftAuthDialog.xaml.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using QRCoder;
  2. using SWRIS.Core;
  3. using SWRIS.Extensions;
  4. using SWRIS.Models;
  5. using System;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. namespace SWRIS
  14. {
  15. /// <summary>
  16. /// SoftAuthDialog.xaml 的交互逻辑
  17. /// </summary>
  18. public partial class SoftAuthDialog : Window
  19. {
  20. public SoftAuthorizeModel AuthorizeModel { get; set; }
  21. public SoftAuthDialog()
  22. {
  23. InitializeComponent();
  24. AuthorizeModel = new SoftAuthorizeModel() { MachineCode = SoftAuth.GetMachineCode() };
  25. DataContext = this;
  26. }
  27. private void Window_Loaded(object sender, RoutedEventArgs e)
  28. {
  29. if (AuthorizeModel.MachineCode != null)
  30. {
  31. QRCodeGenerator qrGenerator = new QRCodeGenerator();
  32. QRCodeData qrCodeData = qrGenerator.CreateQrCode(AuthorizeModel.MachineCode, QRCodeGenerator.ECCLevel.Q);
  33. QRCode qrCode = new QRCode(qrCodeData);
  34. Bitmap qrCodeImage = qrCode.GetGraphic(10);
  35. imgQRCode.Source = qrCodeImage.ConvertBitmapToBitmapImage();
  36. }
  37. }
  38. private void Copy_Click(object sender, RoutedEventArgs e)
  39. {
  40. Clipboard.SetDataObject(AuthorizeModel.MachineCode);
  41. }
  42. private void PasteExecuted(object sender, ExecutedRoutedEventArgs e)
  43. {
  44. IDataObject iData = Clipboard.GetDataObject();
  45. if (iData.GetDataPresent(DataFormats.Text))
  46. {
  47. var finalCode = (string)iData.GetData(DataFormats.Text);
  48. AuthorizeModel.FinalData.Set(finalCode);
  49. }
  50. e.Handled = true;
  51. }
  52. private void BtnSave_Click(object sender, RoutedEventArgs e)
  53. {
  54. DialogResult = SoftAuth.WriteRegistry(AuthorizeModel.FinalCode);
  55. }
  56. private void Fourth_TextChanged(object sender, TextChangedEventArgs e)
  57. {
  58. var fourth = sender as TextBox;
  59. if (fourth.Text.Length == fourth.MaxLength)
  60. {
  61. if (SoftAuth.CheckFinalCode(AuthorizeModel.FinalCode))
  62. {
  63. piCheckResult.Visibility = Visibility.Visible;
  64. btnSave.IsEnabled = true;
  65. return;
  66. }
  67. }
  68. piCheckResult.Visibility = Visibility.Hidden;
  69. btnSave.IsEnabled = false;
  70. }
  71. private void Card_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  72. {
  73. base.OnMouseLeftButtonDown(e);
  74. DragMove();
  75. }
  76. private void OnCodeTextBoxTextChanged(object sender, TextChangedEventArgs e)
  77. {
  78. TextBox textBox = sender as TextBox;
  79. if (textBox == null) return;
  80. if (AuthorizeModel.FinalCode.Length == 16)
  81. {
  82. Dispatcher.BeginInvoke(new Action(() =>
  83. {
  84. ValidateRegistrationCode();
  85. }), System.Windows.Threading.DispatcherPriority.Background);
  86. }
  87. else
  88. {
  89. piCheckResult.Visibility = Visibility.Hidden;
  90. btnSave.IsEnabled = false;
  91. }
  92. // 当输入框填满4个字符时,自动跳转到下一个
  93. if (textBox.Text.Length >= textBox.MaxLength)
  94. {
  95. if (textBox.Parent is StackPanel parent)
  96. {
  97. var textBoxes = parent.Children.OfType<TextBox>().ToList();
  98. var currentIndex = textBoxes.IndexOf(textBox);
  99. // 如果不是最后一个文本框,则跳转到下一个
  100. if (currentIndex >= 0 && currentIndex < textBoxes.Count - 1)
  101. {
  102. var nextTextBox = textBoxes[currentIndex + 1];
  103. nextTextBox.Focus();
  104. nextTextBox.SelectAll();
  105. }
  106. else if (currentIndex == textBoxes.Count - 1)
  107. {
  108. // 最后一个输入框填满后,延迟执行验证,避免UI卡顿
  109. Dispatcher.BeginInvoke(new Action(() =>
  110. {
  111. ValidateRegistrationCode();
  112. }), System.Windows.Threading.DispatcherPriority.Background);
  113. }
  114. }
  115. }
  116. }
  117. private async void ValidateRegistrationCode()
  118. {
  119. // 显示加载状态(可选)
  120. piCheckResult.Visibility = Visibility.Hidden;
  121. // 异步执行验证
  122. bool isValid = await Task.Run(() => SoftAuth.CheckFinalCode(AuthorizeModel.FinalCode));
  123. // 回到UI线程更新界面
  124. piCheckResult.Visibility = isValid ? Visibility.Visible : Visibility.Hidden;
  125. btnSave.IsEnabled = isValid;
  126. if (isValid)
  127. {
  128. piCheckResult.Foreground = new SolidColorBrush(Colors.LawnGreen);
  129. }
  130. }
  131. }
  132. }