using BPA.Helper; using FryPot_DosingSystem.Control; using FryPot_DosingSystem.Model; using FryPot_DosingSystem.View; using BPA.Helper; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Forms; namespace FryPot_DosingSystem.ViewModel { internal class MainViewModel : NotifyBase { public ObservableCollection menus { get; set; } = new ObservableCollection(); public BPARelayCommand Login { get; set; } public BPARelayCommand PasswordChange { get; set; } public BPARelayCommand ExitLogin { get; set; } public BPARelayCommand TogglePag { get; set; } public bool UserManagement { get { return _mUserManagement; } set { _mUserManagement = value; OnPropertyChanged(); } } private bool _mUserManagement; /// /// 当前登陆信息 /// private UserInfo _currentLoginAccount; public UserInfo CurrentLoginAccount { get { return _currentLoginAccount; } set { _currentLoginAccount = value; OnPropertyChanged(); } } /// /// 不同权限显示不同菜单 /// public ObservableCollection DisplayMenus { get; set; } = new ObservableCollection(); public FrameworkElement MyWindow { get { return _mMyWindow; } set { _mMyWindow = value; OnPropertyChanged(); } } private FrameworkElement _mMyWindow; public string WindowTitleName { get { return _mWindowTitleName; } set { _mWindowTitleName = value; OnPropertyChanged(); } } private string _mWindowTitleName; /// /// 开机自启 /// public bool AutoStart { get { return SystemHelper.GetInstance.IsAutoStart(); } set { SystemHelper.GetInstance.AutoStart(value); OnPropertyChanged(); } } private bool _status; /// /// 设备初始化状态 /// public bool Status { get { return _status; } set { if (value) { ActionManage.GetInstance.Send("StartPlcInite"); } else { ActionManage.GetInstance.Send("EndPlcInite"); } _status = value; OnPropertyChanged(); } } private Authority authority; public Authority Authority { get { return authority; } set { var res = menus.Where(p => Array.FindIndex(p.Authority, au => au == value) >= 0).ToList(); if (res != null && res.Count > 0) { DisplayMenus.Clear(); res.ForEach(p => DisplayMenus.Add(p)); DoNavChanged(res[0].CommandParameter); } authority = value; } } public MainViewModel() { LogViewModel model = LogViewModel.GetInstance; // DeviceOperate deviceOperate = DeviceOperate.GetInstance;//开启实时PLC数据读取 DosingLogicControl logigControl = DosingLogicControl.GetInstance;//开启逻辑控制任务程序 TogglePag = new BPARelayCommand(DoNavChanged); Login = new BPARelayCommand(() => { ActionManage.GetInstance.CancelRegister("ContentUpdate"); ActionManage.GetInstance.Register(new Func(() => { return "注册"; }), "ContentUpdate"); DoNavChanged("AdministratorLoginView.用户登陆"); UserManagement = false; }); PasswordChange = new BPARelayCommand(() => { ActionManage.GetInstance.CancelRegister("ContentUpdate"); ActionManage.GetInstance.Register(new Func(() => { return "修改"; }), "ContentUpdate"); ActionManage.GetInstance.CancelRegister("LoginInfo"); ActionManage.GetInstance.Register(new Func(() => { return CurrentLoginAccount; }), "LoginInfo"); DoNavChanged("AdministratorLoginView.密码修改"); UserManagement = false; }); ExitLogin = new BPARelayCommand(() => { //DoNavChanged("LoginView.退出登录"); CurrentLoginAccount = null; Authority = Authority.观察员; UserManagement = false; }); LoginInfoConfig.GetInstance.Init();//用户数据初始化 LoginRegister(); MenusDefaultInit(); Authority = Authority.管理员; } private void MenusDefaultInit() { menus.Add(new ActionMenu() { MenuName = "配方设置", CommandParameter = "RecipeSetView.配方设置", Authority = new Authority[] { Authority.管理员, Authority.技术员 } }); menus.Add(new ActionMenu() { MenuName = "配方下发", CommandParameter = "RecipeSendDownView.配方下发", Authority = new Authority[] { Authority.管理员, Authority.技术员, Authority.操作员 } }); menus.Add(new ActionMenu() { MenuName = "设备列表", CommandParameter = "DeviceListVIew.设备列表", Authority = new Authority[] { Authority.管理员, Authority.技术员, Authority.操作员, Authority.观察员 } }); menus.Add(new ActionMenu() { MenuName = "状态监测", CommandParameter = "HardWareStatusView.状态监测", Authority = new Authority[] { Authority.管理员, Authority.技术员, Authority.操作员, Authority.观察员 } }); menus.Add(new ActionMenu() { MenuName = "日志信息", CommandParameter = "LogView.日志信息", Authority = new Authority[] { Authority.管理员, Authority.技术员, Authority.操作员, Authority.观察员 } }); menus.Add(new ActionMenu() { MenuName = "报警记录", CommandParameter = "", Authority = new Authority[] { Authority.管理员, Authority.技术员, Authority.操作员, Authority.观察员 } }); } public void DoNavChanged(object obj) { if (obj != null && obj is string stobj) { var strs = stobj.Split('.'); if (strs != null && strs.Length == 2) { Type type = Type.GetType($"FryPot_DosingSystem.View.{strs[0]}"); var res = type?.GetConstructor(System.Type.EmptyTypes)?.Invoke(null); if (res != null && res is FrameworkElement fe) MyWindow = fe; WindowTitleName = strs[1]; } } } /// /// 登陆验证 /// private void LoginRegister() { ActionManage.GetInstance.Register(new Func((o) => { if (o != null && o is string str) { var strs = str.Split("-="); if (strs != null && strs.Length == 3) { var us = Global.userManager.userInfos.FirstOrDefault(p => p.UserName == strs[0] && p.Password == strs[1] && strs[2] == p.Authority.ToString()); if (us != null) { Authority = us.Authority; CurrentLoginAccount = us;//记录当前登陆信息 return string.Empty; } } } return "用户名或密码错误"; }), "LoginDosingSystem"); } } }