XiangTan_DM/DM_Weight/ViewModels/LoginWindowViewModel.cs

480 lines
19 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using log4net;
using log4net.Core;
using log4net.Repository.Hierarchy;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using DM_Weight.Finger;
using DM_Weight.Models;
using DM_Weight.msg;
using DM_Weight.Port;
using DM_Weight.util;
using DM_Weight.Views;
using System.Collections.ObjectModel;
using Microsoft.Win32;
using System.Xml;
using System.Diagnostics;
using DM_Weight.HIKVISION;
using System.Security.AccessControl;
namespace DM_Weight.ViewModels
{
public class LoginWindowViewModel : BindableBase, IRegionMemberLifetime, IConfirmNavigationRequest
{
private readonly ILog logger = LogManager.GetLogger(typeof(LoginWindowViewModel));
private string username;
private string password;
private Boolean _loginBtnEnable = true;
IRegionManager _regionManager;
IEventAggregator _eventAggregator;
private int loginMode = Convert.ToInt32(ConfigurationManager.AppSettings["loginMode"]?.ToString() ?? "1");
private string firstLogin = ConfigurationManager.AppSettings["firstLogin"]?.ToString() ?? "operator";
public bool SingleLogin
{
get => ReadAppSetting("loginMode") == "1";
//get => loginMode == 1;
}
public bool MultiLogin
{
//get => loginMode == 2;
get => ReadAppSetting("loginMode") == "2";
}
public string LoginUserCheck
{
get => ReadAppSetting("loginUser");
}
private HkcChangeShifts listHkcChangeShifts = new HkcChangeShifts();
private FingerprintUtil _fingerprintUtil;
private PortUtil _portUtil;
//private CHKFunction _chkFunction;
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 UserList Operator { get; set; }
public UserList Reviewer { get; set; }
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);
}
//public bool FridgePortMsg
//{
// get => !_portUtil.fridgeSerial.IsOpen;
//}
//录像机登录状态
//private bool _hikMsg;
//public bool HIKMsg
//{
// get=>_hikMsg;
// set=>SetProperty(ref _hikMsg, value);
//}
//public LoginWindowViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, PortUtil portUtil, FingerprintUtil fingerprintUtil)
//{
// _fingerprintUtil = fingerprintUtil;
// _portUtil = portUtil;
// _regionManager = regionManager;
// _eventAggregator = eventAggregator;
//}
public LoginWindowViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, PortUtil portUtil, FingerprintUtil fingerprintUtil)
{
_fingerprintUtil = fingerprintUtil;
_portUtil = portUtil;
//_chkFunction= chcFunction;
_regionManager = regionManager;
_eventAggregator = eventAggregator;
//_eventAggregator.GetEvent<FingerprintEvent>().Subscribe(LoginEvent);
}
private DelegateCommand? _loginCommand;
private DelegateCommand? _exitCommand;
public DelegateCommand LoginCommand =>
_loginCommand ??= new DelegateCommand(Login);
public DelegateCommand ExitCommand =>
_exitCommand ??= new DelegateCommand(Exit);
public bool KeepAlive => false;
void Login()
{
LoginBtnEnable = false;
if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
{
if (Username.Equals("hkcadmin") && Password.Equals("hkc123"))
{
ObservableCollection<PremissionDm> 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<UserList>()
.Includes<RoleDm>(u => u.Role)
.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<SnackbarEvent>().Publish(alertMsg);
Username = "";
Password = "";
}
else if (userList.Role == null || userList.Role.Permissions.Count <= 0)
{
AlertMsg alertMsg = new AlertMsg
{
Message = "用户还未设置权限,请联系管理员",
Type = MsgType.ERROR
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
Username = "";
Password = "";
}
else
{
//1仅当班人、审核人可登录
if (LoginUserCheck.Equals("1"))
{
//检查当前登录人是否是值班人 (审核人与发药人)
listHkcChangeShifts = SqlSugarHelper.Db.Queryable<HkcChangeShifts>()
.Where(cs => cs.State == "0").First();
if (listHkcChangeShifts != null)
{
if (!listHkcChangeShifts.FromOperator.Equals(userList.UserName) && !listHkcChangeShifts.FromRviewer.Equals(userList.UserName))
{
AlertMsg alertMsg = new AlertMsg
{
Message = "仅值班人可登录,当前登录人非值班人",
Type = MsgType.ERROR
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
Username = "";
Password = "";
return;
}
}
}
if (userList.PassWord == MD5.GetMD5Hash(Password))
{
SetUser(userList);
}
else
{
AlertMsg alertMsg = new AlertMsg
{
Message = "密码错误",
Type = MsgType.ERROR
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
Password = "";
}
}
}
}
else
{
AlertMsg alertMsg = new AlertMsg
{
Message = "请输入账号或密码",
Type = MsgType.ERROR
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
LoginBtnEnable = true;
}
private NavigationParameters keys = new NavigationParameters();
void SetUser(UserList user)
{
// 单人登录模式
if (SingleLogin)
{
//添加参数,键值对格式
keys.Add("operator", user);
//System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
//{
logger.Info("跳转home页面");
_regionManager.RequestNavigate("MainRegion", "HomeWindow", keys);
//}));
}
// 双人登录模式
else
{
// 如果已经录入了发药人,已经有一个用户登录
if (keys.ContainsKey("operator"))
{
if (keys.GetValue<UserList>("operator").Id != user.Id)
{
keys.Add("reviewer", user);
Reviewer = user;
RaisePropertyChanged("Reviewer");
//System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
//{
_regionManager.RequestNavigate("MainRegion", "HomeWindow", keys);
//}));
}
else
{
AlertMsg alertMsg = new AlertMsg
{
Message = "该发药人账号已登录,请输入不同账号",
Type = MsgType.ERROR
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
}
// 如果已经录入了审核人, 已经有一个用户登录
else if (keys.ContainsKey("reviewer"))
{
if (keys.GetValue<UserList>("reviewer").Id != user.Id)
{
keys.Add("operator", user);
Operator = user;
RaisePropertyChanged("Operator");
//System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
//{
_regionManager.RequestNavigate("MainRegion", "HomeWindow", keys);
//}));
}
else
{
AlertMsg alertMsg = new AlertMsg
{
Message = "该审核人账号已登录,请输入不同账号",
Type = MsgType.ERROR
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
}
// 第一个用户登录
else
{
//1仅当班人、审核人可登录
if (LoginUserCheck.Equals("1"))
{
if (listHkcChangeShifts != null)
{
if (listHkcChangeShifts.FromOperator.Equals(user.UserName))
{
keys.Add("operator", user);
Operator = user;
RaisePropertyChanged("Operator");
}
else
{
keys.Add("reviewer", user);
Reviewer = user;
RaisePropertyChanged("Reviewer");
}
Username = string.Empty;
Password = string.Empty;
return;
}
}
if (firstLogin.Equals("operator"))
{
keys.Add("operator", user);
Operator = user;
RaisePropertyChanged("Operator");
}
else
{
keys.Add("reviewer", user);
Reviewer = user;
RaisePropertyChanged("Reviewer");
}
Username = string.Empty;
Password = string.Empty;
}
}
}
void Exit()
{
//_chkFunction.HIKLoginOut();
Process.GetCurrentProcess().Kill();
Environment.Exit(0);
}
void LoginEvent(FingerprintMsg msg)
{
logger.Info(msg.ToString());
if (msg.Message.Equals("CONNECT"))
{
FingerMsg = !msg.Result;
}
logger.Info($"LoginBtnEnable:{LoginBtnEnable}");
if (LoginBtnEnable)
{
logger.Info($"msg.Message:{msg.Message};{msg.Message.Equals("LOGIN")}");
if (msg.Message.Equals("LOGIN"))
{
UserList userList = new UserList();
userList = SqlSugarHelper.Db.Queryable<UserList>()
.Includes<RoleDm>(u => u.Role)
.Where(u => u.Id == msg.Id && u.MachineId == ConfigurationManager.AppSettings["machineId"].ToString())
.WithCacheIF(false).First();
//.First(u => u.Id == msg.Id && u.MachineId == ConfigurationManager.AppSettings["machineId"].ToString());
logger.Info($"userList==null?{userList == null}");
if (userList == null)
{
AlertMsg alertMsg = new AlertMsg
{
Message = "无此用户",
Type = MsgType.ERROR
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
Username = "";
Password = "";
}
else if (userList.Role == null)
{
AlertMsg alertMsg = new AlertMsg
{
Message = "用户还未设置权限,请联系管理员",
Type = MsgType.ERROR
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
Username = "";
Password = "";
}
else
{
//1仅当班人、审核人可登录
if (LoginUserCheck.Equals("1"))
{
//检查当前登录人是否是值班人 (审核人与发药人)
listHkcChangeShifts = SqlSugarHelper.Db.Queryable<HkcChangeShifts>()
.Where(cs => cs.State == "0").First();
if (listHkcChangeShifts != null)
{
if (!listHkcChangeShifts.FromOperator.Equals(userList.UserName) && !listHkcChangeShifts.FromRviewer.Equals(userList.UserName))
{
AlertMsg alertMsg = new AlertMsg
{
Message = "仅值班人可登录,当前登录人非值班人",
Type = MsgType.ERROR
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
Username = "";
Password = "";
return;
}
}
}
SetUser(userList);
}
}
}
}
//这个方法用于拦截请求,continuationCallback(true)就是不拦截continuationCallback(false)拦截本次操作
public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
{
continuationCallback(true);
}
//接收导航传过来的参数 现在是在此处初始化了表格数据
public void OnNavigatedTo(NavigationContext navigationContext)
{
//FingerMsg = !_fingerprintUtil.bIsConnected;
_eventAggregator.GetEvent<FingerprintEvent>().Subscribe(LoginEvent);
}
//每次导航的时候该实列用不用重新创建true是不重新创建,false是重新创建
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
//这个方法用于拦截请求
public void OnNavigatedFrom(NavigationContext navigationContext)
{
_eventAggregator.GetEvent<FingerprintEvent>().Unsubscribe(LoginEvent);
}
//手动实现调用配置的逻辑 规避修改配置文件后不起作用的问题
public string ReadAppSetting(string key)
{
string xPath = $"/configuration/appSettings//add[@key='{key}']";
XmlDocument doc = new XmlDocument();
string exeFileName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
doc.Load(exeFileName + ".dll.config");
XmlNode node = doc.SelectSingleNode(xPath);
return node.Attributes["value"].Value.ToString();
}
}
}