using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Daniel.Liu.DAO;
using System.Xml;
//0
namespace TiesTest
{
///
/// 系统登陆
///
public partial class frmLogin : Form
{
LoginUserManage lum = new LoginUserManage();
public frmLogin()
{
InitializeComponent();
}
///
/// 用户输入验证
///
///
private bool UserInputCheck() //输入为空检查
{
// 保存登录名称
string loginName = this.txtUserID.Text.Trim();
// 保存用户密码
string userPwd = this.txtPassword.Text.Trim();
// 开始验证
if (string.IsNullOrEmpty(loginName))
{
this.toolTip.ToolTipIcon = ToolTipIcon.Info;
this.toolTip.ToolTipTitle = "登录提示";
Point showLocation = new Point(this.txtUserID.Location.X + this.txtUserID.Width,this.txtUserID.Location.Y);
this.toolTip.Show("请您输入登录名称!", this, showLocation, 5000);
this.txtUserID.Focus();
return false;
}
if (string.IsNullOrEmpty(userPwd))
{
this.toolTip.ToolTipIcon = ToolTipIcon.Info;
this.toolTip.ToolTipTitle = "登录提示";
Point showLocation = new Point(this.txtPassword.Location.X + this.txtPassword.Width,this.txtPassword.Location.Y);
this.toolTip.Show("请您输入用户密码!", this, showLocation, 5000);
this.txtPassword.Focus();
return false;
}
//else if (userPwd.Length < 6)
//{
// this.toolTip.ToolTipIcon = ToolTipIcon.Warning;
// this.toolTip.ToolTipTitle = "登录警告";
// Point showLocation = new Point(
// this.txtUserPwd.Location.X + this.txtUserPwd.Width,
// this.txtUserPwd.Location.Y);
// this.toolTip.Show("用户密码长度不能小于六位!", this, showLocation, 5000);
// this.txtUserPwd.Focus();
// return false;
//}
// 如果已通过以上所有验证则返回真
return true;
}
private void login()
{
string level = "";
if (lum.IsEnable(txtUserID.Text.Trim()) == false) //用户名没被禁用
{
MessageBox.Show(Common.UserLevel);
if (lum.CheckUserPassword(txtUserID.Text.Trim(), txtPassword.Text.Trim(), ref Common.UserLevel)) //验证账户和密码
//if (lum.CheckUserPassword(txtUserID.Text.Trim(), txtPassword.Text.Trim(), ref level)) //验证账户和密码
{
Common.UserID = txtUserID.Text.Trim();
Common.UserName = lum.GetLoginUserName(txtUserID.Text.Trim()); //获取和用户ID对应的用户名
WriteLoginUnitXML(); //将本次登陆的用户名与项目名称存起来
SysLog.AddOperateLog(Common.UserName, "系统登陆", "", Common.UserID +" "+ Common.UserName + " 用户进入系统", DateTime.Now.ToString());//写日志
this.DialogResult = DialogResult.OK;
}
else
{
MessageBox.Show("你输入的用户账号或用户密码有错误,请重输!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
return;
}
}
else
{
MessageBox.Show("此用户已禁用,无法登陆系统!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
return;
}
}
private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar.ToString() == "\r")
{
if (UserInputCheck()) // 如果通过输入验证,查空,不为空
{
login();
}
}
}
private void btnLogin_Click(object sender, EventArgs e)
{
if (UserInputCheck()) // 如果通过输入验证
{
login();
}
}
///
/// 绑定下拉列表框,通用方法
///
//public void cboDataBind(ComboBox obj)
//{
// LoginUserManage lum = new LoginUserManage();
// DataTable dtl = lum.GetLoginUserInfo();
// ListItem item;
// for (int i = 0; i < dtl.Rows.Count; i++)
// {
// item = new ListItem();
// item.Text = dtl.Rows[i]["USERNAME"].ToString();
// item.Value = dtl.Rows[i]["USERID"].ToString();
// obj.Items.Add(item);
// }
//}
private void frmLogin_Load(object sender, EventArgs e)
{
CommonDataConfig.ConnectionDefaultStr = System.Configuration.ConfigurationSettings.AppSettings["ConnStr"].ToString(); //初始化数据库连接字符串
GetLoginUnitXML(); //将上次登陆的用户与项目名称加载出来
}
private void btnCancel_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void lnkChangePassword_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
frmChangePassword frmcp = new frmChangePassword();
frmcp.UserID = txtUserID.Text.Trim();
frmcp.UserName = lum.GetLoginUserName(txtUserID.Text.Trim());
frmcp.ShowDialog();
}
///
/// 取出登陆用户名与项目名称
///
private void GetLoginUnitXML()
{
string fileName = Application.StartupPath + @"\HistoryLogin.xml";
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.Load(fileName);
XmlNode rootNode = myXmlDocument.DocumentElement;
//用户上次登陆账号
string userid = rootNode.ChildNodes[0].ChildNodes[0].Attributes["value"].Value;
txtUserID.Text = userid;
}
///
/// 将登陆用户与项目名称存入xml中
///
private void WriteLoginUnitXML()
{
string fileName = Application.StartupPath + @"\HistoryLogin.xml";
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.Load(fileName);
XmlNode rootNode = myXmlDocument.DocumentElement;
//用户上次登陆用户名
rootNode.ChildNodes[0].ChildNodes[0].Attributes["value"].Value = txtUserID.Text;
myXmlDocument.Save(Application.StartupPath + @"\HistoryLogin.xml");
}
}
}