FormAutorize.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. this.staffsTableAdapter.Fill(this.impulseVisionAppDataSet1.Staffs);
  78. GetLastTimeLogin();
  79. }
  80. }
  81. private void FormAutorize_FormClosing(object sender, FormClosingEventArgs e)
  82. {
  83. if(File.Exists(Application.StartupPath+"\\autorun.txt"))
  84. {
  85. this.WindowState = FormWindowState.Minimized;
  86. this.Hide();
  87. Notify.Visible = true;
  88. }
  89. else
  90. {
  91. Application.ExitThread();
  92. }
  93. }
  94. private void FormAutorize_Load(object sender, EventArgs e)
  95. {
  96. // TODO: This line of code loads data into the 'impulseVisionAppDataSet1.Staffs' table. You can move, or remove it, as needed.
  97. try
  98. {
  99. this.staffsTableAdapter.Fill(this.impulseVisionAppDataSet1.Staffs);
  100. }
  101. catch
  102. {
  103. MessageBox.Show("Проверьте подключение к сети интернет и повторите попытку!", "ImpulseVision", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  104. Application.ExitThread();
  105. }
  106. FormStart FStart = new FormStart();
  107. FStart.ShowDialog();
  108. ApplicationParameters application = new ApplicationParameters();
  109. ClearOldRecordInJournal(application.CountDay);
  110. GetLastTimeLogin();
  111. LblRole.Hide();
  112. LblUserID.Hide();
  113. if (File.Exists(Application.StartupPath + "\\autorun.txt"))
  114. {
  115. Notify.Visible = true;
  116. }
  117. }
  118. /// <summary>
  119. /// получение последнего времени входа
  120. /// </summary>
  121. private void GetLastTimeLogin()
  122. {
  123. string LastLogin = string.Empty;
  124. SCon.Open();
  125. string QueryLastLogin = $@"select Max(TimeLogin) as LastTimeLogin
  126. from HistoryLogin";
  127. SqlCommand Cmd = new SqlCommand(QueryLastLogin, SCon);
  128. SqlDataReader Res = Cmd.ExecuteReader();
  129. if(Res.HasRows)
  130. {
  131. Res.Read();
  132. LastLogin = "Последнее время входа: " + Res["LastTimeLogin"].ToString();
  133. }
  134. else
  135. {
  136. LastLogin = "Здесь будет отображаться последнее время входа в систему.";
  137. }
  138. SCon.Close();
  139. Card.TextDescrition = LastLogin;
  140. }
  141. /// <summary>
  142. /// удаление фотографий в журнале,
  143. /// которые старше указанного количества дней
  144. /// </summary>
  145. private void ClearOldRecordInJournal(int CountDay)
  146. {
  147. if (!Directory.Exists(Application.StartupPath + "\\Source\\log"))
  148. {
  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. private void CbxShowPass_CheckedChanged(object sender, EventArgs e)
  211. {
  212. TbxPassword.UseSystemPasswordChar = !CbxShowPass.Checked;
  213. }
  214. }
  215. }