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

138 line
5.1 KiB

  1. using BPA.Message;
  2. using BPASmartClient.CustomResource.Pages.Enums;
  3. using BPASmartClient.CustomResource.Pages.Model;
  4. using BPA.Helper;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. namespace BPASmartClient.CustomResource.Pages.ViewModel
  15. {
  16. internal class UserManageViewModel:NotifyBase
  17. {
  18. //private static UserManageViewModel _instance;
  19. //public static UserManageViewModel GetInstance => _instance ??= new UserManageViewModel();
  20. public static ObservableCollection<UserInfo> usersInfo { get; set; } = new ObservableCollection<UserInfo>();
  21. //public List<Permission> Authorities { get; set; } = new List<Permission>() { Permission.管理员, Permission.操作员, Permission.观察员, Permission.技术员 };
  22. public List<string> Authorities { get; set; } = new List<string>();
  23. public BPARelayCommand<string> SaveCommand { get; set; }
  24. public BPARelayCommand<string> DeleteCommand { get; set; }
  25. public BPARelayCommand AddUserInfoCommand { get; set; }
  26. public BPARelayCommand SaveDataCommand { get; set; }
  27. public UserManageViewModel()
  28. {
  29. var userManager = JsonConvert.DeserializeObject<UserManager>(File.ReadAllText("up.hbl").AESDecrypt());
  30. usersInfo = userManager.userInfos;
  31. AddAuthorities();
  32. SaveCommand = new BPARelayCommand<string>((str) =>
  33. {
  34. if (str != string.Empty && str != null)
  35. {
  36. int num = usersInfo.Where(p => p.UserName == str).Count();
  37. if (num > 0 && num >= 2)
  38. {
  39. MessageBox.Show("用户名重复", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  40. }
  41. else
  42. {
  43. var res = usersInfo.FirstOrDefault(p => p.UserName == str);
  44. if (res.Id == null || res.Id == string.Empty)
  45. {
  46. res.Id = IdProcess();
  47. }
  48. Global.userManager.userInfos = usersInfo;
  49. File.WriteAllText("up.hbl", JsonConvert.SerializeObject(Global.userManager).AESEncrypt());
  50. MessageBox.Show("保存成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  51. }
  52. }
  53. else
  54. {
  55. MessageBox.Show("保存失败,用户名为空或输入后未回车确认", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  56. }
  57. });
  58. DeleteCommand = new BPARelayCommand<string>((str) =>
  59. {
  60. if (str != string.Empty && str != null)
  61. {
  62. var userInfo = usersInfo.FirstOrDefault(p => p.Id == str);
  63. if (userInfo != null)
  64. {
  65. if (usersInfo.Remove(userInfo))
  66. {
  67. Global.userManager.userInfos = usersInfo;
  68. File.WriteAllText("up.hbl", JsonConvert.SerializeObject(Global.userManager).AESEncrypt());
  69. }
  70. }
  71. else
  72. {
  73. MessageBox.Show("未找到对应记录", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  74. }
  75. }
  76. else
  77. {
  78. usersInfo.Remove(usersInfo.Last());
  79. Global.userManager.userInfos = usersInfo;
  80. File.WriteAllText("up.hbl", JsonConvert.SerializeObject(Global.userManager).AESEncrypt());
  81. }
  82. });
  83. AddUserInfoCommand = new BPARelayCommand( ()=>
  84. {
  85. usersInfo.Add(new UserInfo() { Id=IdProcess()});
  86. });
  87. SaveDataCommand = new BPARelayCommand(() =>
  88. {
  89. Global.userManager.userInfos = usersInfo;
  90. File.WriteAllText("up.hbl", JsonConvert.SerializeObject(Global.userManager).AESEncrypt());
  91. MessageBox.Show("保存成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  92. });
  93. }
  94. public string IdProcess()
  95. {
  96. erp:
  97. string id = Guid.NewGuid().ToString();
  98. if (usersInfo.Count > 0)
  99. {
  100. var res = usersInfo.FirstOrDefault(p => p.Id == id);
  101. if (res != null)
  102. {
  103. goto erp;
  104. }
  105. else
  106. {
  107. return id;
  108. }
  109. }
  110. return id;
  111. }
  112. private void AddAuthorities()
  113. {
  114. foreach (Permission aut in System.Enum.GetValues(typeof(Permission)))
  115. {
  116. Authorities.Add(aut.ToString());
  117. }
  118. }
  119. }
  120. }