XiangTan_DM/DM_Weight/ViewModels/LoginWindowViewModel.cs

509 lines
20 KiB
C#
Raw Normal View History

using DM_Weight.Finger;
using DM_Weight.HIKVISION;
using DM_Weight.Models;
using DM_Weight.msg;
using DM_Weight.Port;
using DM_Weight.util;
using DM_Weight.Views;
using log4net;
2024-12-03 13:22:42 +08:00
using log4net.Core;
using log4net.Repository.Hierarchy;
using Microsoft.Win32;
2024-12-03 13:22:42 +08:00
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using ScreenRecorderLib;
2024-12-03 13:22:42 +08:00
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
2024-12-03 13:22:42 +08:00
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.IO;
2024-12-03 13:22:42 +08:00
using System.Linq;
using System.Security.AccessControl;
2024-12-03 13:22:42 +08:00
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
2024-12-03 13:22:42 +08:00
using System.Xml;
namespace DM_Weight.ViewModels
{
public class LoginWindowViewModel : BindableBase, IRegionMemberLifetime, IConfirmNavigationRequest
{
2024-12-03 13:22:42 +08:00
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();
2024-12-03 13:22:42 +08:00
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);
2024-12-03 13:22:42 +08:00
}
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()
2024-12-03 13:22:42 +08:00
{
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)
2024-12-03 13:22:42 +08:00
{
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))
2024-12-03 13:22:42 +08:00
{
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);
2024-12-03 13:22:42 +08:00
//}));
}
// 双人登录模式
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);
2024-12-03 13:22:42 +08:00
//}));
}
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);
2024-12-03 13:22:42 +08:00
//}));
}
else
{
AlertMsg alertMsg = new AlertMsg
{
Message = "该审核人账号已登录,请输入不同账号",
Type = MsgType.ERROR
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
}
// 第一个用户登录
else
{
//1仅当班人、审核人可登录
if (LoginUserCheck.Equals("1"))
{
if (listHkcChangeShifts != null)
2024-12-03 13:22:42 +08:00
{
if (listHkcChangeShifts.FromOperator.Equals(user.UserName))
2024-12-03 13:22:42 +08:00
{
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;
2024-12-03 13:22:42 +08:00
}
}
}
void Exit()
{
//清录屏进程
_eventAggregator.GetEvent<PrintScreenEvent>().Publish(2);
2024-12-03 13:22:42 +08:00
//_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}");
2024-12-03 13:22:42 +08:00
if (LoginBtnEnable)
{
logger.Info($"msg.Message:{msg.Message};{msg.Message.Equals("LOGIN")}");
2024-12-03 13:22:42 +08:00
if (msg.Message.Equals("LOGIN"))
{
try
{
2024-12-03 13:22:42 +08:00
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)
2024-12-03 13:22:42 +08:00
{
AlertMsg alertMsg = new AlertMsg
{
Message = "无此用户",
Type = MsgType.ERROR
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
Username = "";
Password = "";
}
else if (userList.Role == null)
2024-12-03 13:22:42 +08:00
{
AlertMsg alertMsg = new AlertMsg
{
Message = "用户还未设置权限,请联系管理员",
Type = MsgType.ERROR
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
Username = "";
Password = "";
}
else
2024-12-03 13:22:42 +08:00
{
//1仅当班人、审核人可登录
if (LoginUserCheck.Equals("1"))
2024-12-03 13:22:42 +08:00
{
//检查当前登录人是否是值班人 (审核人与发药人)
listHkcChangeShifts = SqlSugarHelper.Db.Queryable<HkcChangeShifts>()
.Where(cs => cs.State == "0").First();
if (listHkcChangeShifts != null)
2024-12-03 13:22:42 +08:00
{
if (!listHkcChangeShifts.FromOperator.Equals(userList.UserName) && !listHkcChangeShifts.FromRviewer.Equals(userList.UserName))
2024-12-03 13:22:42 +08:00
{
AlertMsg alertMsg = new AlertMsg
{
Message = "仅值班人可登录,当前登录人非值班人",
Type = MsgType.ERROR
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
Username = "";
Password = "";
return;
}
2024-12-03 13:22:42 +08:00
}
2024-12-03 13:22:42 +08:00
}
SetUser(userList);
}
}
catch (Exception ex)
{
if (ex.Message.Contains("连接数据库过程中发生错误"))
{
App.DbConnectionFail = true;
_regionManager.RequestNavigate("MainRegion", "EmergencyWindow");
2024-12-03 13:22:42 +08:00
}
logger.Info(ex.Message);
2024-12-03 13:22:42 +08:00
}
}
}
}
//这个方法用于拦截请求,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);
//结束录屏
_eventAggregator.GetEvent<PrintScreenEvent>().Publish(0);
2024-12-03 13:22:42 +08:00
}
//每次导航的时候该实列用不用重新创建true是不重新创建,false是重新创建
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
//这个方法用于拦截请求
public void OnNavigatedFrom(NavigationContext navigationContext)
{
_eventAggregator.GetEvent<FingerprintEvent>().Unsubscribe(LoginEvent);
#region
//登录进来后开始录屏
_eventAggregator.GetEvent<PrintScreenEvent>().Publish(1);
#endregion
2024-12-03 13:22:42 +08:00
}
//手动实现调用配置的逻辑 规避修改配置文件后不起作用的问题
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();
}
2024-12-03 13:22:42 +08:00
}
}