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 _LoginCommand; public DelegateCommand LogoutCommand => _LoginCommand ?? ( _LoginCommand = new DelegateCommand(OnRoleLogin) ); private Users loginUser=new Users(){ Role= Roles.操作员 }; public Users LoginUser { get => loginUser; set { SetProperty(ref loginUser, value); } } private List rolesList=new List(){ 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(); } 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 PasswordChangedCommand => new DelegateCommand(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 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; } } /// /// 将 SecureString 类型的数据转换为普通的字符串类型。 /// /// 要转换的 SecureString 对象。 /// 转换后的字符串,如果转换失败则返回空字符串。 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); } } } } }