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

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