|
-
- using BPASmartClient.Helper;
- using FryPot_DosingSystem.Control;
- using FryPot_DosingSystem.Model;
- using FryPot_DosingSystem.View;
- using Microsoft.Toolkit.Mvvm.ComponentModel;
- using Microsoft.Toolkit.Mvvm.Input;
- 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 : ObservableObject
- {
- public ObservableCollection<ActionMenu> menus { get; set; } = new ObservableCollection<ActionMenu>();
-
- public RelayCommand Login { get; set; }
- public RelayCommand PasswordChange { get; set; }
-
- public RelayCommand ExitLogin { get; set; }
- public RelayCommand<object> TogglePag { get; set; }
- public bool UserManagement { get { return _mUserManagement; } set { _mUserManagement = value; OnPropertyChanged(); } }
- private bool _mUserManagement;
- /// <summary>
- /// 当前登陆信息
- /// </summary>
- private UserInfo _currentLoginAccount;
- public UserInfo CurrentLoginAccount { get { return _currentLoginAccount; } set { _currentLoginAccount = value; OnPropertyChanged(); } }
- /// <summary>
- /// 不同权限显示不同菜单
- /// </summary>
- public ObservableCollection<ActionMenu> DisplayMenus { get; set; } = new ObservableCollection<ActionMenu>();
- 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;
- /// <summary>
- /// 开机自启
- /// </summary>
- public bool AutoStart { get { return SystemHelper.GetInstance.IsAutoStart(); } set { SystemHelper.GetInstance.AutoStart(value); OnPropertyChanged(); } }
-
- private bool _status;
- /// <summary>
- /// 设备初始化状态
- /// </summary>
- 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 RelayCommand<object>(DoNavChanged);
- Login = new RelayCommand(() =>
- {
- ActionManage.GetInstance.CancelRegister("ContentUpdate");
- ActionManage.GetInstance.Register(new Func<object>(() => { return "注册"; }), "ContentUpdate");
- DoNavChanged("AdministratorLoginView.用户登陆");
- UserManagement = false;
- });
- PasswordChange = new RelayCommand(() =>
- {
- ActionManage.GetInstance.CancelRegister("ContentUpdate");
- ActionManage.GetInstance.Register(new Func<object>(() => { return "修改"; }), "ContentUpdate");
- ActionManage.GetInstance.CancelRegister("LoginInfo");
- ActionManage.GetInstance.Register(new Func<object>(() => { return CurrentLoginAccount; }), "LoginInfo");
- DoNavChanged("AdministratorLoginView.密码修改");
- UserManagement = false;
- });
- ExitLogin = new RelayCommand(() =>
- {
- //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];
- }
- }
- }
- /// <summary>
- /// 登陆验证
- /// </summary>
- private void LoginRegister()
- {
- ActionManage.GetInstance.Register(new Func<object, object>((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");
- }
- }
- }
|