|
- 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 ObservableCollection<UserInfo> usersInfo { get; set; } = new ObservableCollection<UserInfo>();
-
- //public List<Permission> Authorities { get; set; } = new List<Permission>() { Permission.管理员, Permission.操作员, Permission.观察员, Permission.技术员 };
- public List<string> Authorities { get; set; } = new List<string>();
- public RelayCommand<string> SaveCommand { get; set; }
- public RelayCommand<string> DeleteCommand { get; set; }
- public UserManageViewModel()
- {
- var userManager = JsonConvert.DeserializeObject<UserManager>(File.ReadAllText("up.hbl").AESDecrypt());
- usersInfo = userManager.userInfos;
- AddAuthorities();
-
- SaveCommand = new RelayCommand<string>((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);
-
- }
-
- }
- });
- DeleteCommand = new RelayCommand<string>((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);
- }
- }
- });
- }
-
-
-
- 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());
- }
-
- }
- }
- }
|