RoleLoginViewModel.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using DefaultEdit.Log4xml;
  2. using DefaultEdit.Model;
  3. using DefaultEdit.Views;
  4. using Prism.Commands;
  5. using Prism.Events;
  6. using Prism.Ioc;
  7. using Prism.Mvvm;
  8. using Prism.Regions;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Runtime.InteropServices;
  12. using System.Security;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Controls;
  17. using System.Windows.Media;
  18. using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
  19. using System.Xml.Linq;
  20. using DefaultEdit.Core;
  21. namespace DefaultEdit.ViewModels
  22. {
  23. public class RoleLoginViewModel : BindableBase
  24. {
  25. private Management _management;
  26. private IContainerProvider _container;
  27. private IEventAggregator _eventAggregator;
  28. private IRegionManager _regionManager;
  29. private Brush _brush= Brushes.Transparent;
  30. private string loginstr="";
  31. private DelegateCommand<RoleLoginView> _LoginCommand;
  32. public DelegateCommand<RoleLoginView> LogoutCommand =>
  33. _LoginCommand ?? ( _LoginCommand = new DelegateCommand<RoleLoginView>(OnRoleLogin) );
  34. private string _CurrRole="操作员";
  35. public RoleLoginViewModel(IContainerProvider container, IRegionManager regionManager, IEventAggregator eventAggregator)
  36. {
  37. _container = container;
  38. _eventAggregator = eventAggregator;
  39. _regionManager = regionManager;
  40. _management = _container.Resolve<Management>();
  41. }
  42. bool colorswitch=false;
  43. void OnRoleLogin(RoleLoginView view)
  44. {
  45. string userPassword= SecureStringToString(Password);
  46. if ( string.IsNullOrEmpty(CurrRole) || string.IsNullOrEmpty(userPassword) )
  47. {
  48. MessageBox.Show("请输入账户名或密码!");
  49. return;
  50. }
  51. var userdata =_management.HttpService.GetUserInfoByAccount(CurrRole, userPassword);
  52. if ( userdata.Success )
  53. {
  54. ///groups内的值满足以下条件才能够登录
  55. if ( userdata.Data.groups.Contains("光固化") || userdata.Data.roles.Contains("光固化操作员") || userdata.Data.roles.Contains("光固化主管") || userdata.Data.roles.Contains("矫治器主管") )
  56. {
  57. LogHelper.Info($"{CurrRole}登录成功");
  58. ///从返回的数据内获取用户ID,上传生产记录时需要用到
  59. _management.UserId = userdata.Data.userId;
  60. _management.UserName = userdata.Data.userName;
  61. L_Brush = Brushes.Green;
  62. Loginstr = "登录成功";
  63. Task task= Task.Run(() =>
  64. {
  65. Thread.Sleep(1000);
  66. Application.Current.Dispatcher.Invoke(new Action(() =>
  67. {
  68. view.Close();
  69. }));
  70. });
  71. }
  72. else
  73. {
  74. LogHelper.Info($"{CurrRole}无操作权限登录");
  75. //Log4netHelper.WriteInfo("无操作权限登录");
  76. if ( colorswitch )
  77. {
  78. colorswitch = false;
  79. L_Brush = Brushes.Red;
  80. }
  81. else
  82. {
  83. colorswitch = true;
  84. L_Brush = Brushes.OrangeRed;
  85. }
  86. Loginstr = "无操作权限登录";
  87. }
  88. }
  89. else
  90. {
  91. L_Brush = Brushes.Red;
  92. Loginstr = userdata.Code.ToString();
  93. //Log4netHelper.WriteInfo($"登录失败,{userdata.RawText}:{userdata.Code}");
  94. LogHelper.Info($"登录失败,{userdata.RawText}:{userdata.Code}");
  95. }
  96. }
  97. public DelegateCommand<object> PasswordChangedCommand => new DelegateCommand<object>(PasswordChanged);
  98. // 密码变量
  99. private SecureString _password;
  100. public SecureString Password
  101. {
  102. get
  103. {
  104. return _password;
  105. }
  106. set
  107. {
  108. // 如果新值与旧值不同
  109. if ( _password != value )
  110. {
  111. // 更新密码
  112. // 触发属性更改通知,通知UI层密码已更改
  113. SetProperty(ref _password, value);
  114. }
  115. }
  116. }
  117. public string CurrRole
  118. {
  119. get => _CurrRole;
  120. set
  121. {
  122. SetProperty(ref _CurrRole, value);
  123. }
  124. }
  125. public Brush L_Brush { get => _brush; set { SetProperty(ref _brush, value); } }
  126. public string Loginstr { get => loginstr; set { SetProperty(ref loginstr, value); } }
  127. private void PasswordChanged(object parameter)
  128. {
  129. var passwordBox = parameter as PasswordBox;
  130. if ( passwordBox != null )
  131. {
  132. // 设置 ViewModel 中的密码属性
  133. Password = passwordBox.SecurePassword;
  134. }
  135. L_Brush = Brushes.Transparent;
  136. Loginstr = "";
  137. }
  138. /// <summary>
  139. /// 将 SecureString 类型的数据转换为普通的字符串类型。
  140. /// </summary>
  141. /// <param name="secureString">要转换的 SecureString 对象。</param>
  142. /// <returns>转换后的字符串,如果转换失败则返回空字符串。</returns>
  143. private string SecureStringToString(SecureString secureString)
  144. {
  145. // 初始化指针
  146. IntPtr ptr = IntPtr.Zero;
  147. try
  148. {
  149. // 将 SecureString 转换为指针
  150. ptr = Marshal.SecureStringToGlobalAllocUnicode(secureString);
  151. if ( ptr != IntPtr.Zero )
  152. {
  153. // 将指针中的数据复制到一个普通的字符串
  154. return Marshal.PtrToStringUni(ptr);
  155. }
  156. else
  157. {
  158. return string.Empty;
  159. }
  160. }
  161. catch ( Exception ex )
  162. {
  163. // 处理异常
  164. Console.WriteLine($"转换 SecureString 出错:{ex.Message}");
  165. return string.Empty;
  166. }
  167. finally
  168. {
  169. // 清除内存中的敏感数据
  170. if ( ptr != IntPtr.Zero )
  171. {
  172. Marshal.ZeroFreeGlobalAllocUnicode(ptr);
  173. }
  174. }
  175. }
  176. }
  177. }