LoginForm.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Threading;
  6. using System.Windows.Forms;
  7. using DevExpress.XtraEditors;
  8. using GCAS.Code;
  9. using GCAS.Dto;
  10. using GCAS.Localization;
  11. using MySql.Data.MySqlClient;
  12. namespace GCAS
  13. {
  14. public partial class LoginForm : XtraForm
  15. {
  16. private List<NameValue> L;
  17. private OperatorRepository operatorRepository;
  18. private ActionsRepository actionsRepository;
  19. public LoginForm()
  20. {
  21. InitializeComponent();
  22. DoubleBuffered = true;
  23. L = LocalizationHelper.GetSource(Thread.CurrentThread.CurrentUICulture);
  24. }
  25. private void LoginForm_Load(object sender, EventArgs e)
  26. {
  27. actionsRepository = new ActionsRepository();
  28. operatorRepository = new OperatorRepository();
  29. panelControl1.ContentImage = Tools.GetLoginBackground();
  30. }
  31. private void btn_cancel_Click(object sender, EventArgs e)
  32. {
  33. Close();
  34. }
  35. private async void btn_login_Click(object sender, EventArgs e)
  36. {
  37. if (text_username.Text.Trim() == string.Empty)
  38. {
  39. XtraMessageBox.Show(L.GetString("userNameNotNull"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
  40. return;
  41. }
  42. // ValidServer();
  43. if (text_username.Text == "--recover" && text_password.Text == "!23Qwe")
  44. {
  45. var result = await actionsRepository.Recover();
  46. if (result > 0)
  47. {
  48. XtraMessageBox.Show("记录清除成功", "重要", MessageBoxButtons.OK, MessageBoxIcon.Information);
  49. }
  50. }
  51. var name = text_username.Text;
  52. var pwd = SHAHelper.SHAmd5Encrypt(text_password.Text);
  53. var user = await operatorRepository.Get(name, pwd);
  54. if (user == null)
  55. {
  56. XtraMessageBox.Show(L.GetString("userNameOrPasswordError"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
  57. return;
  58. }
  59. user.Language = Thread.CurrentThread.CurrentUICulture.Name;
  60. await actionsRepository.Insert(new Model.ActionsModel { Info = L.GetString("loginSystem"), Operator = user.Name, Time = DateTime.Now, RoleName = user.RoleName });
  61. using (MainForm form = new MainForm())
  62. {
  63. Hide();
  64. form.Operator = user;
  65. if (form.ShowDialog() == DialogResult.Retry)
  66. {
  67. Show();
  68. }
  69. Close();
  70. }
  71. }
  72. private void ValidServer()
  73. {
  74. string connString = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)?.ConnectionStrings?.ConnectionStrings["myconn"]?.ToString();
  75. if (string.IsNullOrEmpty(connString))
  76. {
  77. XtraMessageBox.Show(L.GetString("serverError"), L.GetString("exclamation"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  78. Hide();
  79. using (ServerSet form = new ServerSet())
  80. {
  81. if (form.ShowDialog() == DialogResult.OK)
  82. {
  83. Show();
  84. }
  85. else
  86. {
  87. Environment.Exit(0);
  88. }
  89. }
  90. }
  91. else
  92. {
  93. IDbConnection conn = new MySqlConnection(connString);
  94. try
  95. {
  96. conn.Open();
  97. }
  98. catch
  99. {
  100. XtraMessageBox.Show(L.GetString("serverError"), L.GetString("exclamation"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  101. Hide();
  102. using (ServerSet form = new ServerSet())
  103. {
  104. if (form.ShowDialog() == DialogResult.OK)
  105. {
  106. Show();
  107. }
  108. else
  109. {
  110. Environment.Exit(0);
  111. }
  112. }
  113. }
  114. finally
  115. {
  116. conn?.Close();
  117. conn.Dispose();
  118. }
  119. }
  120. }
  121. private void text_username_Click(object sender, EventArgs e)
  122. {
  123. SoftKeyboard.ActiveSoftKeyboard(sender);
  124. }
  125. private void text_password_Click(object sender, EventArgs e)
  126. {
  127. SoftKeyboard.ActiveSoftKeyboard(sender);
  128. }
  129. }
  130. }