|
@@ -13,178 +13,18 @@ namespace Captcha.ViewModels
|
|
|
{
|
|
|
public class MainWindowViewModel : ViewModelBase
|
|
|
{
|
|
|
-#pragma warning disable CA1822 // Mark members as static
|
|
|
+ public static MainWindowViewModel Instance;
|
|
|
|
|
|
- DispatcherTimer timer = new DispatcherTimer();
|
|
|
+ UserControl _pageContent = new Page1();
|
|
|
public MainWindowViewModel()
|
|
|
{
|
|
|
-
|
|
|
- timer.Interval = new TimeSpan(0, 0, 10); // задание того, на какое кол-во времени будет запущен таймер
|
|
|
- timer.Tick += new EventHandler(timer_tick); // добавление объекту таймера событие, запускающего генерацию капчи (обработчик timer_tick реализован ниже)
|
|
|
- }
|
|
|
- private void timer_tick(object sender, EventArgs e)
|
|
|
- {
|
|
|
- StackVis = true;
|
|
|
- TextEnab = true;
|
|
|
- MessageCaptcha = "Введите символы с капчи:";
|
|
|
- CreateCaptha();
|
|
|
- timer.Stop();
|
|
|
- strCaptcha = "";
|
|
|
- }
|
|
|
-
|
|
|
- string login = "";
|
|
|
- string password = "";
|
|
|
- string message = "";
|
|
|
- string messageCaptcha = "";
|
|
|
- string itCaptcha = "";
|
|
|
- string strCaptcha = "";
|
|
|
- bool butVis = true;
|
|
|
- bool butCapVis = true;
|
|
|
- bool stackVis = false;
|
|
|
- bool textEnab = true;
|
|
|
-
|
|
|
- public string Login { get => login; set => this.RaiseAndSetIfChanged(ref login, value); }
|
|
|
- public string Message { get => message; set => this.RaiseAndSetIfChanged(ref message, value); }
|
|
|
- public string MessageCaptcha { get => messageCaptcha; set => this.RaiseAndSetIfChanged(ref messageCaptcha, value); }
|
|
|
- public string ItCaptcha { get => itCaptcha; set => this.RaiseAndSetIfChanged(ref itCaptcha, value); }
|
|
|
- public string Password { get => password; set => this.RaiseAndSetIfChanged(ref password, value); }
|
|
|
- public bool ButVis { get => butVis; set => this.RaiseAndSetIfChanged(ref butVis, value); }
|
|
|
- public bool ButCapVis { get => butCapVis; set => this.RaiseAndSetIfChanged(ref butCapVis, value); }
|
|
|
- public bool StackVis { get => stackVis; set => this.RaiseAndSetIfChanged(ref stackVis,value); }
|
|
|
- public bool TextEnab { get => textEnab; set => this.RaiseAndSetIfChanged(ref textEnab, value); }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public void Authorization()
|
|
|
- {
|
|
|
- if (login == ModelClass.Login && password == ModelClass.Password)
|
|
|
- {
|
|
|
- Message = "Авторизация прошла успешно";
|
|
|
-
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- StackVis = true;
|
|
|
- MessageCaptcha = "Введите символы с капчи:";
|
|
|
- Message = "Неверный логин или пароль";
|
|
|
- ButVis = false;
|
|
|
- CreateCaptha();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- Canvas _can;
|
|
|
-
|
|
|
- public Canvas Can
|
|
|
- {
|
|
|
- get => _can;
|
|
|
- set => this.RaiseAndSetIfChanged(ref _can, value);
|
|
|
+ Instance = this;
|
|
|
}
|
|
|
|
|
|
- public static string RandomString()
|
|
|
+ public UserControl PageContent
|
|
|
{
|
|
|
- string chars = "abcdefghijklmnopqrstuvwxyz0123456789";
|
|
|
- string stringChars = "";
|
|
|
- Random random = new Random();
|
|
|
-
|
|
|
- stringChars = stringChars + chars[random.Next(chars.Length)];
|
|
|
-
|
|
|
- return stringChars;
|
|
|
- }
|
|
|
-
|
|
|
- public void CreateCaptha()
|
|
|
- {
|
|
|
-
|
|
|
- Random rnd = new Random();
|
|
|
-
|
|
|
- Canvas canvas = new Canvas() // создаем размеры Canvas
|
|
|
- {
|
|
|
- Width = 400,
|
|
|
- Height = 400,
|
|
|
- Background = Brushes.White
|
|
|
- };
|
|
|
- // генерируем линии
|
|
|
- for (int i = 0; i < rnd.Next(7,10); i++)
|
|
|
- {
|
|
|
- switch (rnd.Next(3))
|
|
|
- {
|
|
|
- case 0:
|
|
|
- TextBlock number1 = new TextBlock()
|
|
|
- {
|
|
|
- Text = RandomString(),
|
|
|
- FontSize = 50,
|
|
|
- Foreground = new SolidColorBrush(Color.FromRgb(Convert.ToByte(rnd.Next(256)), Convert.ToByte(rnd.Next(256)), Convert.ToByte(rnd.Next(256)))),
|
|
|
- Padding = new Avalonia.Thickness(i * 50, rnd.Next(100, 300), 0, 0),
|
|
|
- FontStyle = FontStyle.Italic
|
|
|
- };
|
|
|
- strCaptcha = strCaptcha + number1.Text;
|
|
|
- canvas.Children.Add(number1);
|
|
|
- break;
|
|
|
- case 1:
|
|
|
- TextBlock number2 = new TextBlock()
|
|
|
- {
|
|
|
- Text = RandomString(),
|
|
|
- FontSize = 50,
|
|
|
- Foreground = new SolidColorBrush(Color.FromRgb(Convert.ToByte(rnd.Next(256)), Convert.ToByte(rnd.Next(256)), Convert.ToByte(rnd.Next(256)))),
|
|
|
- Padding = new Avalonia.Thickness(i * 50, rnd.Next(100, 300), 0, 0),
|
|
|
- FontWeight = FontWeight.Bold
|
|
|
- };
|
|
|
- strCaptcha = strCaptcha + number2.Text;
|
|
|
- canvas.Children.Add(number2);
|
|
|
- break;
|
|
|
- case 2:
|
|
|
- TextBlock number3 = new TextBlock()
|
|
|
- {
|
|
|
- Text = RandomString(),
|
|
|
- FontSize = 50,
|
|
|
- Foreground = new SolidColorBrush(Color.FromRgb(Convert.ToByte(rnd.Next(256)), Convert.ToByte(rnd.Next(256)), Convert.ToByte(rnd.Next(256)))),
|
|
|
- Padding = new Avalonia.Thickness(i * 40, rnd.Next(100, 300), 0, 0),
|
|
|
- FontWeight = FontWeight.Bold,
|
|
|
- FontStyle = FontStyle.Italic
|
|
|
- };
|
|
|
- strCaptcha = strCaptcha + number3.Text;
|
|
|
- canvas.Children.Add(number3);
|
|
|
- break;
|
|
|
- default:
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- for (int i = 0; i < 20; i++)
|
|
|
- {
|
|
|
- Line line = new Line()
|
|
|
- {
|
|
|
- StartPoint = new Avalonia.Point(rnd.Next(400), rnd.Next(400)),
|
|
|
- EndPoint = new Avalonia.Point(rnd.Next(400), rnd.Next(400)),
|
|
|
- Stroke = new SolidColorBrush(Color.FromRgb(Convert.ToByte(rnd.Next(256)), Convert.ToByte(rnd.Next(255)), Convert.ToByte(rnd.Next(255)))),
|
|
|
- StrokeThickness = 3
|
|
|
- };
|
|
|
-
|
|
|
- canvas.Children.Add(line);
|
|
|
- }
|
|
|
-
|
|
|
- Can = canvas; // передача созданного Canvas св-ву Canvas для отображения
|
|
|
- }
|
|
|
-
|
|
|
- public void Trying()
|
|
|
- {
|
|
|
- if (login == ModelClass.Login && password == ModelClass.Password && itCaptcha == strCaptcha)
|
|
|
- {
|
|
|
- Message = "Авторизация прошла успешно";
|
|
|
- StackVis = false;
|
|
|
-
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- Message = "Неверный логин, пароль или капча";
|
|
|
- StackVis = false;
|
|
|
- Login = "";
|
|
|
- Password = "";
|
|
|
- ItCaptcha = "";
|
|
|
- TextEnab = false;
|
|
|
- timer.Start();
|
|
|
- }
|
|
|
+ get => _pageContent;
|
|
|
+ set => this.RaiseAndSetIfChanged(ref _pageContent, value);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-#pragma warning restore CA1822 // Mark members as static
|
|
|
}
|