SoftAuthDialog.xaml.cs 5.2 KB

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