using HMI.Core.Entity;
|
using HMI.Core.Util;
|
using HMI.UI.DpUtil;
|
using HMI.UI.Util;
|
using log4net;
|
using Panuon.WPF.UI;
|
using System;
|
using System.Data;
|
using System.Threading.Tasks;
|
using System.Windows;
|
|
namespace WPFProject
|
{
|
public class LoginException : Exception
|
{
|
public LoginException(string message):base(message)
|
{
|
}
|
}
|
|
/// <summary>
|
/// Login.xaml 的交互逻辑
|
/// </summary>
|
public partial class Login : WindowX
|
{
|
private ILog log;
|
public Login()
|
{
|
log = LogManager.GetLogger("Login");
|
InitializeComponent();
|
}
|
|
private void Button_Click(object sender, RoutedEventArgs e)
|
{
|
if (FormGroupHelper.ValidateForm(formPanel))
|
{
|
ButtonHelper.SetIsPending(BtnLogin, true);
|
string username = TbAccount.Text;
|
string password = PwdPassword.Password;
|
Task.Run(() =>
|
{
|
DoLogin(username, password);
|
});
|
}
|
|
//try
|
//{
|
|
//}
|
//finally {
|
// ButtonHelper.SetIsPending(BtnLogin, false);
|
//}
|
|
}
|
|
private void DoLogin(string username, string password)
|
{
|
SqliteHelper helper = null;
|
try
|
{
|
helper = new SqliteHelper(PathHelper.DatabasePath);
|
string sql = $"SELECT id,username,name From user WHERE username='{username}' and password='{password}'";
|
DataTable dt = helper.GetDataTable(sql);
|
if (dt == null || dt.Rows.Count <= 0)
|
{
|
throw new LoginException(LangUtil.Get("App.ErrorUsernameOrPassword"));
|
}
|
UserInfo user = new UserInfo(
|
dt.Rows[0]["id"].ToString(),
|
dt.Rows[0]["username"].ToString(),
|
dt.Rows[0]["name"].ToString());
|
UserInfo.Login(user);
|
Dispatcher.Invoke(() =>
|
{
|
LoadingWindow loading = new LoadingWindow();
|
loading.Show();
|
this.Close();
|
});
|
}
|
catch (Exception ex)
|
{
|
if (!(ex is LoginException)) log.Error("Login Failed.", ex);
|
Dispatcher.Invoke(() =>
|
{
|
ToastHelper.Error(this, $"{LangUtil.Get("App.LoginFailed")}: {ex.Message}");
|
});
|
}
|
finally
|
{
|
Dispatcher.Invoke(() =>
|
{
|
ButtonHelper.SetIsPending(BtnLogin, false);
|
});
|
}
|
}
|
}
|
}
|