| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Security;
- using System.Text;
- using Prism.Mvvm;
- using System.Threading.Tasks;
- using System.Windows.Controls;
- using Prism.Commands;
- using LampInspectionMachine.Log4xml;
- using LampInspectionMachine.Model;
- using Prism.Events;
- using Prism.Ioc;
- using Prism.Regions;
- using LampInspectionMachine.Views;
- using System.Windows.Media;
- using System.Threading;
- using System.Windows;
- namespace LampInspectionMachine.ViewModels
- {
- public class RoleLoginViewModel:BindableBase
- {
- private AppData _appData;
- private IContainerProvider _container;
- private IEventAggregator _eventAggregator;
- private IRegionManager _regionManager;
- private Brush _brush= Brushes.Transparent;
- private string loginstr="";
- private DelegateCommand<RoleLoginView> _LoginCommand;
- public DelegateCommand<RoleLoginView> LogoutCommand =>
- _LoginCommand ?? ( _LoginCommand = new DelegateCommand<RoleLoginView>(OnRoleLogin) );
- private Users loginUser=new Users(){ Role= Roles.操作员 };
- public Users LoginUser { get => loginUser; set { SetProperty(ref loginUser, value); } }
- private List<string> rolesList=new List<string>(){ Roles.操作员.ToString(),Roles.工程师.ToString(),Roles.管理员.ToString() };
- private string _CurrRole="操作员";
- public RoleLoginViewModel(IContainerProvider container, IRegionManager regionManager, IEventAggregator eventAggregator)
- {
- _container = container;
- _eventAggregator = eventAggregator;
- _regionManager = regionManager;
- _appData=_container.Resolve<AppData>();
- }
- void OnRoleLogin(RoleLoginView view)
- {
- loginUser.Password=SecureStringToString(Password);
- if ( loginUser.CheckUserLogin(loginUser, _appData) )
- {
- L_Brush = Brushes.Green;
- Loginstr = "登录成功";
- LogHelper.Info("登录成功");
- Task task= Task.Run(() =>
- {
- Thread.Sleep(1000);
- Application.Current.Dispatcher.Invoke(new Action(() =>
- {
- view.Close();
- }));
- });
-
-
-
- }
- else
- {
- L_Brush = Brushes.Red;
- Loginstr = "登录失败";
- Task task= Task.Run(() =>
- {
- Thread.Sleep(1000);
- L_Brush = Brushes.Transparent;
- Loginstr = "";
- });
- LogHelper.Info("登录失败");
- }
- }
- public DelegateCommand<object> PasswordChangedCommand => new DelegateCommand<object>(PasswordChanged);
- // 密码变量
- private SecureString _password;
- public SecureString Password
- {
- get
- {
- return _password;
- }
- set
- {
- // 如果新值与旧值不同
- if ( _password != value )
- {
- // 更新密码
- // 触发属性更改通知,通知UI层密码已更改
- SetProperty(ref _password, value);
- }
- }
- }
- public string CurrRole { get => _CurrRole;
- set
- {
- SetProperty(ref _CurrRole, value);
- LoginUser.Role = ( Roles ) Enum.Parse(typeof(Roles), CurrRole);
- }
- }
- public List<string> RolesList { get => rolesList; set { SetProperty(ref rolesList, value); } }
- public Brush L_Brush { get => _brush; set { SetProperty(ref _brush, value); } }
- public string Loginstr { get => loginstr; set { SetProperty(ref loginstr, value); } }
- private void PasswordChanged(object parameter)
- {
- var passwordBox = parameter as PasswordBox;
- if ( passwordBox != null )
- {
- // 设置 ViewModel 中的密码属性
- Password = passwordBox.SecurePassword;
- }
- }
- /// <summary>
- /// 将 SecureString 类型的数据转换为普通的字符串类型。
- /// </summary>
- /// <param name="secureString">要转换的 SecureString 对象。</param>
- /// <returns>转换后的字符串,如果转换失败则返回空字符串。</returns>
- private string SecureStringToString(SecureString secureString)
- {
- // 初始化指针
- IntPtr ptr = IntPtr.Zero;
- try
- {
- // 将 SecureString 转换为指针
- ptr = Marshal.SecureStringToGlobalAllocUnicode(secureString);
- if ( ptr != IntPtr.Zero )
- {
- // 将指针中的数据复制到一个普通的字符串
- return Marshal.PtrToStringUni(ptr);
- }
- else
- {
- return string.Empty;
- }
- }
- catch ( Exception ex )
- {
- // 处理异常
- Console.WriteLine($"转换 SecureString 出错:{ex.Message}");
- return string.Empty;
- }
- finally
- {
- // 清除内存中的敏感数据
- if ( ptr != IntPtr.Zero )
- {
- Marshal.ZeroFreeGlobalAllocUnicode(ptr);
- }
- }
- }
- }
- }
|