123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- 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
- /// <summary>
- /// 开始登录代理
- /// </summary>
- /// <param name="connectPara"></param>
- /// <returns></returns>
- private delegate bool LoginBeginHandler(object connectPara);
- #endregion
- /// <summary>
- /// 用户ID
- /// </summary>
- private string userid = "";
- //private string useridMD5 = "";
- /// <summary>
- /// MD5加密后的密码
- /// </summary>
- private string passwordMD5 = string.Empty;
- /// <summary>
- /// 用户登录记录文件名
- /// </summary>
- private const string logonRecordsFileName = "LogonRecords.xml";
- /// <summary>
- /// 用户登录记录文件全路径
- /// </summary>
- private string logonRecordsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, logonRecordsFileName);
- /// <summary>
- /// 定义委托
- /// </summary>
- public delegate void refresh();
- /// <summary>
- /// 定义事件,刷新主窗口中的菜单栏控件
- /// </summary>
- public event refresh myRefresh;
- /// <summary>
- /// 打开监控窗口
- /// </summary>
- public event refresh showMonitorWindow;
- public FormLogIn()
- {
- InitializeComponent();
- InitControls();
- LoadLogonRecords();
- this.btn_LogIn.TabIndex = 0;
- }
- /// <summary>
- /// 初始化控件
- /// </summary>
- 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<BlAuthoryUser>.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();
- }
- }
- /// <summary>
- /// 加载用户登录记录信息
- /// </summary>
- 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()));
- }
- }
- /// <summary>
- /// 记录登录用户ID
- /// </summary>
- 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;
- }
-
- }
- }
|