using BPA.Message; using BPASmartClient.CustomResource.Pages.Enums; using BPASmartClient.CustomResource.Pages.Model; using Microsoft.Toolkit.Mvvm.ComponentModel; using Microsoft.Toolkit.Mvvm.Input; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace BPASmartClient.CustomResource.Pages.ViewModel { internal class UserManageViewModel:ObservableObject { //private static UserManageViewModel _instance; //public static UserManageViewModel GetInstance => _instance ??= new UserManageViewModel(); public static ObservableCollection usersInfo { get; set; } = new ObservableCollection(); //public List Authorities { get; set; } = new List() { Permission.管理员, Permission.操作员, Permission.观察员, Permission.技术员 }; public List Authorities { get; set; } = new List(); public RelayCommand SaveCommand { get; set; } public RelayCommand DeleteCommand { get; set; } public RelayCommand AddUserInfoCommand { get; set; } public RelayCommand SaveDataCommand { get; set; } public UserManageViewModel() { var userManager = JsonConvert.DeserializeObject(File.ReadAllText("up.hbl").AESDecrypt()); usersInfo = userManager.userInfos; AddAuthorities(); SaveCommand = new RelayCommand((str) => { if (str != string.Empty && str != null) { int num = usersInfo.Where(p => p.UserName == str).Count(); if (num > 0 && num >= 2) { MessageBox.Show("用户名重复", "提示", MessageBoxButton.OK, MessageBoxImage.Information); } else { var res = usersInfo.FirstOrDefault(p => p.UserName == str); if (res.Id == null || res.Id == string.Empty) { res.Id = IdProcess(); } Global.userManager.userInfos = usersInfo; File.WriteAllText("up.hbl", JsonConvert.SerializeObject(Global.userManager).AESEncrypt()); MessageBox.Show("保存成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information); } } else { MessageBox.Show("保存失败,用户名为空或输入后未回车确认", "提示", MessageBoxButton.OK, MessageBoxImage.Warning); } }); DeleteCommand = new RelayCommand((str) => { if (str != string.Empty && str != null) { var userInfo = usersInfo.FirstOrDefault(p => p.Id == str); if (userInfo != null) { if (usersInfo.Remove(userInfo)) { Global.userManager.userInfos = usersInfo; File.WriteAllText("up.hbl", JsonConvert.SerializeObject(Global.userManager).AESEncrypt()); } } else { MessageBox.Show("未找到对应记录", "提示", MessageBoxButton.OK, MessageBoxImage.Information); } } else { usersInfo.Remove(usersInfo.Last()); Global.userManager.userInfos = usersInfo; File.WriteAllText("up.hbl", JsonConvert.SerializeObject(Global.userManager).AESEncrypt()); } }); AddUserInfoCommand = new RelayCommand( ()=> { usersInfo.Add(new UserInfo() { Id=IdProcess()}); }); SaveDataCommand = new RelayCommand(() => { Global.userManager.userInfos = usersInfo; File.WriteAllText("up.hbl", JsonConvert.SerializeObject(Global.userManager).AESEncrypt()); MessageBox.Show("保存成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information); }); } public string IdProcess() { erp: string id = Guid.NewGuid().ToString(); if (usersInfo.Count > 0) { var res = usersInfo.FirstOrDefault(p => p.Id == id); if (res != null) { goto erp; } else { return id; } } return id; } private void AddAuthorities() { foreach (Permission aut in System.Enum.GetValues(typeof(Permission))) { Authorities.Add(aut.ToString()); } } } }