using HandyControl.Controls; using LogoForceTestApp.Core; using LogoForceTestApp.Core.Mvvm; using LogoForceTestApp.Modules.MainModule.Models; using LogoForceTestApp.Modules.MainModule.Views; using Prism.Commands; using Prism.Events; using Prism.Regions; using System; using System.Collections.Generic; namespace LogoForceTestApp.Modules.MainModule.ViewModels { public class LoginPageViewModel : RegionViewModelBase { public string Password { get; set; } public List Roles { get; set; } public DelegateCommand GoBackCommand { get; set; } public DelegateCommand LoginCommand { get; set; } public Role UserRole { get; set; } public LoginPageViewModel(IRegionManager regionManager,IEventAggregator eventAggregator) : base(regionManager) { Roles = new List(); foreach (Role item in Enum.GetValues(typeof(Role))) { Roles.Add(item); } GoBackCommand = new DelegateCommand(GoBack); LoginCommand = new DelegateCommand(Login); _eventAggregator = eventAggregator; } private void Login() { if (string.IsNullOrWhiteSpace(Password)) { Growl.Warning($"{nameof(Password)}不能为空"); } if (Password == "123456") { CurrentUser.CurrentUserName = UserRole.ToString(); Growl.Info($"{UserRole}登录成功"); _eventAggregator.GetEvent().Publish(true); if (CurrentUser.CurrentUserName == "Admin")//(CurrentUser.CurrentUserName== "Engineer" || CurrentUser.CurrentUserName=="Admin") { GoBack(); } else { RegionManager.RequestNavigate(RegionNames.ContentRegion, nameof(MainPage)); } } else { Growl.Warning($"{UserRole}登录失败:密码错误"); CurrentUser.CurrentUserName = string.Empty; _eventAggregator.GetEvent().Publish(false); } } private IRegionNavigationJournal _regionNavigationJournal; private readonly IEventAggregator _eventAggregator; private void GoBack() { _regionNavigationJournal?.GoBack(); } public override void OnNavigatedFrom(NavigationContext navigationContext) { base.OnNavigatedFrom(navigationContext); } public override void OnNavigatedTo(NavigationContext navigationContext) { _regionNavigationJournal = navigationContext.NavigationService.Journal; } } }