LoginViewModel.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using BizService;
  2. using PLCTool.Common;
  3. using Prism.Commands;
  4. using Prism.Mvvm;
  5. using Prism.Services.Dialogs;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. namespace PLCTool.ViewModels
  13. {
  14. public class LoginViewModel : BindableBase, IDialogAware
  15. {
  16. private readonly IUserService _iUserService;
  17. private readonly IUserRoleService _iUserRoleService;
  18. public LoginViewModel(IUserService iUserService,IUserRoleService iUserRoleService)
  19. {
  20. _iUserService = iUserService;
  21. _iUserRoleService = iUserRoleService;
  22. ExecuteCommand = new DelegateCommand<string>(Execute);
  23. }
  24. public string Title { set; get; } = "登录";
  25. public event Action<IDialogResult> RequestClose;
  26. public bool CanCloseDialog()
  27. {
  28. return true;
  29. }
  30. public void OnDialogClosed()
  31. {
  32. }
  33. public void OnDialogOpened(IDialogParameters parameters)
  34. {
  35. }
  36. private void Execute(string obj)
  37. {
  38. switch (obj)
  39. {
  40. case "Login": Login(); break;
  41. }
  42. }
  43. /// <summary>
  44. /// 登录
  45. /// </summary>
  46. async void Login()
  47. {
  48. if (string.IsNullOrWhiteSpace(UserName) ||
  49. string.IsNullOrWhiteSpace(PassWord))
  50. {
  51. MessageBox.Show("请输入用户名和密码!", "确认", MessageBoxButton.OK, MessageBoxImage.Warning);
  52. return;
  53. }
  54. //进度条设为可见
  55. int userId = _iUserService.FindDataByUserAndPass(UserName, PassWord);
  56. if (userId!=-1)
  57. {
  58. //roleID赋值给全局变量
  59. var userRole=_iUserRoleService.QueryList().FirstOrDefault(x=>x.UserId==userId);
  60. if(userRole!=null)
  61. {
  62. Appsession.RoleId = (int)userRole.RoleId;
  63. Appsession.UserName=userName;
  64. Appsession.UserId = userId;
  65. }
  66. RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
  67. }
  68. else
  69. {
  70. MessageBox.Show("用户名或密码错误!", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
  71. return;
  72. }
  73. }
  74. private string userName;
  75. public string UserName
  76. {
  77. get { return userName; }
  78. set { userName = value; RaisePropertyChanged(); }
  79. }
  80. private string password;
  81. public string PassWord
  82. {
  83. get { return password; }
  84. set { password = value; RaisePropertyChanged(); }
  85. }
  86. public DelegateCommand<string> ExecuteCommand { set; get; }
  87. }
  88. }