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

167 lines
6.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Toolkit.Mvvm.ComponentModel;
  7. using System.Collections.ObjectModel;
  8. using BPASmartClient.CustomResource.Pages.Enums;
  9. using Microsoft.Toolkit.Mvvm.Input;
  10. using BPASmartClient.CustomResource.Pages.Model;
  11. using BPASmartClient.Nfc;
  12. using System.Threading;
  13. using BPASmartClient.Helper;
  14. namespace BPASmartClient.CustomResource.Pages.ViewModel
  15. {
  16. public class NfcSetViewModel : ObservableObject
  17. {
  18. public NfcSetViewModel()
  19. {
  20. NfcServer.GetInstance.EnableLogin = true;
  21. permissions.Clear();
  22. Enum.GetNames(typeof(Permission)).ToList()?.ForEach(item => {
  23. if(item !="管理员") permissions.Add(new PermissionSelect() { PermissionName = item });
  24. });
  25. UserAddCommand = new RelayCommand(() =>
  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. MessageLog.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 RelayCommand(() =>
  82. {
  83. var res = Global.userManager.userInfos.FirstOrDefault(p=>p.CardId == CardNum);
  84. if(res != null)
  85. {
  86. Global.userManager.userInfos.Remove(res);
  87. Config.GetInstance.SaveUser();
  88. Info = $"Info:ID {CardNum} 注销成功;";
  89. MessageLog.GetInstance.ShowRunLog($"ID {CardNum} 用户注销成功");
  90. Thread.Sleep(1000);
  91. ActionManage.GetInstance.Send("Exit");
  92. }
  93. else
  94. {
  95. Info = "Info:当前卡在系统中不存在;";
  96. return;
  97. }
  98. });
  99. NfcServer.GetInstance.Update = new Action(() =>
  100. {
  101. CardNum = NFCHelper.GetInstance.GetReadResult.CardNum;
  102. });
  103. }
  104. private bool Verify()
  105. {
  106. if (permissions?.FirstOrDefault(p => p.PermissionSelected == true) == null)
  107. {
  108. Info = "Error:请选择一个权限;";
  109. return false;
  110. }
  111. if (CardNum == null || CardNum?.Length <= 0)
  112. {
  113. Info = "Error:请将卡放到刷卡区;";
  114. return false;
  115. }
  116. return true;
  117. }
  118. public ObservableCollection<PermissionSelect> permissions { get; set; } = new ObservableCollection<PermissionSelect>();
  119. public string CardNum { get { return _mCardNum; } set { _mCardNum = value; OnPropertyChanged(); } }
  120. private string _mCardNum;
  121. public string Info { get { return _mInfo; } set { _mInfo = value; OnPropertyChanged(); } }
  122. private string _mInfo;
  123. public string UserName { get { return _userName; } set { _userName = value; OnPropertyChanged(); } }
  124. private string _userName = string.Empty;
  125. public string CurrentPassword { get { return _currentPassword; } set { _currentPassword = value; OnPropertyChanged(); } }
  126. private string _currentPassword = "888888";
  127. public string CheckPassword { get { return _checkPassword; } set { _checkPassword = value; OnPropertyChanged(); } }
  128. private string _checkPassword = "888888";
  129. public RelayCommand SavePermission { get; set; }
  130. public RelayCommand DeletePermission { get; set; }
  131. public RelayCommand UserAddCommand { get; set; }
  132. public RelayCommand UserCancelCommand { get; set; }
  133. }
  134. public class PermissionSelect : ObservableObject
  135. {
  136. public string PermissionName { get { return _mPermissionName; } set { _mPermissionName = value; OnPropertyChanged(); } }
  137. private string _mPermissionName;
  138. public bool PermissionSelected { get { return _mPermissionSelected; } set { _mPermissionSelected = value; OnPropertyChanged(); } }
  139. private bool _mPermissionSelected = false;
  140. }
  141. }