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

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