From 9d1660270272180cd71147d6ddd53d1382643d2e Mon Sep 17 00:00:00 2001
From: wenxiangchao <wenxiangchao@ihengxin.com.cn>
Date: 星期一, 02 十一月 2026 18:00:14 +0800
Subject: [PATCH] merge: 并入远程 Initial commit 作为历史根节点
---
WPFProject/Login.xaml.cs | 202 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 202 insertions(+), 0 deletions(-)
diff --git a/WPFProject/Login.xaml.cs b/WPFProject/Login.xaml.cs
new file mode 100644
index 0000000..6dc77f1
--- /dev/null
+++ b/WPFProject/Login.xaml.cs
@@ -0,0 +1,202 @@
+锘縰sing HMI.Core.Entity;
+using HMI.Core.Setting;
+using HMI.Core.Util;
+using HMI.UI.DpUtil;
+using HMI.UI.Util;
+using log4net;
+using Panuon.WPF.UI;
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.IO;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Media.Imaging;
+using System.Windows.Threading;
+using WPFProject.Utils;
+
+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();
+ logoImage.Source = new BitmapImage(new Uri(Path.Combine(PathHelper.IconFolder,
+ SystemSetting.Instance.LoginLogoPath ?? "LoginLogo.png")));
+ }
+
+ private static readonly Encryptor encryptor = new Encryptor("aB3dE5fG7hI9jjK1lMnOpQrS2tUvWxYz",
+ "8Wq9RsT4vYx6ZuB1");
+
+ private void Button_Click(object sender, RoutedEventArgs e)
+ {
+ if (FormGroupHelper.ValidateForm(formPanel))
+ {
+ ButtonHelper.SetIsPending(BtnLogin, true);
+ string username = TbAccount.Text;
+ string password = PwdPassword.Password;
+ bool rememberMe = SwRememberMe.IsChecked == true;
+ Task.Run(() =>
+ {
+ DoLogin(username, password, rememberMe);
+ });
+ }
+ }
+
+ private List<PermissionData> SelectPermission(string username, SqliteHelper sqliteHelper, out List<PermissionData> allPermission)
+ {
+ List<PermissionData> data = new List<PermissionData>();
+ allPermission = new List<PermissionData>();
+ Dictionary<string, PermissionData> cache = new Dictionary<string, PermissionData>();
+ try
+ {
+ string sql = $@"SELECT DISTINCT m.* FROM menu AS m INNER JOIN role_menu_rel AS rrel ON m.code = rrel.menuCode
+INNER JOIN role AS r ON rrel.roleCode = r.code
+INNER JOIN user_role_rel AS urel ON r.code = urel.roleCode
+AND urel.username = '{username}' ORDER BY m.level,m.sort, m.id;";
+
+ DataTable dt = sqliteHelper.GetDataTable(sql);
+ if (dt != null)
+ {
+ foreach (DataRow dr in dt.Rows)
+ {
+ string path = Path.Combine(PathHelper.IconFolder, dr["iconPath"].ToString());
+ var menu = new PermissionData
+ {
+ Code = dr["code"].ToString(),
+ Name = dr["name"].ToString(),
+ Type = dr["type"].ToString(),
+ ParentCode = dr["parentCode"].ToString(),
+ DllPath = dr["dllPath"].ToString(),
+ ClassName = dr["className"].ToString(),
+ IconFont = dr["icon"].ToString()
+ };
+ cache[menu.Code] = menu;
+ allPermission.Add(menu);
+ if (!cache.ContainsKey(menu.ParentCode))
+ {
+ data.Add(menu);
+ }
+ else
+ {
+ cache[menu.ParentCode].Children.Add(menu);
+ }
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ //LoadMessageAppend?.Invoke(this, ex.Message);
+ }
+ return data;
+ }
+
+ private void DoLogin(string username, string password, bool rememberMe)
+ {
+ try
+ {
+ using (SqliteHelper helper = new SqliteHelper(PathHelper.DatabasePath))
+ {
+ string sql = $"SELECT id,code,username,password,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"));
+ }
+ List<PermissionData> permissions = SelectPermission(username, helper, out List<PermissionData> allPermission);
+ UserInfo user = new UserInfo(
+ dt.Rows[0]["id"].ToString(),
+ dt.Rows[0]["code"].ToString(),
+ dt.Rows[0]["username"].ToString(),
+ dt.Rows[0]["name"].ToString(),
+ permissions,
+ allPermission,
+ dt.Rows[0]["password"].ToString()
+ );
+
+ UserInfo.Login(user);
+ if (rememberMe)
+ {
+ // 璁颁綇鐧诲綍鎿嶄綔
+ string userToken =
+ JsonUtil.Serialize(
+ new UserInfo(null, user.Code, user.Username, user.Name, null, null,
+ dt.Rows[0]["password"].ToString()));
+ // 瀛樺偍鏁版嵁
+ BinUtil.SaveStrToBin(encryptor.Encrypt(userToken),
+ Path.Combine(PathHelper.DatabaseFolder, "user.bin"));
+ }
+
+ Dispatcher.Invoke(() =>
+ {
+ LoadingWindow loading = new LoadingWindow();
+ loading.Show();
+ this.Closed -= window_Closed;
+ 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);
+ });
+ }
+ }
+
+ /// <summary>
+ /// 娓叉煋瀹屾垚鍚庯紝灏濊瘯鑷姩鐧诲綍
+ /// </summary>
+ /// <param name="sender"></param>
+ /// <param name="e"></param>
+ private void WindowX_ContentRendered(object sender, EventArgs e)
+ {
+ string userToken = string.Empty;
+ if (BinUtil.LoadStrFromBin(ref userToken, Path.Combine(PathHelper.DatabaseFolder, "user.bin")))
+ {
+ userToken = encryptor.Decrypt(userToken);
+ UserInfo userInfo = JsonUtil.Deserialize<UserInfo>(userToken);
+ TbAccount.Text = userInfo.Username;
+ PwdPassword.Password = userInfo.Password;
+ SwRememberMe.IsChecked = true;
+ Button_Click(null, null);
+ }
+ }
+ private void window_Closed(object sender, EventArgs e)
+ {
+ Application.Current.Shutdown();
+ }
+
+ private void SwRememberMe_Unchecked(object sender, RoutedEventArgs e)
+ {
+ string path = Path.Combine(PathHelper.DatabaseFolder, "user.bin");
+ if (File.Exists(path))
+ {
+ File.Delete(path);
+ }
+ }
+ }
+}
--
Gitblit v1.9.1