LoginPageViewModel.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using HandyControl.Controls;
  2. using LogoForceTestApp.Core;
  3. using LogoForceTestApp.Core.Mvvm;
  4. using LogoForceTestApp.Modules.MainModule.Models;
  5. using LogoForceTestApp.Modules.MainModule.Views;
  6. using Prism.Commands;
  7. using Prism.Events;
  8. using Prism.Regions;
  9. using System;
  10. using System.Collections.Generic;
  11. namespace LogoForceTestApp.Modules.MainModule.ViewModels
  12. {
  13. public class LoginPageViewModel : RegionViewModelBase
  14. {
  15. public string Password { get; set; }
  16. public List<Role> Roles { get; set; }
  17. public DelegateCommand GoBackCommand { get; set; }
  18. public DelegateCommand LoginCommand { get; set; }
  19. public Role UserRole { get; set; }
  20. public LoginPageViewModel(IRegionManager regionManager,IEventAggregator eventAggregator) : base(regionManager)
  21. {
  22. Roles = new List<Role>();
  23. foreach (Role item in Enum.GetValues(typeof(Role)))
  24. {
  25. Roles.Add(item);
  26. }
  27. GoBackCommand = new DelegateCommand(GoBack);
  28. LoginCommand = new DelegateCommand(Login);
  29. _eventAggregator = eventAggregator;
  30. }
  31. private void Login()
  32. {
  33. if (string.IsNullOrWhiteSpace(Password))
  34. {
  35. Growl.Warning($"{nameof(Password)}不能为空");
  36. }
  37. if (Password == "123456")
  38. {
  39. CurrentUser.CurrentUserName = UserRole.ToString();
  40. Growl.Info($"{UserRole}登录成功");
  41. _eventAggregator.GetEvent<LoginSuccessEvent>().Publish(true);
  42. if (CurrentUser.CurrentUserName == "Admin")//(CurrentUser.CurrentUserName== "Engineer" || CurrentUser.CurrentUserName=="Admin")
  43. {
  44. GoBack();
  45. }
  46. else
  47. {
  48. RegionManager.RequestNavigate(RegionNames.ContentRegion, nameof(MainPage));
  49. }
  50. }
  51. else
  52. {
  53. Growl.Warning($"{UserRole}登录失败:密码错误");
  54. CurrentUser.CurrentUserName = string.Empty;
  55. _eventAggregator.GetEvent<LoginSuccessEvent>().Publish(false);
  56. }
  57. }
  58. private IRegionNavigationJournal _regionNavigationJournal;
  59. private readonly IEventAggregator _eventAggregator;
  60. private void GoBack()
  61. {
  62. _regionNavigationJournal?.GoBack();
  63. }
  64. public override void OnNavigatedFrom(NavigationContext navigationContext)
  65. {
  66. base.OnNavigatedFrom(navigationContext);
  67. }
  68. public override void OnNavigatedTo(NavigationContext navigationContext)
  69. {
  70. _regionNavigationJournal = navigationContext.NavigationService.Journal;
  71. }
  72. }
  73. }