上位机学习记录(3)编写用户登录模块
上位机学习记录(3)编写用户登录模块
(一)业务逻辑说明
FrmLogin界面的cmb_LoginName控件进行数据绑定,通过SysAdminService.GetAllAdminDB()获取到所有的用户信息
(二)界面初始化逻辑
登陆逻辑:
- Program.cs: 先调用FrmLogin界面,当FrmLogin完成登陆验证后返回ok,然后启动FrmMain主界面
- FrmLogin.cs:
- 将数据封装为Admin对象
- 传入SysAdminService方法
- 当SysAdminService查询,查询到返回对象;查询不到返回null
- FrmLogin检测Admin对象是否为null,当不为空,返回ok;为null,调用FrmConfirmSingle弹出错误对话框
登陆界面美化:
- 登陆页面使用无边框。
- 可以使用鼠标进行拖动
代码展示:
SysAdminService.cs
public class SysAdminService
{
/// <summary>
/// 返回所有的用户对象
/// </summary>
/// <returns>用户对象集合</returns>
public static List<SysAdmin> GetAllAdminDB()
{
return SqlSugarHelper.SqlSugarClient.Queryable<SysAdmin>().Where(c=>c.LoginName.ToLower()!="admin").ToList();
}
/// <summary>
/// 验证用户登录结果
/// </summary>
/// <param name="admin">用户对象</param>
/// <returns>用户对象</returns>
public static SysAdmin AdminLogin(SysAdmin admin)
{
var list= SqlSugarHelper.SqlSugarClient.Queryable<SysAdmin>().Where(c => c.LoginName==admin.LoginName&&c.LoginPwd==admin.LoginPwd).ToList();
return list.Count == 0 ? null : list[0];
}
}
SqlSugarService.cs
public class SqlSugarService
{
public static void SetConnectionString(string ConnectionString)
{
SqlSugarHelper.ConnectionString = ConnectionString;
}
}
SysAdmin.cs
public class SysAdmin
{
public int LoginID { get; set; }
public string LoginName { get; set; }
public string LoginPwd { get; set; }
public int Role { get; set; }
}
FrmLogin.cs (LoginPwd使用了md5加密)
public partial class FrmLogin : Form
{
public FrmLogin()
{
InitializeComponent();
}
private void FrmLogin_Load(object sender, EventArgs e)
{
this.cmb_LoginName.DataSource = SysAdminService.GetAllAdminDB();
this.cmb_LoginName.DisplayMember = "LoginName";
}
private void btn_Login_Click(object sender, EventArgs e)
{
//验证
if (this.txt_LoginPwd.Text.Trim().Length == 0)
{
new FrmConfirmSingle("登录提示", "请输入密码!") { TopMost = true }.ShowDialog();
this.txt_LoginPwd.Focus();
return;
}
//封装
SysAdmin objAdmin = new SysAdmin()
{
LoginName = this.cmb_LoginName.Text,
LoginPwd = AuthorizationHelper.Encrypt(this.txt_LoginPwd.Text.Trim())
};
objAdmin = SysAdminService.AdminLogin(objAdmin);
if (objAdmin == null)
{
new FrmConfirmSingle("登录提示", "用户名或密码错误!") { TopMost = true }.ShowDialog();
}
else
{
CommonMethod.sysAdmin = objAdmin;
this.DialogResult = DialogResult.OK;
}
}
private void MainPanel_DoubleClick(object sender, EventArgs e)
{
this.Close();
}
Point mPoint;
private void MainPanel_MouseDown(object sender, MouseEventArgs e)
{
mPoint = new Point(e.X, e.Y);
}
private void MainPanel_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
}
}
private void txt_LoginPwd_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.btn_Login_Click(null, null);
}
}
}