using DefaultEdit.Log4xml; using DefaultEdit.Model; using DefaultEdit.Views; using Prism.Commands; using Prism.Events; using Prism.Ioc; using Prism.Mvvm; using Prism.Regions; using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Security; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel; using System.Xml.Linq; using DefaultEdit.Core; namespace DefaultEdit.ViewModels { public class RoleLoginViewModel : BindableBase { private Management _management; 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 string _CurrRole="操作员"; public RoleLoginViewModel(IContainerProvider container, IRegionManager regionManager, IEventAggregator eventAggregator) { _container = container; _eventAggregator = eventAggregator; _regionManager = regionManager; _management = _container.Resolve(); } bool colorswitch=false; void OnRoleLogin(RoleLoginView view) { string userPassword= SecureStringToString(Password); if ( string.IsNullOrEmpty(CurrRole) || string.IsNullOrEmpty(userPassword) ) { MessageBox.Show("请输入账户名或密码!"); return; } var userdata =_management.HttpService.GetUserInfoByAccount(CurrRole, userPassword); if ( userdata.Success ) { ///groups内的值满足以下条件才能够登录 if ( userdata.Data.groups.Contains("光固化") || userdata.Data.roles.Contains("光固化操作员") || userdata.Data.roles.Contains("光固化主管") || userdata.Data.roles.Contains("矫治器主管") ) { LogHelper.Info($"{CurrRole}登录成功"); ///从返回的数据内获取用户ID,上传生产记录时需要用到 _management.UserId = userdata.Data.userId; _management.UserName = userdata.Data.userName; L_Brush = Brushes.Green; Loginstr = "登录成功"; Task task= Task.Run(() => { Thread.Sleep(1000); Application.Current.Dispatcher.Invoke(new Action(() => { view.Close(); })); }); } else { LogHelper.Info($"{CurrRole}无操作权限登录"); //Log4netHelper.WriteInfo("无操作权限登录"); if ( colorswitch ) { colorswitch = false; L_Brush = Brushes.Red; } else { colorswitch = true; L_Brush = Brushes.OrangeRed; } Loginstr = "无操作权限登录"; } } else { L_Brush = Brushes.Red; Loginstr = userdata.Code.ToString(); //Log4netHelper.WriteInfo($"登录失败,{userdata.RawText}:{userdata.Code}"); LogHelper.Info($"登录失败,{userdata.RawText}:{userdata.Code}"); } } 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); } } 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; } L_Brush = Brushes.Transparent; Loginstr = ""; } /// /// 将 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); } } } } }