123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- using System;
- using System.Collections.Generic;
- using Avalonia.Controls.Shapes;
- using Avalonia.Controls;
- using Avalonia.Media;
- using Avalonia.Threading;
- using Avalonia;
- using ReactiveUI;
- namespace CaptchaAndSimpleCalculator.ViewModels
- {
- public class AuthViewModel : ReactiveObject
- {
-
- #region Ñâîéñòâà äëÿ ëîãèíà, ïàðîëÿ è ïðîâåðêà àâòîðèçàöèè
- string Lg;
- string Pw;
- string ChAuth;
- int erroneousAuth = 0;
- bool _TextBoxEnable = true;
- public bool TextBoxIsEnable { get => _TextBoxEnable; set => this.RaiseAndSetIfChanged(ref _TextBoxEnable, value); }
- public string Login { get => Lg; set => this.RaiseAndSetIfChanged(ref Lg, value); }
- public string Password { get => Pw; set => this.RaiseAndSetIfChanged(ref Pw, value); }
-
- public string CheckAuth { get => ChAuth; set => this.RaiseAndSetIfChanged(ref ChAuth, value); }
- public int ErroneousAuth { get => erroneousAuth; set => this.RaiseAndSetIfChanged(ref erroneousAuth, value); }
- #endregion
- #region Ãåíåðàöèÿ êàï÷è
- Canvas _Captcha;
- public Canvas Captcha { get => _Captcha; set => this.RaiseAndSetIfChanged(ref _Captcha, value); }
- public char rndLet()
- {
- Random rnd = new Random();
- int randomIndex = rnd.Next(0, 26);
- char letter = (char)('a' + randomIndex);
- return letter;
- }
- #region Ñâîéñòâà è ïåðåìåííûå äëÿ ýëåìåíòîâ êàï÷è
- bool _Button1Visible = true;
- bool _Button2Visible = false;
- bool _TextBoxVisible = false;
- public bool Button1IsVisible { get => _Button1Visible; set => this.RaiseAndSetIfChanged(ref _Button1Visible, value); }
- public bool Button2IsVisible { get => _Button2Visible; set => this.RaiseAndSetIfChanged(ref _Button2Visible, value); }
- public bool TextBoxISVisible { get => _TextBoxVisible; set => this.RaiseAndSetIfChanged(ref _TextBoxVisible, value); }
- string _Kod;
- public string Kod { get => _Kod; set => this.RaiseAndSetIfChanged(ref _Kod, value); }
- List<string> kodList = new List<string>();
- public List<string> KodList { get => kodList; set => kodList = this.RaiseAndSetIfChanged(ref kodList, value); }
-
- #endregion
- public void CreateCaptha()
- {
- ErroneousAuth++;
- Button1IsVisible = false;
- Random rnd = new Random();
- SolidColorBrush color1 = new SolidColorBrush(Color.FromRgb(
- Convert.ToByte(rnd.Next(151, 256)),
- Convert.ToByte(rnd.Next(151, 256)),
- Convert.ToByte(rnd.Next(151, 256)))
- );
- SolidColorBrush color2 = new SolidColorBrush(Color.FromRgb(
- Convert.ToByte(rnd.Next(102, 150)),
- Convert.ToByte(rnd.Next(102, 150)),
- Convert.ToByte(rnd.Next(102, 150)))
- );
- SolidColorBrush color3 = new SolidColorBrush(Color.FromRgb(
- Convert.ToByte(rnd.Next(0, 101)),
- Convert.ToByte(rnd.Next(0, 101)),
- Convert.ToByte(rnd.Next(0, 101)))
- );
- Canvas canvas = new Canvas()
- {
- Width = 400,
- Height = 300,
- Background = color3,
- };
- for (int i = 0; i < 50; i++)
- {
- Line line = new Line()
- {
- StartPoint = new Point(rnd.Next(400), rnd.Next(300)),
- EndPoint = new Point(rnd.Next(400), rnd.Next(300)),
- Stroke = color2,
- StrokeThickness = 2,
- };
- canvas.Children.Add(line);
- }
- double lastX = 10;
- double lastY = 100;
- for (int i = 0; i < 5; i++)
- {
- TextBlock number = new TextBlock()
- {
- Text = rnd.Next(10).ToString(),
- FontSize = rnd.Next(20, 30),
- Foreground = color1,
- Padding = new Avalonia.Thickness(lastX, lastY)
- };
- KodList.Add(number.Text);
- if (rnd.Next(2) == 0) number.FontStyle = FontStyle.Normal;
- else if (rnd.Next(2) == 1) number.FontStyle = FontStyle.Italic;
- else if (rnd.Next(2) == 2) number.FontWeight = FontWeight.Bold;
- lastX += number.FontSize + 10;
- lastY += rnd.Next(0, 50);
- TextBlock letter = new TextBlock()
- {
- Text = rndLet().ToString(),
- FontSize = rnd.Next(20, 30),
- Foreground = color1,
- Padding = new Avalonia.Thickness(lastX, lastY)
- };
- KodList.Add(letter.Text);
- if (rnd.Next(3) == 0) letter.FontStyle = FontStyle.Normal;
- else if (rnd.Next(3) == 1) letter.FontStyle = FontStyle.Italic;
- else if (rnd.Next(3) == 2) letter.FontWeight = FontWeight.Bold;
- lastX += number.FontSize + 10;
- lastY -= rnd.Next(0, 50);
- canvas.Children.Add(letter);
- canvas.Children.Add(number);
- }
- Border border = new Border
- {
- BorderBrush = Brushes.Black,
- BorderThickness = new Thickness(2),
- Width = 400,
- Height = 300
- };
- canvas.Children.Add(border);
- Captcha = canvas;
- TextBoxISVisible = true;
- Button2IsVisible = true;
- }
- #endregion
- #region Òàéìåð
- DispatcherTimer timer = new DispatcherTimer();
- public AuthViewModel()
- {
- timer.Interval = new TimeSpan(0, 0, 10);
- timer.Tick += new EventHandler(StopTimer);
- }
- public void StartTimer()
- {
- timer.Start();
- Kod = "";
- KodList.Clear();
- TextBoxISVisible = false;
- Button2IsVisible = false;
- Captcha.IsVisible = false;
- Login = "";
- Password = "";
- TextBoxIsEnable = false;
- }
- public void StopTimer(object sender, EventArgs e)
- {
- timer.Stop();
- TextBoxIsEnable = true;
- TextBoxISVisible = true;
- Button2IsVisible = true;
- Captcha.IsVisible = true;
- CreateCaptha();
- }
- #endregion
-
- }
- }
|