|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using BPASmartClient.CustomResource.Pages.Model;
- using BPASmartClient.Helper;
- using Microsoft.Toolkit.Mvvm.ComponentModel;
- using Microsoft.Toolkit.Mvvm.Input;
-
- namespace BPASmartClient.CustomResource.Pages.ViewModel
- {
- public class LoginViewModel : ObservableObject
- {
- public LoginViewModel()
- {
- ActionManage.GetInstance.Register(new Action(() => { Login(); }), "EnterLogin", true);
- LoginCommand = new RelayCommand(() => { Login(); });
-
- ExitCommand = new RelayCommand(() =>
- {
- ActionManage.GetInstance.Send("ExitAction");
- });
-
- }
-
- public static void NfcLogin(string cardId)
- {
-
- var res = Global.userManager.userInfos.FirstOrDefault(p => p.CardId == cardId);
- if (res != null)
- {
- Global.userInfo = res;
- //permission权限
- for (int i = 0; i < MenuManage.GetInstance.menuModels.Count; i++)
- {
- if (MenuManage.GetInstance.menuModels.ElementAt(i).MainMenuPermission.Contains(res.permission))
- MenuManage.GetInstance.menuModels.ElementAt(i).MainMenuVisibility = Visibility.Visible;
- else
- MenuManage.GetInstance.menuModels.ElementAt(i).MainMenuVisibility = Visibility.Collapsed;
-
- if (MenuManage.GetInstance.menuModels.ElementAt(i).subMenumodels.FirstOrDefault(p => p.SubMenuPermission.Contains(res.permission)) == null)
- MenuManage.GetInstance.menuModels.ElementAt(i).MainMenuVisibility = Visibility.Collapsed;
-
- for (int m = 0; m < MenuManage.GetInstance.menuModels.ElementAt(i).subMenumodels.Count; m++)
- {
- if (MenuManage.GetInstance.menuModels.ElementAt(i).subMenumodels.ElementAt(m).SubMenuPermission.Contains(res.permission))
- MenuManage.GetInstance.menuModels.ElementAt(i).subMenumodels.ElementAt(m).SubMenuVisibility = Visibility.Visible;
- else
- MenuManage.GetInstance.menuModels.ElementAt(i).subMenumodels.ElementAt(m).SubMenuVisibility = Visibility.Collapsed;
- }
- }
- //分配后的权限
- if (res.userTreeViewModels != null)
- {
- if (res.userTreeViewModels.Count > 0)
- {
- foreach (var items in MenuManage.GetInstance.menuModels)
- {
- foreach (var item in items.subMenumodels)
- {
- items.MainMenuVisibility = Visibility.Visible;
- if (res.userTreeViewModels.FirstOrDefault(p => p.Name == item.SubMenuName) != null)
- {
- item.SubMenuVisibility = Visibility.Visible;
- }
- }
- }
- }
- }
- Application.Current.Dispatcher.Invoke(new Action(() => { ActionManage.GetInstance.Send("LoginOk"); }));
-
- }
- }
-
- private void Login()
- {
- var res = Global.userManager.userInfos.FirstOrDefault(p => p.UserName == UserName && p.Password == Password);
- if (res != null)
- {
- Global.userInfo = res;
-
- Global.userInfo.LastLogInTime = DateTime.Now.ToString();
- res.LastLogInTime = DateTime.Now.ToString();
- Config.GetInstance.SaveUser();
- //permission权限
- for (int i = 0; i < MenuManage.GetInstance.menuModels.Count; i++)
- {
- if (MenuManage.GetInstance.menuModels.ElementAt(i).MainMenuPermission.Contains(res.permission))
- MenuManage.GetInstance.menuModels.ElementAt(i).MainMenuVisibility = Visibility.Visible;
- else
- MenuManage.GetInstance.menuModels.ElementAt(i).MainMenuVisibility = Visibility.Collapsed;
-
- if (MenuManage.GetInstance.menuModels.ElementAt(i).subMenumodels.FirstOrDefault(p => p.SubMenuPermission.Contains(res.permission)) == null)
- MenuManage.GetInstance.menuModels.ElementAt(i).MainMenuVisibility = Visibility.Collapsed;
-
- for (int m = 0; m < MenuManage.GetInstance.menuModels.ElementAt(i).subMenumodels.Count; m++)
- {
- if (MenuManage.GetInstance.menuModels.ElementAt(i).subMenumodels.ElementAt(m).SubMenuPermission.Contains(res.permission))
- MenuManage.GetInstance.menuModels.ElementAt(i).subMenumodels.ElementAt(m).SubMenuVisibility = Visibility.Visible;
- else
- MenuManage.GetInstance.menuModels.ElementAt(i).subMenumodels.ElementAt(m).SubMenuVisibility = Visibility.Collapsed;
- }
- }
-
- //分配后的权限
- if (res.permission == Enums.Permission.管理员)
- {
- foreach( var items in MenuManage.GetInstance.menuModels)
- {
- items.MainMenuVisibility = Visibility.Visible;
- foreach(var item in items.subMenumodels)
- {
- item.SubMenuVisibility = Visibility.Visible;
- }
- }
- }
- else
- {
- if (res.userTreeViewModels != null)
- {
- if (res.userTreeViewModels.Count > 0)
- {
- foreach (var items in MenuManage.GetInstance.menuModels)
- {
- foreach (var item in items.subMenumodels)
- {
- items.MainMenuVisibility = Visibility.Visible;
- if (res.userTreeViewModels.FirstOrDefault(p => p.Name == item.SubMenuName) != null)
- {
- item.SubMenuVisibility = Visibility.Visible;
- }
- }
- }
- }
- }
- }
-
-
- ActionManage.GetInstance.Send("PermissionChange");
- ActionManage.GetInstance.Send("LoginOk");
-
- }
- else
- {
- ErrorInfo = "用户名或密码错误!";
- }
- }
-
- public RelayCommand LoginCommand { get; set; }
-
- public RelayCommand ExitCommand { get; set; }
-
-
-
- public string ErrorInfo { get { return _mErrorInfo; } set { _mErrorInfo = value; OnPropertyChanged(); } }
- private string _mErrorInfo;
-
-
- public string UserName { get { return _mUserName; } set { _mUserName = value; OnPropertyChanged(); } }
- private string _mUserName = Global.userInfo.UserName;
-
-
- public string Password { get { return _mPassword; } set { _mPassword = value; OnPropertyChanged(); } }
- private string _mPassword;
-
-
- }
- }
|