FormLogIn.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using DevExpress.XtraEditors;
  11. using ProjectBase.Controls;
  12. using System.IO;
  13. using SIMDP.BLL;
  14. using ProjectBase.Data.Encrypt;
  15. using ProjectBase.Data.Logs;
  16. using ProjectBase.Data.XML;
  17. using ProjectBase.Util;
  18. using ProjectBase.Data.BaseDAL;
  19. namespace SIMDP.View
  20. {
  21. public partial class FormLogIn : BaseDock
  22. {
  23. #region Delegate
  24. /// <summary>
  25. /// 开始登录代理
  26. /// </summary>
  27. /// <param name="connectPara"></param>
  28. /// <returns></returns>
  29. private delegate bool LoginBeginHandler(object connectPara);
  30. #endregion
  31. /// <summary>
  32. /// 用户ID
  33. /// </summary>
  34. private string userid = "";
  35. //private string useridMD5 = "";
  36. /// <summary>
  37. /// MD5加密后的密码
  38. /// </summary>
  39. private string passwordMD5 = string.Empty;
  40. /// <summary>
  41. /// 用户登录记录文件名
  42. /// </summary>
  43. private const string logonRecordsFileName = "LogonRecords.xml";
  44. /// <summary>
  45. /// 用户登录记录文件全路径
  46. /// </summary>
  47. private string logonRecordsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, logonRecordsFileName);
  48. /// <summary>
  49. /// 定义委托
  50. /// </summary>
  51. public delegate void refresh();
  52. /// <summary>
  53. /// 定义事件,刷新主窗口中的菜单栏控件
  54. /// </summary>
  55. public event refresh myRefresh;
  56. /// <summary>
  57. /// 打开监控窗口
  58. /// </summary>
  59. public event refresh showMonitorWindow;
  60. public FormLogIn()
  61. {
  62. InitializeComponent();
  63. InitControls();
  64. LoadLogonRecords();
  65. this.btn_LogIn.TabIndex = 0;
  66. }
  67. /// <summary>
  68. /// 初始化控件
  69. /// </summary>
  70. private void InitControls()
  71. {
  72. this.panelEnhanced1.Opacity = 0;
  73. this.panelEnhanced1.Controls.Add(this.panel1);
  74. Rectangle screenArea = System.Windows.Forms.Screen.GetWorkingArea(this); //这个区域不包含任务栏
  75. int width = screenArea.Width;
  76. int height = screenArea.Height;
  77. Point point = new Point();
  78. point.X = (width/2) - (this.panelEnhanced1.Width /2);
  79. point.Y = (height / 2) - (this.panelEnhanced1.Height / 2) - 50;
  80. this.panelEnhanced1.Location = point;
  81. }
  82. private void btn_LogIn_Click(object sender, EventArgs e)
  83. {
  84. if (txt_PassWord.Text.Trim() == "" || txt_Account.Text.Trim() == "")
  85. {
  86. DevExpress.XtraEditors.XtraMessageBox.Show("用户名或密码不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  87. return;
  88. }
  89. try
  90. {
  91. userid = txt_Account.Text.Trim();
  92. passwordMD5 = EncodeHelper.EncryptString(txt_PassWord.Text);
  93. string message = null;
  94. bool flag = BLLFactory<BlAuthoryUser>.Instance.VerifyLogin(userid, passwordMD5, out message);
  95. if (flag)
  96. {
  97. SysEnvironment.CurrentLoginID = userid;
  98. SysEnvironment.passWordMD5 = passwordMD5;
  99. RecordLogonUser();
  100. if (myRefresh != null)
  101. {
  102. myRefresh();
  103. }
  104. if (showMonitorWindow != null)
  105. {
  106. showMonitorWindow();
  107. }
  108. }
  109. else
  110. {
  111. DevExpress.XtraEditors.XtraMessageBox.Show(message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  112. }
  113. }
  114. catch (Exception ex)
  115. {
  116. LogHelper.log.Error(string.Format("登录出现错误:{0}", ex.ToString()));
  117. }
  118. finally
  119. {
  120. this.Close();
  121. }
  122. }
  123. /// <summary>
  124. /// 加载用户登录记录信息
  125. /// </summary>
  126. private void LoadLogonRecords()
  127. {
  128. try
  129. {
  130. if (File.Exists(logonRecordsPath))
  131. {
  132. Permission permission = XmlHelper.XmlDeserialize(logonRecordsPath, typeof(Permission)) as Permission;
  133. if (string.IsNullOrEmpty(permission.userInfo.LoginID) && string.IsNullOrEmpty(permission.userInfo.LoginPassWord))
  134. {
  135. this.check_PassWord.CheckState = CheckState.Unchecked;
  136. return;
  137. }
  138. this.txt_Account.Text = permission.userInfo.LoginID;
  139. this.txt_PassWord.Text = EncodeHelper.DecryptString(permission.userInfo.LoginPassWord, true);
  140. this.check_PassWord.CheckState = CheckState.Checked;
  141. }
  142. }
  143. catch (Exception ex)
  144. {
  145. LogHelper.log.Error(string.Format("登录出现错误:{0}", ex.ToString()));
  146. }
  147. }
  148. /// <summary>
  149. /// 记录登录用户ID
  150. /// </summary>
  151. private void RecordLogonUser()
  152. {
  153. if (check_PassWord.CheckState != CheckState.Checked)
  154. {
  155. if (File.Exists(logonRecordsPath))
  156. {
  157. XmlHelper xml = new XmlHelper(logonRecordsPath);
  158. xml.Replace("/Permission/User/LoginID", "");
  159. xml.Replace("/Permission/User/LoginPassWord", "");
  160. xml.Save();
  161. }
  162. }
  163. else if (check_PassWord.CheckState == CheckState.Checked)
  164. {
  165. if (File.Exists(logonRecordsPath))
  166. {
  167. XmlHelper xml = new XmlHelper(logonRecordsPath);
  168. string account = xml.GetElementData("/Permission/User", "LoginID");
  169. if (account != userid)
  170. {
  171. xml.Replace("/Permission/User/LoginID", userid);
  172. xml.Replace("/Permission/User/LoginPassWord", passwordMD5);
  173. xml.Save();
  174. }
  175. else
  176. {
  177. xml.Replace("/Permission/User/LoginPassWord", passwordMD5);
  178. xml.Save();
  179. }
  180. }
  181. else
  182. {
  183. User user = new User { LoginID = userid, LoginPassWord = passwordMD5 };
  184. Permission permission = new Permission { userInfo = user };
  185. bool flag = XmlHelper.XmlSerialize(logonRecordsPath, permission, typeof(Permission));
  186. if (!flag)
  187. {
  188. DevExpress.XtraEditors.XtraMessageBox.Show("记住密码失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  189. }
  190. }
  191. }
  192. }
  193. private void txt_Account_TextChanged(object sender, EventArgs e)
  194. {
  195. this.txt_PassWord.Text = null;
  196. this.check_PassWord.CheckState = CheckState.Unchecked;
  197. }
  198. }
  199. }