AuthViewModel.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Controls.Shapes;
  4. using Avalonia.Controls;
  5. using Avalonia.Media;
  6. using Avalonia.Threading;
  7. using Avalonia;
  8. using ReactiveUI;
  9. namespace CaptchaAndSimpleCalculator.ViewModels
  10. {
  11. public class AuthViewModel : ReactiveObject
  12. {
  13. #region Ñâîéñòâà äëÿ ëîãèíà, ïàðîëÿ è ïðîâåðêà àâòîðèçàöèè
  14. string Lg;
  15. string Pw;
  16. string ChAuth;
  17. int erroneousAuth = 0;
  18. bool _TextBoxEnable = true;
  19. public bool TextBoxIsEnable { get => _TextBoxEnable; set => this.RaiseAndSetIfChanged(ref _TextBoxEnable, value); }
  20. public string Login { get => Lg; set => this.RaiseAndSetIfChanged(ref Lg, value); }
  21. public string Password { get => Pw; set => this.RaiseAndSetIfChanged(ref Pw, value); }
  22. public string CheckAuth { get => ChAuth; set => this.RaiseAndSetIfChanged(ref ChAuth, value); }
  23. public int ErroneousAuth { get => erroneousAuth; set => this.RaiseAndSetIfChanged(ref erroneousAuth, value); }
  24. #endregion
  25. #region Ãåíåðàöèÿ êàï÷è
  26. Canvas _Captcha;
  27. public Canvas Captcha { get => _Captcha; set => this.RaiseAndSetIfChanged(ref _Captcha, value); }
  28. public char rndLet()
  29. {
  30. Random rnd = new Random();
  31. int randomIndex = rnd.Next(0, 26);
  32. char letter = (char)('a' + randomIndex);
  33. return letter;
  34. }
  35. #region Ñâîéñòâà è ïåðåìåííûå äëÿ ýëåìåíòîâ êàï÷è
  36. bool _Button1Visible = true;
  37. bool _Button2Visible = false;
  38. bool _TextBoxVisible = false;
  39. public bool Button1IsVisible { get => _Button1Visible; set => this.RaiseAndSetIfChanged(ref _Button1Visible, value); }
  40. public bool Button2IsVisible { get => _Button2Visible; set => this.RaiseAndSetIfChanged(ref _Button2Visible, value); }
  41. public bool TextBoxISVisible { get => _TextBoxVisible; set => this.RaiseAndSetIfChanged(ref _TextBoxVisible, value); }
  42. string _Kod;
  43. public string Kod { get => _Kod; set => this.RaiseAndSetIfChanged(ref _Kod, value); }
  44. List<string> kodList = new List<string>();
  45. public List<string> KodList { get => kodList; set => kodList = this.RaiseAndSetIfChanged(ref kodList, value); }
  46. #endregion
  47. public void CreateCaptha()
  48. {
  49. ErroneousAuth++;
  50. Button1IsVisible = false;
  51. Random rnd = new Random();
  52. SolidColorBrush color1 = new SolidColorBrush(Color.FromRgb(
  53. Convert.ToByte(rnd.Next(151, 256)),
  54. Convert.ToByte(rnd.Next(151, 256)),
  55. Convert.ToByte(rnd.Next(151, 256)))
  56. );
  57. SolidColorBrush color2 = new SolidColorBrush(Color.FromRgb(
  58. Convert.ToByte(rnd.Next(102, 150)),
  59. Convert.ToByte(rnd.Next(102, 150)),
  60. Convert.ToByte(rnd.Next(102, 150)))
  61. );
  62. SolidColorBrush color3 = new SolidColorBrush(Color.FromRgb(
  63. Convert.ToByte(rnd.Next(0, 101)),
  64. Convert.ToByte(rnd.Next(0, 101)),
  65. Convert.ToByte(rnd.Next(0, 101)))
  66. );
  67. Canvas canvas = new Canvas()
  68. {
  69. Width = 400,
  70. Height = 300,
  71. Background = color3,
  72. };
  73. for (int i = 0; i < 50; i++)
  74. {
  75. Line line = new Line()
  76. {
  77. StartPoint = new Point(rnd.Next(400), rnd.Next(300)),
  78. EndPoint = new Point(rnd.Next(400), rnd.Next(300)),
  79. Stroke = color2,
  80. StrokeThickness = 2,
  81. };
  82. canvas.Children.Add(line);
  83. }
  84. double lastX = 10;
  85. double lastY = 100;
  86. for (int i = 0; i < 5; i++)
  87. {
  88. TextBlock number = new TextBlock()
  89. {
  90. Text = rnd.Next(10).ToString(),
  91. FontSize = rnd.Next(20, 30),
  92. Foreground = color1,
  93. Padding = new Avalonia.Thickness(lastX, lastY)
  94. };
  95. KodList.Add(number.Text);
  96. if (rnd.Next(2) == 0) number.FontStyle = FontStyle.Normal;
  97. else if (rnd.Next(2) == 1) number.FontStyle = FontStyle.Italic;
  98. else if (rnd.Next(2) == 2) number.FontWeight = FontWeight.Bold;
  99. lastX += number.FontSize + 10;
  100. lastY += rnd.Next(0, 50);
  101. TextBlock letter = new TextBlock()
  102. {
  103. Text = rndLet().ToString(),
  104. FontSize = rnd.Next(20, 30),
  105. Foreground = color1,
  106. Padding = new Avalonia.Thickness(lastX, lastY)
  107. };
  108. KodList.Add(letter.Text);
  109. if (rnd.Next(3) == 0) letter.FontStyle = FontStyle.Normal;
  110. else if (rnd.Next(3) == 1) letter.FontStyle = FontStyle.Italic;
  111. else if (rnd.Next(3) == 2) letter.FontWeight = FontWeight.Bold;
  112. lastX += number.FontSize + 10;
  113. lastY -= rnd.Next(0, 50);
  114. canvas.Children.Add(letter);
  115. canvas.Children.Add(number);
  116. }
  117. Border border = new Border
  118. {
  119. BorderBrush = Brushes.Black,
  120. BorderThickness = new Thickness(2),
  121. Width = 400,
  122. Height = 300
  123. };
  124. canvas.Children.Add(border);
  125. Captcha = canvas;
  126. TextBoxISVisible = true;
  127. Button2IsVisible = true;
  128. }
  129. #endregion
  130. #region Òàéìåð
  131. DispatcherTimer timer = new DispatcherTimer();
  132. public AuthViewModel()
  133. {
  134. timer.Interval = new TimeSpan(0, 0, 10);
  135. timer.Tick += new EventHandler(StopTimer);
  136. }
  137. public void StartTimer()
  138. {
  139. timer.Start();
  140. Kod = "";
  141. KodList.Clear();
  142. TextBoxISVisible = false;
  143. Button2IsVisible = false;
  144. Captcha.IsVisible = false;
  145. Login = "";
  146. Password = "";
  147. TextBoxIsEnable = false;
  148. }
  149. public void StopTimer(object sender, EventArgs e)
  150. {
  151. timer.Stop();
  152. TextBoxIsEnable = true;
  153. TextBoxISVisible = true;
  154. Button2IsVisible = true;
  155. Captcha.IsVisible = true;
  156. CreateCaptha();
  157. }
  158. #endregion
  159. }
  160. }