|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.Toolkit.Mvvm.ComponentModel;
- using System.Collections.ObjectModel;
- using BPASmartClient.CustomResource.Pages.Enums;
- using Microsoft.Toolkit.Mvvm.Input;
- using BPASmartClient.CustomResource.Pages.Model;
- using BPASmartClient.Nfc;
- using System.Threading;
- using BPASmartClient.Helper;
-
- namespace BPASmartClient.CustomResource.Pages.ViewModel
- {
- public class NfcSetViewModel : ObservableObject
- {
- public NfcSetViewModel()
- {
- NfcServer.GetInstance.EnableLogin = true;
- permissions.Clear();
- Enum.GetNames(typeof(Permission)).ToList()?.ForEach(item =>
- {
- if (item != "管理员") permissions.Add(new PermissionSelect() { PermissionName = item });
- });
-
- UserAddCommand = new RelayCommand(() =>
- {
- if (permissions?.FirstOrDefault(p => p.PermissionSelected == true) == null)
- {
- Info = "Error:请选择一个权限;";
- return;
- }
- if (CardNum == null || CardNum?.Length <= 0)
- {
- Info = "Error:请将卡放到刷卡区;";
- return;
- }
- var per = Global.userManager.userInfos.FirstOrDefault(p => p.CardId == CardNum);
- if (per != null)
- {
- Info = "Error:用户已存在!;";
- return;
- }
- if (permissions?.FirstOrDefault(p => p.PermissionSelected == true) == null)
- {
- Info = "Error:请选择一个权限;";
- return;
- }
-
- if (UserName == string.Empty)
- {
- Info = "Error:请添加用户名;";
- return;
- }
-
- var name = Global.userManager.userInfos.FirstOrDefault(p => p.UserName == UserName);
- if (name != null)
- {
- Info = "Error:用户名已存在!;";
- }
- var res = permissions.FirstOrDefault(p => p.PermissionSelected == true);
- if (res != null)
- {
- int index = Array.FindIndex(Global.userManager.userInfos.ToArray(), p => p.CardId.ToString() == CardNum);
- if (index == -1)
- {
- Global.userManager.userInfos.Add(new UserInfo
- {
- CardId = CardNum,
- UserName = UserName,
- Password = CurrentPassword,
- permission = (Permission)Enum.Parse(typeof(Permission), res.PermissionName)
- });
- Config.GetInstance.SaveUser();
- Info = $"Info:ID {CardNum} 添加成功;";
- MessageLog.GetInstance.ShowRunLog($" ID {CardNum} 用户添加成功");
- Thread.Sleep(1000);
- ActionManage.GetInstance.Send("Exit");
- }
- else
- Info = $"Info:ID {NFCHelper.GetInstance.GetReadResult.CardNum} 已存在;";
-
- }
- });
-
- UserCancelCommand = new RelayCommand(() =>
- {
- if (CardNum != null && CardNum != string.Empty)
- {
- var res = Global.userManager.userInfos.FirstOrDefault(p => p.CardId == CardNum);
-
- if (res != null)
- {
- Global.userManager.userInfos.Remove(res);
- Config.GetInstance.SaveUser();
- Info = $"Info:ID {CardNum} 注销成功;";
- MessageLog.GetInstance.ShowRunLog($"ID {CardNum} 用户注销成功");
- Thread.Sleep(1000);
- ActionManage.GetInstance.Send("Exit");
- }
- else
- {
- Info = "Info:当前卡在系统中不存在;";
- return;
- }
- }
- else
- {
- Info = "Error:请将卡放到刷卡区;";
- }
- });
-
- NfcServer.GetInstance.Update = new Action(() =>
- {
- CardNum = NFCHelper.GetInstance.GetReadResult.CardNum;
-
- });
- }
-
- private bool Verify()
- {
- if (permissions?.FirstOrDefault(p => p.PermissionSelected == true) == null)
- {
- Info = "Error:请选择一个权限;";
- return false;
- }
- if (CardNum == null || CardNum?.Length <= 0)
- {
- Info = "Error:请将卡放到刷卡区;";
- return false;
- }
- return true;
- }
-
- public ObservableCollection<PermissionSelect> permissions { get; set; } = new ObservableCollection<PermissionSelect>();
-
- public string CardNum { get { return _mCardNum; } set { _mCardNum = value; OnPropertyChanged(); } }
- private string _mCardNum;
-
- public string Info { get { return _mInfo; } set { _mInfo = value; OnPropertyChanged(); } }
- private string _mInfo;
-
- public string UserName { get { return _userName; } set { _userName = value; OnPropertyChanged(); } }
- private string _userName = string.Empty;
-
- public string CurrentPassword { get { return _currentPassword; } set { _currentPassword = value; OnPropertyChanged(); } }
- private string _currentPassword = "888888";
-
- public string CheckPassword { get { return _checkPassword; } set { _checkPassword = value; OnPropertyChanged(); } }
- private string _checkPassword = "888888";
-
- public RelayCommand SavePermission { get; set; }
-
- public RelayCommand DeletePermission { get; set; }
-
- public RelayCommand UserAddCommand { get; set; }
-
- public RelayCommand UserCancelCommand { get; set; }
-
- }
-
- public class PermissionSelect : ObservableObject
- {
-
- public string PermissionName { get { return _mPermissionName; } set { _mPermissionName = value; OnPropertyChanged(); } }
- private string _mPermissionName;
-
-
- public bool PermissionSelected { get { return _mPermissionSelected; } set { _mPermissionSelected = value; OnPropertyChanged(); } }
- private bool _mPermissionSelected = false;
-
- }
- }
|