MainWindowViewModel.cs 7.1 KB

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