138 lines
4.3 KiB
C#
138 lines
4.3 KiB
C#
using AntdUI;
|
|
using DH.RBAC.Logic.Sys;
|
|
using DH.RBAC.Model.Sys;
|
|
using DH.RBAC.Utility.Other;
|
|
using DHSoftware.Models;
|
|
using DHSoftware.Services;
|
|
|
|
namespace DHSoftware
|
|
{
|
|
public partial class LoginWindow : AntdUI.Window
|
|
{
|
|
private SysUserLogic userlogic;
|
|
private SysUserLogOnLogic userLogOnLogic;
|
|
private SysPermissionLogic permissionLogic;
|
|
public LoginWindow()
|
|
{
|
|
InitializeComponent();
|
|
userlogic = new SysUserLogic();
|
|
userLogOnLogic = new SysUserLogOnLogic();
|
|
permissionLogic = new SysPermissionLogic();
|
|
// 关键设置:允许窗体优先接收按键事件
|
|
this.KeyPreview = true;
|
|
|
|
// 绑定按键事件
|
|
this.KeyDown += Login_KeyDown;
|
|
button_ok.Click += Button_ok_Click;
|
|
button_cancel.Click += Button_cancel_Click;
|
|
}
|
|
|
|
|
|
private void Login_KeyDown(object? sender, KeyEventArgs e)
|
|
{
|
|
// 监听回车键
|
|
if (e.KeyCode == Keys.Enter)
|
|
{
|
|
button_ok.PerformClick(); // 触发确定按钮的点击事件
|
|
e.Handled = true; // 阻止其他控件处理该按键
|
|
}
|
|
|
|
// 监听 ESC 键
|
|
if (e.KeyCode == Keys.Escape)
|
|
{
|
|
button_cancel.PerformClick(); // 触发取消按钮的点击事件
|
|
e.Handled = true;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 窗体对象实例
|
|
/// </summary>
|
|
private static LoginWindow _instance;
|
|
|
|
public static LoginWindow Instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance == null || _instance.IsDisposed)
|
|
_instance = new LoginWindow();
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
public MainWindow parentForm;
|
|
private void Button_cancel_Click(object? sender, EventArgs e)
|
|
{
|
|
DialogResult = DialogResult.Cancel;
|
|
this.Dispose();
|
|
}
|
|
|
|
private void Button_ok_Click(object? sender, EventArgs e)
|
|
{
|
|
string userName = iptName.Text;
|
|
string password = iptPwd.Text;
|
|
if (StringHelper.IsNullOrEmpty(userName))
|
|
{
|
|
AntdUI.Message.warn(this, "请输入用户名!", autoClose: 3);
|
|
|
|
return;
|
|
}
|
|
if (StringHelper.IsNullOrEmpty(password))
|
|
{
|
|
AntdUI.Message.warn(this, "请输入密码!", autoClose: 3);
|
|
|
|
|
|
return;
|
|
}
|
|
password = password.MD5Encrypt();
|
|
var userEntity = userlogic.GetByUserName(userName);
|
|
if (userEntity == null)
|
|
{
|
|
AntdUI.Message.warn(this, "该账户不存在,请重新输入!", autoClose: 3);
|
|
|
|
|
|
return;
|
|
}
|
|
if (!userEntity.IsEnabled)
|
|
{
|
|
AntdUI.Message.warn(this, "该账户已被禁用,请联系管理员!", autoClose: 3);
|
|
|
|
|
|
return;
|
|
}
|
|
var userLogOnEntity = userLogOnLogic.GetByAccount(userEntity.Id);
|
|
string inputPassword = password.DESEncrypt(userLogOnEntity.SecretKey).MD5Encrypt();
|
|
if (inputPassword != userLogOnEntity.Password)
|
|
{
|
|
AntdUI.Message.warn(this, "密码错误,请重新输入!", autoClose: 3);
|
|
|
|
|
|
return;
|
|
}
|
|
userLogOnLogic.UpdateLogin(userLogOnEntity);
|
|
|
|
DH.RBAC.Common.GlobalConfig.CurrentUser = userEntity;
|
|
List<SysPermission> list;
|
|
if (userlogic.ContainsUser("admin", DH.RBAC.Common.GlobalConfig.CurrentUser.Id))
|
|
{
|
|
list = permissionLogic.GetList();
|
|
}
|
|
else
|
|
{
|
|
list = permissionLogic.GetList(DH.RBAC.Common.GlobalConfig.CurrentUser.Id);
|
|
}
|
|
if (list.IsNullOrEmpty())
|
|
{
|
|
AntdUI.Message.warn(this, "网络或服务器异常,请稍后重试!", autoClose: 3);
|
|
|
|
return;
|
|
}
|
|
DH.RBAC.Common.GlobalConfig.PermissionList = list;
|
|
DialogResult = DialogResult.OK;
|
|
parentForm.ButtonPermissionList = DH.RBAC.Common.GlobalConfig.PermissionList.ToList();
|
|
this.Dispose();
|
|
}
|
|
|
|
|
|
}
|
|
} |