| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- 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<RoleLoginView> _LoginCommand;
- public DelegateCommand<RoleLoginView> LogoutCommand =>
- _LoginCommand ?? ( _LoginCommand = new DelegateCommand<RoleLoginView>(OnRoleLogin) );
- private string _CurrRole="操作员";
- public RoleLoginViewModel(IContainerProvider container, IRegionManager regionManager, IEventAggregator eventAggregator)
- {
- _container = container;
- _eventAggregator = eventAggregator;
- _regionManager = regionManager;
- _management = _container.Resolve<Management>();
- }
- 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<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);
- }
- }
- 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 = "";
- }
- /// <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);
- }
- }
- }
- }
- }
|