终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

174 lines
7.8 KiB

  1. using BPASmartClient.Helper;
  2. using FryPot_DosingSystem.Control;
  3. using FryPot_DosingSystem.Model;
  4. using FryPot_DosingSystem.View;
  5. using Microsoft.Toolkit.Mvvm.ComponentModel;
  6. using Microsoft.Toolkit.Mvvm.Input;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Forms;
  16. namespace FryPot_DosingSystem.ViewModel
  17. {
  18. internal class MainViewModel : ObservableObject
  19. {
  20. public ObservableCollection<ActionMenu> menus { get; set; } = new ObservableCollection<ActionMenu>();
  21. public RelayCommand Login { get; set; }
  22. public RelayCommand PasswordChange { get; set; }
  23. public RelayCommand ExitLogin { get; set; }
  24. public RelayCommand<object> TogglePag { get; set; }
  25. public bool UserManagement { get { return _mUserManagement; } set { _mUserManagement = value; OnPropertyChanged(); } }
  26. private bool _mUserManagement;
  27. /// <summary>
  28. /// 当前登陆信息
  29. /// </summary>
  30. private UserInfo _currentLoginAccount;
  31. public UserInfo CurrentLoginAccount { get { return _currentLoginAccount; } set { _currentLoginAccount = value; OnPropertyChanged(); } }
  32. /// <summary>
  33. /// 不同权限显示不同菜单
  34. /// </summary>
  35. public ObservableCollection<ActionMenu> DisplayMenus { get; set; } = new ObservableCollection<ActionMenu>();
  36. public FrameworkElement MyWindow { get { return _mMyWindow; } set { _mMyWindow = value; OnPropertyChanged(); } }
  37. private FrameworkElement _mMyWindow;
  38. public string WindowTitleName { get { return _mWindowTitleName; } set { _mWindowTitleName = value; OnPropertyChanged(); } }
  39. private string _mWindowTitleName;
  40. /// <summary>
  41. /// 开机自启
  42. /// </summary>
  43. public bool AutoStart { get { return SystemHelper.GetInstance.IsAutoStart(); } set { SystemHelper.GetInstance.AutoStart(value); OnPropertyChanged(); } }
  44. private bool _status;
  45. /// <summary>
  46. /// 设备初始化状态
  47. /// </summary>
  48. public bool Status
  49. {
  50. get { return _status; }
  51. set
  52. {
  53. if (value)
  54. {
  55. ActionManage.GetInstance.Send("StartPlcInite");
  56. }
  57. else
  58. {
  59. ActionManage.GetInstance.Send("EndPlcInite");
  60. }
  61. _status = value;
  62. OnPropertyChanged();
  63. }
  64. }
  65. private Authority authority;
  66. public Authority Authority
  67. {
  68. get { return authority; }
  69. set
  70. {
  71. var res = menus.Where(p => Array.FindIndex(p.Authority, au => au == value) >= 0).ToList();
  72. if (res != null && res.Count > 0)
  73. {
  74. DisplayMenus.Clear();
  75. res.ForEach(p => DisplayMenus.Add(p));
  76. DoNavChanged(res[0].CommandParameter);
  77. }
  78. authority = value;
  79. }
  80. }
  81. public MainViewModel()
  82. {
  83. LogViewModel model = LogViewModel.GetInstance;
  84. // DeviceOperate deviceOperate = DeviceOperate.GetInstance;//开启实时PLC数据读取
  85. DosingLogicControl logigControl = DosingLogicControl.GetInstance;//开启逻辑控制任务程序
  86. TogglePag = new RelayCommand<object>(DoNavChanged);
  87. Login = new RelayCommand(() =>
  88. {
  89. ActionManage.GetInstance.CancelRegister("ContentUpdate");
  90. ActionManage.GetInstance.Register(new Func<object>(() => { return "注册"; }), "ContentUpdate");
  91. DoNavChanged("AdministratorLoginView.用户登陆");
  92. UserManagement = false;
  93. });
  94. PasswordChange = new RelayCommand(() =>
  95. {
  96. ActionManage.GetInstance.CancelRegister("ContentUpdate");
  97. ActionManage.GetInstance.Register(new Func<object>(() => { return "修改"; }), "ContentUpdate");
  98. ActionManage.GetInstance.CancelRegister("LoginInfo");
  99. ActionManage.GetInstance.Register(new Func<object>(() => { return CurrentLoginAccount; }), "LoginInfo");
  100. DoNavChanged("AdministratorLoginView.密码修改");
  101. UserManagement = false;
  102. });
  103. ExitLogin = new RelayCommand(() =>
  104. {
  105. //DoNavChanged("LoginView.退出登录");
  106. CurrentLoginAccount = null;
  107. Authority = Authority.观察员;
  108. UserManagement = false;
  109. });
  110. LoginInfoConfig.GetInstance.Init();//用户数据初始化
  111. LoginRegister();
  112. MenusDefaultInit();
  113. Authority = Authority.管理员;
  114. }
  115. private void MenusDefaultInit()
  116. {
  117. menus.Add(new ActionMenu() { MenuName = "配方设置", CommandParameter = "RecipeSetView.配方设置", Authority = new Authority[] { Authority.管理员, Authority.技术员 } });
  118. menus.Add(new ActionMenu() { MenuName = "配方下发", CommandParameter = "RecipeSendDownView.配方下发", Authority = new Authority[] { Authority.管理员, Authority.技术员, Authority.操作员 } });
  119. menus.Add(new ActionMenu() { MenuName = "设备列表", CommandParameter = "DeviceListVIew.设备列表", Authority = new Authority[] { Authority.管理员, Authority.技术员, Authority.操作员, Authority.观察员 } });
  120. menus.Add(new ActionMenu() { MenuName = "状态监测", CommandParameter = "HardWareStatusView.状态监测", Authority = new Authority[] { Authority.管理员, Authority.技术员, Authority.操作员, Authority.观察员 } });
  121. menus.Add(new ActionMenu() { MenuName = "日志信息", CommandParameter = "LogView.日志信息", Authority = new Authority[] { Authority.管理员, Authority.技术员, Authority.操作员, Authority.观察员 } });
  122. menus.Add(new ActionMenu() { MenuName = "报警记录", CommandParameter = "", Authority = new Authority[] { Authority.管理员, Authority.技术员, Authority.操作员, Authority.观察员 } });
  123. }
  124. public void DoNavChanged(object obj)
  125. {
  126. if (obj != null && obj is string stobj)
  127. {
  128. var strs = stobj.Split('.');
  129. if (strs != null && strs.Length == 2)
  130. {
  131. Type type = Type.GetType($"FryPot_DosingSystem.View.{strs[0]}");
  132. var res = type?.GetConstructor(System.Type.EmptyTypes)?.Invoke(null);
  133. if (res != null && res is FrameworkElement fe) MyWindow = fe;
  134. WindowTitleName = strs[1];
  135. }
  136. }
  137. }
  138. /// <summary>
  139. /// 登陆验证
  140. /// </summary>
  141. private void LoginRegister()
  142. {
  143. ActionManage.GetInstance.Register(new Func<object, object>((o) =>
  144. {
  145. if (o != null && o is string str)
  146. {
  147. var strs = str.Split("-=");
  148. if (strs != null && strs.Length == 3)
  149. {
  150. var us = Global.userManager.userInfos.FirstOrDefault(p => p.UserName == strs[0] && p.Password == strs[1] && strs[2] == p.Authority.ToString());
  151. if (us != null)
  152. {
  153. Authority = us.Authority;
  154. CurrentLoginAccount = us;//记录当前登陆信息
  155. return string.Empty;
  156. }
  157. }
  158. }
  159. return "用户名或密码错误";
  160. }), "LoginDosingSystem");
  161. }
  162. }
  163. }