Files
CapMachine/CapMachine.Wpf/ViewModels/UserManageViewModel.cs
2026-03-27 12:39:12 +08:00

204 lines
6.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using CapMachine.Core;
using CapMachine.Model;
using CapMachine.Wpf.Dtos;
using CapMachine.Wpf.Services;
using Masuit.Tools;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace CapMachine.Wpf.ViewModels
{
public class UserManageViewModel : NavigationViewModel
{
public UserManageViewModel(IEventAggregator eventAggregator, IFreeSql freeSql, ConfigService configService, IDialogService dialogService, IRegionManager regionManager)
{
//事件服务
_EventAggregator = eventAggregator;
FreeSql = freeSql;
ConfigService = configService;
DialogService = dialogService;
RegionManager = regionManager;
CurUserDto = new UserDto() { IsEnable = true };
}
private IEventAggregator _EventAggregator { get; set; }
public IFreeSql FreeSql { get; }
public ConfigService ConfigService { get; }
public IDialogService DialogService { get; }
public IRegionManager RegionManager { get; }
/// <summary>
/// 当前用户
/// </summary>
public UserDto CurUserDto { get; set; }
private DelegateCommand _LoginCmd;
/// <summary>
/// 用户登录命令
/// </summary>
public DelegateCommand LoginCmd
{
set
{
_LoginCmd = value;
}
get
{
if (_LoginCmd == null)
{
_LoginCmd = new DelegateCommand(() => LoginCmdMethod());
}
return _LoginCmd;
}
}
/// <summary>
/// 用户登录信息
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void LoginCmdMethod()
{
if (string.IsNullOrEmpty(CurUserDto.Name))
{
MessageBox.Show("用户为空");
return;
}
if (string.IsNullOrEmpty(CurUserDto.Password))
{
MessageBox.Show("密码为空");
return;
}
var ListUser = FreeSql.Select<User>().Where(a => a.Name == CurUserDto.Name!.Trim() && a.IsEnable == true).ToList();
if (ListUser != null && ListUser.Count() > 0)
{
if (ListUser.FirstOrDefault().Password == CurUserDto.Password.Trim())
{
MessageBox.Show("登录成功!", "提示");
var loginUser = ListUser.FirstOrDefault();
CurUserDto.Id = loginUser.Id;
CurUserDto.Name = loginUser.Name;
CurUserDto.Level = loginUser.Level;
CurUserDto.IsEnable = loginUser.IsEnable;
CurUserDto.Password = "";
ConfigService.CurUserDto = CurUserDto;
ConfigService.IsUserLoggedIn = true;
_UserManageCmd?.RaiseCanExecuteChanged();
RegionManager.RequestNavigate("MainViewContentRegion", "MonitorView");
}
else
{
ConfigService.IsUserLoggedIn = false;
_UserManageCmd?.RaiseCanExecuteChanged();
MessageBox.Show("密码错误!", "提示");
}
}
else
{
ConfigService.IsUserLoggedIn = false;
_UserManageCmd?.RaiseCanExecuteChanged();
MessageBox.Show("当前用户不存在!", "提示");
}
}
//UserManageCmd
private DelegateCommand _UserManageCmd;
/// <summary>
/// 用户登录命令
/// </summary>
public DelegateCommand UserManageCmd
{
set
{
_UserManageCmd = value;
}
get
{
if (_UserManageCmd == null)
{
_UserManageCmd = new DelegateCommand(() => UserManageCmdMethod(), () => CanUserManage());
}
return _UserManageCmd;
}
}
/// <summary>
/// 判断当前用户是否可以进行用户管理
/// </summary>
/// <returns>true可管理false不可管理</returns>
private bool CanUserManage()
{
return ConfigService.IsUserLoggedIn &&
ConfigService.CurUserDto != null &&
ConfigService.CurUserDto.Level == "管理员";
}
/// <summary>
/// 用户管理
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void UserManageCmdMethod()
{
if (!ConfigService.IsUserLoggedIn)
{
MessageBox.Show("请先登录后再进行用户管理!", "提示");
return;
}
if (ConfigService.CurUserDto != null && ConfigService.CurUserDto.Level == "管理员")
{
ShowDialogExpInfo();
}
else
{
MessageBox.Show("登录的管理员才有权限编辑用户信息!", "提示");
}
}
private void ShowDialogExpInfo()
{
//弹窗
DialogService.ShowDialog("DialogUserView", new DialogParameters() { { "Name", "" } }, (par) =>
{
if (par.Result == ButtonResult.OK)
{
//用户信息
//var ListChartTabGroupDto = par.Parameters.GetValue<ExpInfoDto>("ReturnValue");
}
else if (par.Result == ButtonResult.Cancel)
{
//取消
}
});
}
/// <summary>
/// 导航到当前页面时刷新用户管理按钮状态
/// </summary>
/// <param name="navigationContext">导航上下文</param>
public override void OnNavigatedTo(NavigationContext navigationContext)
{
_UserManageCmd?.RaiseCanExecuteChanged();
base.OnNavigatedTo(navigationContext);
}
}
}