alielie
2025-03-11 2e54b439c64e0adbb7e010243c1f33aa5fc5f503
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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);
                });
            }
        }
    }
}