终端一体化运控平台
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.
 
 
 

171 lines
7.5 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using BPASmartClient.CustomResource.Pages.Model;
  8. using BPASmartClient.Helper;
  9. using Microsoft.Toolkit.Mvvm.ComponentModel;
  10. using Microsoft.Toolkit.Mvvm.Input;
  11. namespace BPASmartClient.CustomResource.Pages.ViewModel
  12. {
  13. public class LoginViewModel : ObservableObject
  14. {
  15. public LoginViewModel()
  16. {
  17. ActionManage.GetInstance.Register(new Action(() => { Login(); }), "EnterLogin", true);
  18. LoginCommand = new RelayCommand(() => { Login(); });
  19. ExitCommand = new RelayCommand(() =>
  20. {
  21. ActionManage.GetInstance.Send("ExitAction");
  22. });
  23. }
  24. public static void NfcLogin(string cardId)
  25. {
  26. var res = Global.userManager.userInfos.FirstOrDefault(p => p.CardId == cardId);
  27. if (res != null)
  28. {
  29. Global.userInfo = res;
  30. //permission权限
  31. for (int i = 0; i < MenuManage.GetInstance.menuModels.Count; i++)
  32. {
  33. if (MenuManage.GetInstance.menuModels.ElementAt(i).MainMenuPermission.Contains(res.permission))
  34. MenuManage.GetInstance.menuModels.ElementAt(i).MainMenuVisibility = Visibility.Visible;
  35. else
  36. MenuManage.GetInstance.menuModels.ElementAt(i).MainMenuVisibility = Visibility.Collapsed;
  37. if (MenuManage.GetInstance.menuModels.ElementAt(i).subMenumodels.FirstOrDefault(p => p.SubMenuPermission.Contains(res.permission)) == null)
  38. MenuManage.GetInstance.menuModels.ElementAt(i).MainMenuVisibility = Visibility.Collapsed;
  39. for (int m = 0; m < MenuManage.GetInstance.menuModels.ElementAt(i).subMenumodels.Count; m++)
  40. {
  41. if (MenuManage.GetInstance.menuModels.ElementAt(i).subMenumodels.ElementAt(m).SubMenuPermission.Contains(res.permission))
  42. MenuManage.GetInstance.menuModels.ElementAt(i).subMenumodels.ElementAt(m).SubMenuVisibility = Visibility.Visible;
  43. else
  44. MenuManage.GetInstance.menuModels.ElementAt(i).subMenumodels.ElementAt(m).SubMenuVisibility = Visibility.Collapsed;
  45. }
  46. }
  47. //分配后的权限
  48. if (res.userTreeViewModels != null)
  49. {
  50. if (res.userTreeViewModels.Count > 0)
  51. {
  52. foreach (var items in MenuManage.GetInstance.menuModels)
  53. {
  54. foreach (var item in items.subMenumodels)
  55. {
  56. items.MainMenuVisibility = Visibility.Visible;
  57. if (res.userTreeViewModels.FirstOrDefault(p => p.Name == item.SubMenuName) != null)
  58. {
  59. item.SubMenuVisibility = Visibility.Visible;
  60. }
  61. }
  62. }
  63. }
  64. }
  65. Application.Current.Dispatcher.Invoke(new Action(() => { ActionManage.GetInstance.Send("LoginOk"); }));
  66. }
  67. }
  68. private void Login()
  69. {
  70. var res = Global.userManager.userInfos.FirstOrDefault(p => p.UserName == UserName && p.Password == Password);
  71. if (res != null)
  72. {
  73. Global.userInfo = res;
  74. Global.userInfo.LastLogInTime = DateTime.Now.ToString();
  75. res.LastLogInTime = DateTime.Now.ToString();
  76. Config.GetInstance.SaveUser();
  77. //permission权限
  78. for (int i = 0; i < MenuManage.GetInstance.menuModels.Count; i++)
  79. {
  80. if (MenuManage.GetInstance.menuModels.ElementAt(i).MainMenuPermission.Contains(res.permission))
  81. MenuManage.GetInstance.menuModels.ElementAt(i).MainMenuVisibility = Visibility.Visible;
  82. else
  83. MenuManage.GetInstance.menuModels.ElementAt(i).MainMenuVisibility = Visibility.Collapsed;
  84. if (MenuManage.GetInstance.menuModels.ElementAt(i).subMenumodels.FirstOrDefault(p => p.SubMenuPermission.Contains(res.permission)) == null)
  85. MenuManage.GetInstance.menuModels.ElementAt(i).MainMenuVisibility = Visibility.Collapsed;
  86. for (int m = 0; m < MenuManage.GetInstance.menuModels.ElementAt(i).subMenumodels.Count; m++)
  87. {
  88. if (MenuManage.GetInstance.menuModels.ElementAt(i).subMenumodels.ElementAt(m).SubMenuPermission.Contains(res.permission))
  89. MenuManage.GetInstance.menuModels.ElementAt(i).subMenumodels.ElementAt(m).SubMenuVisibility = Visibility.Visible;
  90. else
  91. MenuManage.GetInstance.menuModels.ElementAt(i).subMenumodels.ElementAt(m).SubMenuVisibility = Visibility.Collapsed;
  92. }
  93. }
  94. //分配后的权限
  95. if (res.permission == Enums.Permission.管理员)
  96. {
  97. foreach( var items in MenuManage.GetInstance.menuModels)
  98. {
  99. items.MainMenuVisibility = Visibility.Visible;
  100. foreach(var item in items.subMenumodels)
  101. {
  102. item.SubMenuVisibility = Visibility.Visible;
  103. }
  104. }
  105. }
  106. else
  107. {
  108. if (res.userTreeViewModels != null)
  109. {
  110. if (res.userTreeViewModels.Count > 0)
  111. {
  112. foreach (var items in MenuManage.GetInstance.menuModels)
  113. {
  114. foreach (var item in items.subMenumodels)
  115. {
  116. items.MainMenuVisibility = Visibility.Visible;
  117. if (res.userTreeViewModels.FirstOrDefault(p => p.Name == item.SubMenuName) != null)
  118. {
  119. item.SubMenuVisibility = Visibility.Visible;
  120. }
  121. }
  122. }
  123. }
  124. }
  125. }
  126. ActionManage.GetInstance.Send("PermissionChange");
  127. ActionManage.GetInstance.Send("LoginOk");
  128. }
  129. else
  130. {
  131. ErrorInfo = "用户名或密码错误!";
  132. }
  133. }
  134. public RelayCommand LoginCommand { get; set; }
  135. public RelayCommand ExitCommand { get; set; }
  136. public string ErrorInfo { get { return _mErrorInfo; } set { _mErrorInfo = value; OnPropertyChanged(); } }
  137. private string _mErrorInfo;
  138. public string UserName { get { return _mUserName; } set { _mUserName = value; OnPropertyChanged(); } }
  139. private string _mUserName = Global.userInfo.UserName;
  140. public string Password { get { return _mPassword; } set { _mPassword = value; OnPropertyChanged(); } }
  141. private string _mPassword;
  142. }
  143. }