Files
BR_YKC/上位机/YKC/LoginWindow.xaml.cs

67 lines
1.9 KiB
C#

using System;
using System.Windows;
using System.Windows.Input;
using Newtonsoft.Json.Linq;
namespace YKC
{
public partial class LoginWindow : Window
{
public LoginWindow()
{
InitializeComponent();
txtUsername.Focus();
KeyDown += (s, e) =>
{
if (e.Key == Key.Enter) Login();
};
}
private async void Login_Click(object sender, RoutedEventArgs e) => Login();
private async void Login()
{
string user = txtUsername.Text.Trim();
string pass = txtPassword.Password;
if (string.IsNullOrEmpty(user) || string.IsNullOrEmpty(pass))
{
txtError.Text = "请输入账号和密码";
return;
}
btnLogin.IsEnabled = false;
btnLogin.Content = "校验中...";
txtError.Text = "";
await System.Threading.Tasks.Task.Run(() =>
{
var result = UdpClientHolder.Instance.SendSync(new JObject
{
["cmd"] = Config.Cmd["LOGIN"],
["username"] = user,
["password"] = pass
});
Dispatcher.BeginInvoke(new Action(() =>
{
if (result.Value<bool>("success"))
{
var mainWin = new MainWindow();
mainWin.SetUser(user);
mainWin.Show();
Close();
}
else
{
txtError.Text = result.Value<string>("error") ?? "账号或密码错误";
btnLogin.IsEnabled = true;
btnLogin.Content = "登 录";
}
}));
});
}
}
}