FormLogIn.cs 8.3 KB

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