using DM_Weight.Finger; using DM_Weight.Models; using DM_Weight.msg; using DM_Weight.Port; using DM_Weight.util; using log4net; using log4net.Core; using log4net.Repository.Hierarchy; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Configuration; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DM_Weight.ViewModels { public class SecondCheckDialogViewModel : BindableBase, IRegionMemberLifetime, IDialogAware { public bool KeepAlive => false; private readonly ILog logger = LogManager.GetLogger(typeof(LoginWindowViewModel)); private string username; private string password; private Boolean _loginBtnEnable = true; IRegionManager _regionManager; private int loginMode = Convert.ToInt32(ConfigurationManager.AppSettings["loginMode"]?.ToString() ?? "1"); private string firstLogin = ConfigurationManager.AppSettings["firstLogin"]?.ToString() ?? "operator"; public bool SingleLogin { get => _commonClass.ReadAppSetting("loginMode") == "1"; //get => loginMode == 1; } public bool MultiLogin { //get => loginMode == 2; get => _commonClass.ReadAppSetting("loginMode") == "2"; } private FingerprintUtil _fingerprintUtil; private PortUtil _portUtil; public Boolean LoginBtnEnable { get { return _loginBtnEnable; } set { SetProperty(ref _loginBtnEnable, value); } } public string Password { get { return password; } set { SetProperty(ref password, value); } } public string Username { get { return username; } set { SetProperty(ref username, value); } } public bool DrawerPortMsg { get => !_portUtil.drawerSerial.IsOpen; } public bool CanBusPortMsg { get => _portUtil._canBusExsit && !_portUtil.canBusSerial.IsOpen; } private bool _fingerMsg; public bool FingerMsg { get => _fingerMsg; set => SetProperty(ref _fingerMsg, value); } private DelegateCommand? _loginCommand; private DelegateCommand? _exitCommand; public DelegateCommand LoginCommand => _loginCommand ??= new DelegateCommand(Login); public DelegateCommand ExitCommand => _exitCommand ??= new DelegateCommand(Exit); public string Title => throw new NotImplementedException(); IEventAggregator _eventAggregator; CommonClass _commonClass; public SecondCheckDialogViewModel(IEventAggregator eventAggregator, CommonClass commonClass, PortUtil portUtil) { _portUtil = portUtil; _eventAggregator = eventAggregator; _commonClass = commonClass; } void Login() { LoginBtnEnable = false; if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password)) { if (Username.Equals("hkcadmin") && Password.Equals("hkc123")) { ObservableCollection defaultAll = RoleManagerWindowViewModel.defaultAll; PremissionDm[] a = new PremissionDm[defaultAll.Count]; Array.Copy(defaultAll.ToArray(), 0, a, 0, defaultAll.Count); a[4].Children.Add(new PremissionDm() { Id = 54, PremissionName = "调试页面", PremissionPath = "DebugWindow", }); //添加参数,键值对格式 keys.Add("operator", new UserList() { UserName = Username, Nickname = "华康测试账号", Id = 9999, Role = new RoleDm() { Id = 9999, RoleName = "hkcadmin", Permissions = a.ToList() }, }); _regionManager.RequestNavigate("MainRegion", "HomeWindow", keys); } else { UserList userList = SqlSugarHelper.Db.Queryable() .Includes(u => u.Role) .InnerJoin((u, r) => u.RoleId == r.Id) .First(u => u.UserName == username && ConfigurationManager.AppSettings["machineId"].ToString().Equals(u.MachineId)); if (userList == null) { AlertMsg alertMsg = new AlertMsg { Message = "无此用户", Type = MsgType.ERROR }; _eventAggregator.GetEvent().Publish(alertMsg); Username = ""; Password = ""; } else if (userList.Role == null) { AlertMsg alertMsg = new AlertMsg { Message = "用户还未设置权限,请联系管理员", Type = MsgType.ERROR }; _eventAggregator.GetEvent().Publish(alertMsg); Username = ""; Password = ""; } else { if (userList.PassWord == MD5.GetMD5Hash(Password)) { SetUser(userList); } else { AlertMsg alertMsg = new AlertMsg { Message = "密码错误", Type = MsgType.ERROR }; _eventAggregator.GetEvent().Publish(alertMsg); Password = ""; } } } } else { AlertMsg alertMsg = new AlertMsg { Message = "请输入账号或密码", Type = MsgType.ERROR }; _eventAggregator.GetEvent().Publish(alertMsg); } LoginBtnEnable = true; } public void ConfirmNavigationRequest(NavigationContext navigationContext, Action continuationCallback) { continuationCallback(true); } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } private NavigationParameters keys = new NavigationParameters(); public event Action RequestClose; void SetUser(UserList user) { // 单人登录模式 if (SingleLogin) { //添加参数,键值对格式 keys.Add("operator", user); RequestClose?.Invoke(new DialogResult(ButtonResult.OK)); } // 双人登录模式 else { // 如果已经录入了发药人,已经有一个用户登录 if (keys.ContainsKey("operator")) { if (keys.GetValue("operator").Id != user.Id) { keys.Add("reviewer", user); RequestClose?.Invoke(new DialogResult(ButtonResult.OK)); } else { AlertMsg alertMsg = new AlertMsg { Message = "该发药人账号已登录,请输入不同账号", Type = MsgType.ERROR }; _eventAggregator.GetEvent().Publish(alertMsg); } } // 如果已经录入了审核人, 已经有一个用户登录 else if (keys.ContainsKey("reviewer")) { if (keys.GetValue("reviewer").Id != user.Id) { keys.Add("operator", user); RequestClose?.Invoke(new DialogResult(ButtonResult.OK)); } else { AlertMsg alertMsg = new AlertMsg { Message = "该审核人账号已登录,请输入不同账号", Type = MsgType.ERROR }; _eventAggregator.GetEvent().Publish(alertMsg); } } // 第一个用户登录 else { if (firstLogin.Equals("operator")) { keys.Add("operator", user); } else { keys.Add("reviewer", user); } Username = string.Empty; Password = string.Empty; } } } void Exit() { RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel)); } void LoginEvent(FingerprintMsg msg) { logger.Info(msg.ToString()); if (msg.Message.Equals("CONNECT")) { FingerMsg = !msg.Result; } if (LoginBtnEnable) { if (msg.Message.Equals("LOGIN")) { UserList userList = SqlSugarHelper.Db.Queryable() .Includes(u => u.Role) .First(u => u.Id == msg.Id); if (userList == null) { AlertMsg alertMsg = new AlertMsg { Message = "无此用户", Type = MsgType.ERROR }; _eventAggregator.GetEvent().Publish(alertMsg); Username = ""; Password = ""; } else if (userList.Role == null) { AlertMsg alertMsg = new AlertMsg { Message = "用户还未设置权限,请联系管理员", Type = MsgType.ERROR }; _eventAggregator.GetEvent().Publish(alertMsg); Username = ""; Password = ""; } else { SetUser(userList); } } } } public bool CanCloseDialog() { return true; } public void OnDialogClosed() { _eventAggregator.GetEvent().Unsubscribe(LoginEvent); } public void OnDialogOpened(IDialogParameters parameters) { FingerMsg = false; _eventAggregator.GetEvent().Subscribe(LoginEvent); } } }