RoleLoginViewModel.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Security;
  6. using System.Text;
  7. using Prism.Mvvm;
  8. using System.Threading.Tasks;
  9. using System.Windows.Controls;
  10. using Prism.Commands;
  11. using LampInspectionMachine.Log4xml;
  12. using LampInspectionMachine.Model;
  13. using Prism.Events;
  14. using Prism.Ioc;
  15. using Prism.Regions;
  16. using LampInspectionMachine.Views;
  17. using System.Windows.Media;
  18. using System.Threading;
  19. using System.Windows;
  20. namespace LampInspectionMachine.ViewModels
  21. {
  22. public class RoleLoginViewModel:BindableBase
  23. {
  24. private AppData _appData;
  25. private IContainerProvider _container;
  26. private IEventAggregator _eventAggregator;
  27. private IRegionManager _regionManager;
  28. private Brush _brush= Brushes.Transparent;
  29. private string loginstr="";
  30. private DelegateCommand<RoleLoginView> _LoginCommand;
  31. public DelegateCommand<RoleLoginView> LogoutCommand =>
  32. _LoginCommand ?? ( _LoginCommand = new DelegateCommand<RoleLoginView>(OnRoleLogin) );
  33. private Users loginUser=new Users(){ Role= Roles.操作员 };
  34. public Users LoginUser { get => loginUser; set { SetProperty(ref loginUser, value); } }
  35. private List<string> rolesList=new List<string>(){ Roles.操作员.ToString(),Roles.工程师.ToString(),Roles.管理员.ToString() };
  36. private string _CurrRole="操作员";
  37. public RoleLoginViewModel(IContainerProvider container, IRegionManager regionManager, IEventAggregator eventAggregator)
  38. {
  39. _container = container;
  40. _eventAggregator = eventAggregator;
  41. _regionManager = regionManager;
  42. _appData=_container.Resolve<AppData>();
  43. }
  44. void OnRoleLogin(RoleLoginView view)
  45. {
  46. loginUser.Password=SecureStringToString(Password);
  47. if ( loginUser.CheckUserLogin(loginUser, _appData) )
  48. {
  49. L_Brush = Brushes.Green;
  50. Loginstr = "登录成功";
  51. LogHelper.Info("登录成功");
  52. Task task= Task.Run(() =>
  53. {
  54. Thread.Sleep(1000);
  55. Application.Current.Dispatcher.Invoke(new Action(() =>
  56. {
  57. view.Close();
  58. }));
  59. });
  60. }
  61. else
  62. {
  63. L_Brush = Brushes.Red;
  64. Loginstr = "登录失败";
  65. Task task= Task.Run(() =>
  66. {
  67. Thread.Sleep(1000);
  68. L_Brush = Brushes.Transparent;
  69. Loginstr = "";
  70. });
  71. LogHelper.Info("登录失败");
  72. }
  73. }
  74. public DelegateCommand<object> PasswordChangedCommand => new DelegateCommand<object>(PasswordChanged);
  75. // 密码变量
  76. private SecureString _password;
  77. public SecureString Password
  78. {
  79. get
  80. {
  81. return _password;
  82. }
  83. set
  84. {
  85. // 如果新值与旧值不同
  86. if ( _password != value )
  87. {
  88. // 更新密码
  89. // 触发属性更改通知,通知UI层密码已更改
  90. SetProperty(ref _password, value);
  91. }
  92. }
  93. }
  94. public string CurrRole { get => _CurrRole;
  95. set
  96. {
  97. SetProperty(ref _CurrRole, value);
  98. LoginUser.Role = ( Roles ) Enum.Parse(typeof(Roles), CurrRole);
  99. }
  100. }
  101. public List<string> RolesList { get => rolesList; set { SetProperty(ref rolesList, value); } }
  102. public Brush L_Brush { get => _brush; set { SetProperty(ref _brush, value); } }
  103. public string Loginstr { get => loginstr; set { SetProperty(ref loginstr, value); } }
  104. private void PasswordChanged(object parameter)
  105. {
  106. var passwordBox = parameter as PasswordBox;
  107. if ( passwordBox != null )
  108. {
  109. // 设置 ViewModel 中的密码属性
  110. Password = passwordBox.SecurePassword;
  111. }
  112. }
  113. /// <summary>
  114. /// 将 SecureString 类型的数据转换为普通的字符串类型。
  115. /// </summary>
  116. /// <param name="secureString">要转换的 SecureString 对象。</param>
  117. /// <returns>转换后的字符串,如果转换失败则返回空字符串。</returns>
  118. private string SecureStringToString(SecureString secureString)
  119. {
  120. // 初始化指针
  121. IntPtr ptr = IntPtr.Zero;
  122. try
  123. {
  124. // 将 SecureString 转换为指针
  125. ptr = Marshal.SecureStringToGlobalAllocUnicode(secureString);
  126. if ( ptr != IntPtr.Zero )
  127. {
  128. // 将指针中的数据复制到一个普通的字符串
  129. return Marshal.PtrToStringUni(ptr);
  130. }
  131. else
  132. {
  133. return string.Empty;
  134. }
  135. }
  136. catch ( Exception ex )
  137. {
  138. // 处理异常
  139. Console.WriteLine($"转换 SecureString 出错:{ex.Message}");
  140. return string.Empty;
  141. }
  142. finally
  143. {
  144. // 清除内存中的敏感数据
  145. if ( ptr != IntPtr.Zero )
  146. {
  147. Marshal.ZeroFreeGlobalAllocUnicode(ptr);
  148. }
  149. }
  150. }
  151. }
  152. }