|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using BPA.Message;
- using BPASmartClient.CustomResource.Pages.Enums;
- using BPASmartClient.CustomResource.Pages.Model;
- using BPA.Helper;
-
- 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 FryPot_DosingSystem.ViewModel
- {
- internal class UserManageViewModel : NotifyBase
- {
- 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 BPARelayCommand<string> SaveCommand { get; set; }
- public BPARelayCommand<string> DeleteCommand { get; set; }
- public UserManageViewModel()
- {
- var userManager = JsonConvert.DeserializeObject<UserManager>(File.ReadAllText("up.hbl").AESDecrypt());
- usersInfo = userManager.userInfos;
- AddAuthorities();
-
- SaveCommand = new BPARelayCommand<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 BPARelayCommand<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());
- }
-
- }
- }
- }
|