终端一体化运控平台
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

110 行
3.9 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 FryPot_DosingSystem.ViewModel
  15. {
  16. internal class UserManageViewModel : NotifyBase
  17. {
  18. private static UserManageViewModel _instance;
  19. public static UserManageViewModel GetInstance=>_instance??= new UserManageViewModel();
  20. public 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 UserManageViewModel()
  26. {
  27. var userManager = JsonConvert.DeserializeObject<UserManager>(File.ReadAllText("up.hbl").AESDecrypt());
  28. usersInfo = userManager.userInfos;
  29. AddAuthorities();
  30. SaveCommand = new BPARelayCommand<string>((str) =>
  31. {
  32. if (str != string.Empty && str != null)
  33. {
  34. int num = usersInfo.Where(p => p.UserName == str).Count();
  35. if (num > 0 && num >= 2)
  36. {
  37. MessageBox.Show("用户名重复", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  38. }
  39. else
  40. {
  41. var res = usersInfo.FirstOrDefault(p => p.UserName == str);
  42. if (res.Id == null || res.Id == string.Empty)
  43. {
  44. res.Id = IdProcess();
  45. }
  46. Global.userManager.userInfos = usersInfo;
  47. File.WriteAllText("up.hbl", JsonConvert.SerializeObject(Global.userManager).AESEncrypt());
  48. MessageBox.Show("保存成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  49. }
  50. }
  51. });
  52. DeleteCommand = new BPARelayCommand<string>((str) =>
  53. {
  54. if (str != string.Empty && str != null)
  55. {
  56. var userInfo = usersInfo.FirstOrDefault(p => p.Id == str);
  57. if (userInfo != null)
  58. {
  59. if (usersInfo.Remove(userInfo))
  60. {
  61. Global.userManager.userInfos = usersInfo;
  62. File.WriteAllText("up.hbl", JsonConvert.SerializeObject(Global.userManager).AESEncrypt());
  63. }
  64. }
  65. else
  66. {
  67. MessageBox.Show("未找到对应记录", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  68. }
  69. }
  70. });
  71. }
  72. public string IdProcess()
  73. {
  74. erp:
  75. string id = Guid.NewGuid().ToString();
  76. if (usersInfo.Count > 0)
  77. {
  78. var res = usersInfo.FirstOrDefault(p => p.Id == id);
  79. if (res != null)
  80. {
  81. goto erp;
  82. }
  83. else
  84. {
  85. return id;
  86. }
  87. }
  88. return id;
  89. }
  90. private void AddAuthorities()
  91. {
  92. foreach (Permission aut in System.Enum.GetValues(typeof(Permission)))
  93. {
  94. Authorities.Add(aut.ToString());
  95. }
  96. }
  97. }
  98. }