DHDHSoftware/DHSoftware/Views/LoginWindow.cs
2025-03-28 10:16:28 +08:00

123 lines
3.8 KiB
C#

using AntdUI;
using DHSoftware.Models;
using DHSoftware.Services;
namespace DHSoftware
{
public partial class LoginWindow : AntdUI.Window
{
public LoginWindow()
{
InitializeComponent();
// 关键设置:允许窗体优先接收按键事件
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;
internal static LoginWindow Instance
{
get
{
if (_instance == null || _instance.IsDisposed)
_instance = new LoginWindow();
return _instance;
}
}
private void Button_cancel_Click(object? sender, EventArgs e)
{
this.Dispose();
}
private void Button_ok_Click(object? sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(iptName.Text))
{
AntdUI.Message.warn(this, "用户名不能为空!", autoClose: 3);
return;
}
if (string.IsNullOrWhiteSpace(iptPwd.Text))
{
AntdUI.Message.warn(this, "密码不能为空!", autoClose: 3);
return;
}
if (AuthService.Login(iptName.Text, iptPwd.Text))
{
if (this.Owner is MainWindow parent)
{
List<string> UserPermissions = AuthService.GetUserPermissions();
// 检查当前用户是否有权限
if (AuthService.HasPermission("system:config"))
{
parent.ShowConfig = true;
}
else
{
parent.ShowConfig = false;
}
if (AuthService.HasPermission("system:loadscheme"))
{
parent.Loadscheme = true;
}
else
{
parent.Loadscheme = false;
}
if (AuthService.HasPermission("system:addscheme"))
{
parent.Addscheme = true;
}
else
{
parent.Addscheme = false;
}
if (AuthService.HasPermission("system:deletescheme"))
{
parent.Deleteschememe = true;
}
else
{
parent.Deleteschememe = false;
}
parent.LoginName = iptName.Text;
}
this.Dispose();
}
else
{
AntdUI.Message.warn(this, "用户名或密码错误,登录失败!", autoClose: 3);
}
}
private void LoginWindow_Load(object sender, EventArgs e)
{
}
}
}