123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- using BizService;
- using PLCTool.Common;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- namespace PLCTool.ViewModels
- {
- public class LoginViewModel : BindableBase, IDialogAware
- {
- private readonly IUserService _iUserService;
- private readonly IUserRoleService _iUserRoleService;
- public LoginViewModel(IUserService iUserService,IUserRoleService iUserRoleService)
- {
- _iUserService = iUserService;
- _iUserRoleService = iUserRoleService;
- ExecuteCommand = new DelegateCommand<string>(Execute);
- }
- public string Title { set; get; } = "登录";
- public event Action<IDialogResult> RequestClose;
- public bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
- }
- public void OnDialogOpened(IDialogParameters parameters)
- {
- }
- private void Execute(string obj)
- {
- switch (obj)
- {
- case "Login": Login(); break;
- }
- }
- /// <summary>
- /// 登录
- /// </summary>
- async void Login()
- {
- if (string.IsNullOrWhiteSpace(UserName) ||
- string.IsNullOrWhiteSpace(PassWord))
- {
- MessageBox.Show("请输入用户名和密码!", "确认", MessageBoxButton.OK, MessageBoxImage.Warning);
- return;
- }
- //进度条设为可见
- int userId = _iUserService.FindDataByUserAndPass(UserName, PassWord);
- if (userId!=-1)
- {
- //roleID赋值给全局变量
- var userRole=_iUserRoleService.QueryList().FirstOrDefault(x=>x.UserId==userId);
- if(userRole!=null)
- {
- Appsession.RoleId = (int)userRole.RoleId;
- Appsession.UserName=userName;
- Appsession.UserId = userId;
- }
- RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
- }
- else
- {
- MessageBox.Show("用户名或密码错误!", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
- return;
- }
- }
- private string userName;
- public string UserName
- {
- get { return userName; }
- set { userName = value; RaisePropertyChanged(); }
- }
- private string password;
- public string PassWord
- {
- get { return password; }
- set { password = value; RaisePropertyChanged(); }
- }
- public DelegateCommand<string> ExecuteCommand { set; get; }
- }
- }
|