using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; using DevExpress.XtraEditors; using ProjectBase.Controls; using System.IO; using SIMDP.BLL; using ProjectBase.Data.Encrypt; using ProjectBase.Data.Logs; using ProjectBase.Data.XML; using ProjectBase.Util; using ProjectBase.Data.BaseDAL; namespace SIMDP.View { public partial class FormLogIn : BaseDock { #region Delegate /// /// 开始登录代理 /// /// /// private delegate bool LoginBeginHandler(object connectPara); #endregion /// /// 用户ID /// private string userid = ""; //private string useridMD5 = ""; /// /// MD5加密后的密码 /// private string passwordMD5 = string.Empty; /// /// 用户登录记录文件名 /// private const string logonRecordsFileName = "LogonRecords.xml"; /// /// 用户登录记录文件全路径 /// private string logonRecordsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, logonRecordsFileName); /// /// 定义委托 /// public delegate void refresh(); /// /// 定义事件,刷新主窗口中的菜单栏控件 /// public event refresh myRefresh; /// /// 打开监控窗口 /// public event refresh showMonitorWindow; public FormLogIn() { InitializeComponent(); InitControls(); LoadLogonRecords(); this.btn_LogIn.TabIndex = 0; } /// /// 初始化控件 /// private void InitControls() { this.panelEnhanced1.Opacity = 0; this.panelEnhanced1.Controls.Add(this.panel1); Rectangle screenArea = System.Windows.Forms.Screen.GetWorkingArea(this); //这个区域不包含任务栏 int width = screenArea.Width; int height = screenArea.Height; Point point = new Point(); point.X = (width/2) - (this.panelEnhanced1.Width /2); point.Y = (height / 2) - (this.panelEnhanced1.Height / 2) - 50; this.panelEnhanced1.Location = point; } private void btn_LogIn_Click(object sender, EventArgs e) { if (txt_PassWord.Text.Trim() == "" || txt_Account.Text.Trim() == "") { DevExpress.XtraEditors.XtraMessageBox.Show("用户名或密码不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } try { userid = txt_Account.Text.Trim(); passwordMD5 = EncodeHelper.EncryptString(txt_PassWord.Text); string message = null; bool flag = BLLFactory.Instance.VerifyLogin(userid, passwordMD5, out message); if (flag) { SysEnvironment.CurrentLoginID = userid; SysEnvironment.passWordMD5 = passwordMD5; RecordLogonUser(); if (myRefresh != null) { myRefresh(); } if (showMonitorWindow != null) { showMonitorWindow(); } } else { DevExpress.XtraEditors.XtraMessageBox.Show(message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { LogHelper.log.Error(string.Format("登录出现错误:{0}", ex.ToString())); } finally { this.Close(); } } /// /// 加载用户登录记录信息 /// private void LoadLogonRecords() { try { if (File.Exists(logonRecordsPath)) { Permission permission = XmlHelper.XmlDeserialize(logonRecordsPath, typeof(Permission)) as Permission; if (string.IsNullOrEmpty(permission.userInfo.LoginID) && string.IsNullOrEmpty(permission.userInfo.LoginPassWord)) { this.check_PassWord.CheckState = CheckState.Unchecked; return; } this.txt_Account.Text = permission.userInfo.LoginID; this.txt_PassWord.Text = EncodeHelper.DecryptString(permission.userInfo.LoginPassWord, true); this.check_PassWord.CheckState = CheckState.Checked; } } catch (Exception ex) { LogHelper.log.Error(string.Format("登录出现错误:{0}", ex.ToString())); } } /// /// 记录登录用户ID /// private void RecordLogonUser() { if (check_PassWord.CheckState != CheckState.Checked) { if (File.Exists(logonRecordsPath)) { XmlHelper xml = new XmlHelper(logonRecordsPath); xml.Replace("/Permission/User/LoginID", ""); xml.Replace("/Permission/User/LoginPassWord", ""); xml.Save(); } } else if (check_PassWord.CheckState == CheckState.Checked) { if (File.Exists(logonRecordsPath)) { XmlHelper xml = new XmlHelper(logonRecordsPath); string account = xml.GetElementData("/Permission/User", "LoginID"); if (account != userid) { xml.Replace("/Permission/User/LoginID", userid); xml.Replace("/Permission/User/LoginPassWord", passwordMD5); xml.Save(); } else { xml.Replace("/Permission/User/LoginPassWord", passwordMD5); xml.Save(); } } else { User user = new User { LoginID = userid, LoginPassWord = passwordMD5 }; Permission permission = new Permission { userInfo = user }; bool flag = XmlHelper.XmlSerialize(logonRecordsPath, permission, typeof(Permission)); if (!flag) { DevExpress.XtraEditors.XtraMessageBox.Show("记住密码失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } } private void txt_Account_TextChanged(object sender, EventArgs e) { this.txt_PassWord.Text = null; this.check_PassWord.CheckState = CheckState.Unchecked; } } }