MainWindowViewModel.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using Avalonia.Controls;
  2. using ReactiveUI;
  3. using Captcha.Views;
  4. using System.Dynamic;
  5. using Tmds.DBus.Protocol;
  6. using System.Collections.Generic;
  7. using System.Security.Cryptography.X509Certificates;
  8. using Avalonia.Controls.Shapes;
  9. using Avalonia.Media;
  10. using Avalonia;
  11. using System;
  12. using System.Windows.Input;
  13. using Avalonia.Data;
  14. using Avalonia.Threading;
  15. using System.Threading;
  16. namespace Captcha.ViewModels
  17. {
  18. public class MainWindowViewModel : ViewModelBase
  19. {
  20. UserControl us = new Autorize();
  21. public UserControl US
  22. {
  23. get => us;
  24. set => this.RaiseAndSetIfChanged(ref us, value);
  25. }
  26. public CalculatorViewModel CalcVM { get => calcVM; set => calcVM = value; }
  27. CalculatorViewModel calcVM = new CalculatorViewModel();
  28. public void ToCalc()
  29. {
  30. US = new Calculator();
  31. }
  32. DispatcherTimer distimer = new DispatcherTimer();
  33. string login = "";
  34. string password = "";
  35. string capcha = "";
  36. string check;
  37. int count = 0;
  38. bool visa = true;
  39. bool visc = false;
  40. bool on = true;
  41. public string Login
  42. {
  43. get => login;
  44. set => this.RaiseAndSetIfChanged(ref login, value);
  45. }
  46. public string Password
  47. {
  48. get => password;
  49. set => this.RaiseAndSetIfChanged(ref password, value);
  50. }
  51. public string Capcha
  52. {
  53. get => capcha;
  54. set => this.RaiseAndSetIfChanged(ref capcha, value);
  55. }
  56. Canvas can;
  57. public Canvas Can
  58. {
  59. get => can;
  60. set => this.RaiseAndSetIfChanged(ref can, value);
  61. }
  62. public void CreateCaptcha() // метод для создания Canvas, внутри которого рандомно генерируются линии
  63. {
  64. SolidColorBrush color = new SolidColorBrush(Color.FromRgb(155, 155, 155));
  65. Canvas canvas = new Canvas()
  66. {
  67. Width = 210,
  68. Height = 100,
  69. Background = color
  70. };
  71. Random rnd = new Random();
  72. TextBlock text = new TextBlock()
  73. {
  74. Text = Convert.ToString(rnd.Next(0, 9)),
  75. FontSize = 36,
  76. Foreground = Brushes.Black,
  77. Padding = new Thickness(20)
  78. };
  79. TextBlock text1 = new TextBlock()
  80. {
  81. Text = Convert.ToString(Convert.ToChar(rnd.Next(97, 123))),
  82. FontSize = 36,
  83. Foreground = Brushes.Black,
  84. Padding = new Thickness(40)
  85. };
  86. TextBlock text2 = new TextBlock()
  87. {
  88. Text = Convert.ToString(rnd.Next(0, 20)),
  89. FontSize = 36,
  90. Foreground = Brushes.Black,
  91. Padding = new Thickness(60,10),
  92. };
  93. TextBlock text3 = new TextBlock()
  94. {
  95. Text = Convert.ToString(Convert.ToChar(rnd.Next(97, 123))),
  96. FontSize = 36,
  97. Foreground = Brushes.Black,
  98. Padding = new Thickness(100,2)
  99. };
  100. TextBlock text4 = new TextBlock()
  101. {
  102. Text = Convert.ToString(rnd.Next(0, 9)),
  103. FontSize = 36,
  104. Foreground = Brushes.Black,
  105. Padding = new Thickness(130,10)
  106. };
  107. TextBlock text5 = new TextBlock()
  108. {
  109. Text = Convert.ToString(Convert.ToChar(rnd.Next(65, 91))),
  110. FontSize = 36,
  111. Foreground = Brushes.Black,
  112. Padding = new Thickness(150,1)
  113. };
  114. TextBlock text6 = new TextBlock()
  115. {
  116. Text = Convert.ToString(Convert.ToChar(rnd.Next(97, 123))),
  117. FontSize = 36,
  118. Foreground = Brushes.Black,
  119. Padding = new Thickness(170,-3)
  120. };
  121. canvas.Children.Add(text);
  122. canvas.Children.Add(text1);
  123. canvas.Children.Add(text2);
  124. canvas.Children.Add(text3);
  125. canvas.Children.Add(text4);
  126. canvas.Children.Add(text5);
  127. canvas.Children.Add(text6);
  128. check = text.Text + text1.Text + text2.Text + text3.Text + text4.Text + text5.Text + text6.Text;
  129. for (int i = 0; i <= 10; i++)
  130. {
  131. Line line = new Line()
  132. {
  133. StartPoint = new Point(rnd.Next(220), rnd.Next(100)),
  134. EndPoint = new Point(rnd.Next(220), rnd.Next(100)),
  135. Stroke = new SolidColorBrush(Color.FromRgb(Convert.ToByte(rnd.Next(0, 155)), Convert.ToByte(rnd.Next(0, 155)), Convert.ToByte(rnd.Next(0, 155)))),
  136. StrokeThickness = 2
  137. };
  138. canvas.Children.Add(line);
  139. }
  140. Can = canvas;
  141. }
  142. public bool VisibilityAuto
  143. {
  144. get => visa;
  145. set => this.RaiseAndSetIfChanged(ref visa, value);
  146. }
  147. public bool VisibilityCaptcha
  148. {
  149. get => visc;
  150. set => this.RaiseAndSetIfChanged(ref visc, value);
  151. }
  152. public bool Enabled
  153. {
  154. get => on;
  155. set => this.RaiseAndSetIfChanged(ref on, value);
  156. }
  157. public void Check()
  158. {
  159. if (Login != Models.UserClass.Login || Password != Models.UserClass.Login)
  160. {
  161. count+=1;
  162. }
  163. else
  164. {
  165. ToCalc();
  166. }
  167. if (count == 1)
  168. {
  169. VisibilityCaptcha = true;
  170. VisibilityAuto = false;
  171. CreateCaptcha();
  172. }
  173. }
  174. public void CheckCaptcha()
  175. {
  176. if (Capcha != check || Login != Models.UserClass.Login || Password != Models.UserClass.Login)
  177. {
  178. count++;
  179. }
  180. else
  181. {
  182. ToCalc();
  183. }
  184. if(count > 1)
  185. {
  186. Enabled = false;
  187. VisibilityCaptcha = false;
  188. Login = "";
  189. Password = "";
  190. Capcha = "";
  191. distimer.Interval = new TimeSpan(0, 0, 10);
  192. distimer.Tick += new EventHandler(TimerEnd);
  193. distimer.Start();
  194. }
  195. }
  196. public void TimerEnd(object sender, EventArgs e)
  197. {
  198. Enabled = true;
  199. CreateCaptcha();
  200. VisibilityCaptcha = true;
  201. distimer.Stop();
  202. }
  203. }
  204. }