using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Prism.Commands; using Prism.Events; using Prism.Ioc; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text; using System.Windows; using TeamAAS_VP.Core; using TeamAAS_VP.Core.Lights; using TeamAAS_VP.Interfaces; using TeamAAS_VP.Views; using TeamAAS_VP.Views.User; namespace TeamAAS_VP.ViewModels.User { public class CardLoginWindowViewModel : BindableBase { #region 字段 IRegionManager _regionManager; IRegionNavigationService _regionNavigationService; IContainerProvider _container; IDialogService _dialogService; IEventAggregator _eventAggregator; IConfigService _configService; System.Net.Http.HttpClient httpClient; #endregion #region 属性 private string _Name; public string Name { get { return _Name; } set { SetProperty(ref _Name, value); } } private string _Password; public string Password { get { return _Password; } set { SetProperty(ref _Password, value); } } public bool IsLocal { get; private set; } public CardLoginWindow View { get; set; } //正在登录中 private bool _IsLogining; public bool IsLogining { get { return _IsLogining; } set { SetProperty(ref _IsLogining, value); } } private int _PageIndex; public int PageIndex { get { return _PageIndex; } set { SetProperty(ref _PageIndex, value); } } private Visibility _CardBtnVisibility = Visibility.Visible; public Visibility CardBtnVisibility { get { return _CardBtnVisibility; } set { SetProperty(ref _CardBtnVisibility, value); } } public SerialPortProtocol serialPortProtocol { get; set; } #endregion #region 命令 private DelegateCommand _LoginCommand; public DelegateCommand LoginCommand => _LoginCommand ?? (_LoginCommand = new DelegateCommand(ExecuteLoginCommand, CanExecuteLoginCommand).ObservesProperty(() => Name).ObservesProperty(() => Password).ObservesProperty(() => IsLogining)); private DelegateCommand _CloseCommand; public DelegateCommand CloseCommand => _CloseCommand ?? (_CloseCommand = new DelegateCommand(ExecuteCloseCommand)); private DelegateCommand _TransitionerCommand; public DelegateCommand TransitionerCommand => _TransitionerCommand ?? (_TransitionerCommand = new DelegateCommand(ExecuteTransitionerCommand)); private DelegateCommand _LoadedCommand; public DelegateCommand LoadedCommand => _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand)); #endregion #region 事件 #endregion public CardLoginWindowViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService, IConfigService configService) { _regionManager = regionManager; _regionNavigationService = regionNavigationService; _container = container; _dialogService = dialogService; _configService = configService; _eventAggregator = ea; if (httpClient == null) { httpClient = new System.Net.Http.HttpClient(); } } #region 方法 /// /// 加载 /// async void ExecuteLoadedCommand() { //获取刷卡器配置 var cardConfig = _configService.GetCardReaderConfig(); //创建刷卡器通讯 serialPortProtocol = new SerialPortProtocol(cardConfig); serialPortProtocol.DataReceived += CardSerial_DataReceived; await serialPortProtocol.ConnectAsync(); } private async void CardSerial_DataReceived(object sender, string code) { if (PageIndex == 0) { if (string.IsNullOrEmpty(code)) { return; } } } public void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { serialPortProtocol?.Dispose(); } async void ExecuteLoginCommand() { IsLogining = true; try { } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { IsLogining = false; } } bool CanExecuteLoginCommand() { return !string.IsNullOrEmpty(Name) && !string.IsNullOrEmpty(Password) && !IsLogining; } void ExecuteCloseCommand() { View.DialogResult = false; } void ExecuteTransitionerCommand(string parameter) { PageIndex = int.Parse(parameter); } public void SetModel(bool isAdmin) { PageIndex = isAdmin ? 1 : 0; CardBtnVisibility = isAdmin ? Visibility.Collapsed : Visibility.Visible; } #endregion } }