终端一体化运控平台
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

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