FormAutorize.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using System.Windows.Forms.Design;
  14. namespace ImpulseVision
  15. {
  16. public partial class FormAutorize : Form
  17. {
  18. public FormAutorize()
  19. {
  20. InitializeComponent();
  21. TbxLogin.Focus();
  22. TbxLogin.SelectionStart = 0;
  23. }
  24. #region <Переменные>
  25. SqlConnection SCon = new SqlConnection(Properties.Settings.Default.ImpulseVisionAppConnectionString);
  26. #endregion
  27. private void BtnLogin_Click(object sender, EventArgs e)
  28. {
  29. if(TbxLogin.Text.Trim() == string.Empty || TbxPassword.Text.Trim() == string.Empty)
  30. {
  31. MessageBox.Show("Введите учётные данные и повторите попытку!", "ImpulseVision", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  32. return;
  33. }
  34. BsUsers.Filter = $"Login = '{TbxLogin.Text.Trim()}' and Password = '{TbxPassword.Text.Trim()}'";
  35. if (BsUsers.Count == 0)
  36. {
  37. MessageBox.Show("Пользователя с таким логином или паролем не существует!", "ImpilseVision", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  38. return;
  39. }
  40. else
  41. {
  42. Notify.Visible = false;
  43. try
  44. {
  45. SCon.Open();
  46. string QueryLogining = $@"insert into HistoryLogin (StaffsID, TimeLogin)
  47. values ('{LblUserID.Text.Trim()}',GETDATE())";
  48. SqlCommand Cmd = new SqlCommand(QueryLogining, SCon);
  49. Cmd.ExecuteNonQuery();
  50. SCon.Close();
  51. }
  52. catch
  53. {
  54. MessageBox.Show("Сервер не отвечает..\r\nПовторите попытку позже", "ImpulseVision", MessageBoxButtons
  55. .OK, MessageBoxIcon.Exclamation);
  56. return;
  57. }
  58. if (LblRole.Text == "1")
  59. {
  60. this.Hide();
  61. FormMain FMain = new FormMain();
  62. FMain.BsAdministrator.Filter = BsUsers.Filter;
  63. FMain.ShowDialog();
  64. this.Show();
  65. }
  66. else
  67. {
  68. this.Hide();
  69. FormGuard FGuard = new FormGuard();
  70. FGuard.BsStaffs.Filter = BsUsers.Filter;
  71. FGuard.ShowDialog();
  72. this.Show();
  73. }
  74. Notify.Visible = true;
  75. TbxLogin.Text = string.Empty;
  76. TbxPassword.Text = string.Empty;
  77. GetLastTimeLogin();
  78. }
  79. }
  80. private void FormAutorize_FormClosing(object sender, FormClosingEventArgs e)
  81. {
  82. if(File.Exists(Application.StartupPath+"\\autorun.txt"))
  83. {
  84. this.WindowState = FormWindowState.Minimized;
  85. this.Hide();
  86. Notify.Visible = true;
  87. }
  88. else
  89. {
  90. Application.ExitThread();
  91. }
  92. }
  93. private void FormAutorize_Load(object sender, EventArgs e)
  94. {
  95. // TODO: This line of code loads data into the 'impulseVisionAppDataSet1.Staffs' table. You can move, or remove it, as needed.
  96. try
  97. {
  98. this.staffsTableAdapter.Fill(this.impulseVisionAppDataSet1.Staffs);
  99. }
  100. catch
  101. {
  102. MessageBox.Show("Проверьте подключение к сети интернет и повторите попытку!", "ImpulseVision", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  103. Application.ExitThread();
  104. }
  105. FormStart FStart = new FormStart();
  106. FStart.ShowDialog();
  107. ApplicationParameters application = new ApplicationParameters();
  108. ClearOldRecordInJournal(application.CountDay);
  109. GetLastTimeLogin();
  110. LblRole.Hide();
  111. LblUserID.Hide();
  112. if (File.Exists(Application.StartupPath + "\\autorun.txt"))
  113. {
  114. Notify.Visible = true;
  115. }
  116. }
  117. /// <summary>
  118. /// получение последнего времени входа
  119. /// </summary>
  120. private void GetLastTimeLogin()
  121. {
  122. string LastLogin = string.Empty;
  123. SCon.Open();
  124. string QueryLastLogin = $@"select Max(TimeLogin) as LastTimeLogin
  125. from HistoryLogin";
  126. SqlCommand Cmd = new SqlCommand(QueryLastLogin, SCon);
  127. SqlDataReader Res = Cmd.ExecuteReader();
  128. if(Res.HasRows)
  129. {
  130. Res.Read();
  131. LastLogin = "Последнее время входа: " + Res["LastTimeLogin"].ToString();
  132. }
  133. else
  134. {
  135. LastLogin = "Здесь будет отображаться последнее время входа в систему.";
  136. }
  137. SCon.Close();
  138. Card.TextDescrition = LastLogin;
  139. }
  140. /// <summary>
  141. /// удаление фотографий в журнале,
  142. /// которые старше указанного количества дней
  143. /// </summary>
  144. private void ClearOldRecordInJournal(int CountDay)
  145. {
  146. if (!Directory.Exists(Application.StartupPath + "\\Source\\log"))
  147. {
  148. MessageBox.Show("Файлы журнала не обнаружены!", "ImpulseVision", MessageBoxButtons.OK, MessageBoxIcon.Error);
  149. return;
  150. }
  151. DirectoryInfo Dir = new DirectoryInfo(Application.StartupPath + "\\Source\\log\\");
  152. FileInfo[] Files = Dir.GetFiles();
  153. string Path = string.Empty;
  154. foreach (var t in Files)
  155. {
  156. Path = Application.StartupPath + "\\Source\\log\\" + t;
  157. if (!File.Exists(Path))
  158. continue;
  159. string[] Parts = t.ToString().Split('_');
  160. try
  161. {
  162. DateTime Tm = DateTime.Parse(Parts[3]);
  163. TimeSpan Span = DateTime.Now - Tm;
  164. int Dif = Span.Days;
  165. if(Dif > CountDay)
  166. {
  167. File.Delete(Path);
  168. }
  169. }
  170. catch
  171. {
  172. continue;
  173. }
  174. }
  175. }
  176. /// <summary>
  177. /// рисование градиентной заливки на форме
  178. /// </summary>
  179. private void FormAutorize_Paint(object sender, PaintEventArgs e)
  180. {
  181. Point PtStart = new Point(0, 0);
  182. Point PtEnd = new Point(Width, Height);
  183. Color ColFirst = ColorTranslator.FromHtml("#0F2834");
  184. Color ColThird = ColorTranslator.FromHtml("#496870");
  185. //градиентный цвет
  186. LinearGradientBrush LGB = new LinearGradientBrush(PtStart, PtEnd, ColFirst, ColThird);
  187. Pen MyPen = new Pen(LGB);
  188. Rectangle Rect = new Rectangle(PtStart,new Size(PtEnd));
  189. e.Graphics.FillRectangle(LGB, Rect);
  190. }
  191. private void FormAutorize_Resize(object sender, EventArgs e)
  192. {
  193. Invalidate();
  194. }
  195. private void Notify_MouseClick(object sender, MouseEventArgs e)
  196. {
  197. if (this.WindowState == FormWindowState.Minimized)
  198. {
  199. this.ShowInTaskbar = true;
  200. this.Show();
  201. this.WindowState = FormWindowState.Normal;
  202. }
  203. else
  204. {
  205. this.WindowState = FormWindowState.Minimized;
  206. this.Hide();
  207. }
  208. Notify.Visible = true;
  209. }
  210. }
  211. }