终端一体化运控平台
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

174 linhas
7.7 KiB

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