终端一体化运控平台
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

176 wiersze
6.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 BPA.Helper;
  7. using System.Collections.ObjectModel;
  8. using BPASmartClient.CustomResource.Pages.Enums;
  9. using BPASmartClient.CustomResource.Pages.Model;
  10. using BPASmartClient.Nfc;
  11. using System.Threading;
  12. using BPA.Helper;
  13. namespace BPASmartClient.CustomResource.Pages.ViewModel
  14. {
  15. public class NfcSetViewModel : NotifyBase
  16. {
  17. public NfcSetViewModel()
  18. {
  19. NfcServer.GetInstance.EnableLogin = true;
  20. permissions.Clear();
  21. Enum.GetNames(typeof(Permission)).ToList()?.ForEach(item =>
  22. {
  23. if (item != "管理员") permissions.Add(new PermissionSelect() { PermissionName = item });
  24. });
  25. UserAddCommand = new BPARelayCommand(() =>
  26. {
  27. if (permissions?.FirstOrDefault(p => p.PermissionSelected == true) == null)
  28. {
  29. Info = "Error:请选择一个权限;";
  30. return;
  31. }
  32. if (CardNum == null || CardNum?.Length <= 0)
  33. {
  34. Info = "Error:请将卡放到刷卡区;";
  35. return;
  36. }
  37. var per = Global.userManager.userInfos.FirstOrDefault(p => p.CardId == CardNum);
  38. if (per != null)
  39. {
  40. Info = "Error:用户已存在!;";
  41. return;
  42. }
  43. if (permissions?.FirstOrDefault(p => p.PermissionSelected == true) == null)
  44. {
  45. Info = "Error:请选择一个权限;";
  46. return;
  47. }
  48. if (UserName == string.Empty)
  49. {
  50. Info = "Error:请添加用户名;";
  51. return;
  52. }
  53. var name = Global.userManager.userInfos.FirstOrDefault(p => p.UserName == UserName);
  54. if (name != null)
  55. {
  56. Info = "Error:用户名已存在!;";
  57. }
  58. var res = permissions.FirstOrDefault(p => p.PermissionSelected == true);
  59. if (res != null)
  60. {
  61. int index = Array.FindIndex(Global.userManager.userInfos.ToArray(), p => p.CardId.ToString() == CardNum);
  62. if (index == -1)
  63. {
  64. Global.userManager.userInfos.Add(new UserInfo
  65. {
  66. CardId = CardNum,
  67. UserName = UserName,
  68. Password = CurrentPassword,
  69. permission = (Permission)Enum.Parse(typeof(Permission), res.PermissionName)
  70. });
  71. Config.GetInstance.SaveUser();
  72. Info = $"Info:ID {CardNum} 添加成功;";
  73. MessageNotify.GetInstance.ShowRunLog($" ID {CardNum} 用户添加成功");
  74. Thread.Sleep(1000);
  75. ActionManage.GetInstance.Send("Exit");
  76. }
  77. else
  78. Info = $"Info:ID {NFCHelper.GetInstance.GetReadResult.CardNum} 已存在;";
  79. }
  80. });
  81. UserCancelCommand = new BPARelayCommand(() =>
  82. {
  83. if (CardNum != null && CardNum != string.Empty)
  84. {
  85. var res = Global.userManager.userInfos.FirstOrDefault(p => p.CardId == CardNum);
  86. if (res != null)
  87. {
  88. Global.userManager.userInfos.Remove(res);
  89. Config.GetInstance.SaveUser();
  90. Info = $"Info:ID {CardNum} 注销成功;";
  91. MessageNotify.GetInstance.ShowRunLog($"ID {CardNum} 用户注销成功");
  92. Thread.Sleep(1000);
  93. ActionManage.GetInstance.Send("Exit");
  94. }
  95. else
  96. {
  97. Info = "Info:当前卡在系统中不存在;";
  98. return;
  99. }
  100. }
  101. else
  102. {
  103. Info = "Error:请将卡放到刷卡区;";
  104. }
  105. });
  106. NfcServer.GetInstance.Update = new Action(() =>
  107. {
  108. CardNum = NFCHelper.GetInstance.GetReadResult.CardNum;
  109. });
  110. }
  111. private bool Verify()
  112. {
  113. if (permissions?.FirstOrDefault(p => p.PermissionSelected == true) == null)
  114. {
  115. Info = "Error:请选择一个权限;";
  116. return false;
  117. }
  118. if (CardNum == null || CardNum?.Length <= 0)
  119. {
  120. Info = "Error:请将卡放到刷卡区;";
  121. return false;
  122. }
  123. return true;
  124. }
  125. public ObservableCollection<PermissionSelect> permissions { get; set; } = new ObservableCollection<PermissionSelect>();
  126. public string CardNum { get { return _mCardNum; } set { _mCardNum = value; OnPropertyChanged(); } }
  127. private string _mCardNum;
  128. public string Info { get { return _mInfo; } set { _mInfo = value; OnPropertyChanged(); } }
  129. private string _mInfo;
  130. public string UserName { get { return _userName; } set { _userName = value; OnPropertyChanged(); } }
  131. private string _userName = string.Empty;
  132. public string CurrentPassword { get { return _currentPassword; } set { _currentPassword = value; OnPropertyChanged(); } }
  133. private string _currentPassword = "888888";
  134. public string CheckPassword { get { return _checkPassword; } set { _checkPassword = value; OnPropertyChanged(); } }
  135. private string _checkPassword = "888888";
  136. public BPARelayCommand SavePermission { get; set; }
  137. public BPARelayCommand DeletePermission { get; set; }
  138. public BPARelayCommand UserAddCommand { get; set; }
  139. public BPARelayCommand UserCancelCommand { get; set; }
  140. }
  141. public class PermissionSelect : NotifyBase
  142. {
  143. public string PermissionName { get { return _mPermissionName; } set { _mPermissionName = value; OnPropertyChanged(); } }
  144. private string _mPermissionName;
  145. public bool PermissionSelected { get { return _mPermissionSelected; } set { _mPermissionSelected = value; OnPropertyChanged(); } }
  146. private bool _mPermissionSelected = false;
  147. }
  148. }