Files
CapMachine/CapMachine.Wpf/ViewModels/UserManageViewModel.cs
2024-12-18 15:50:21 +08:00

162 lines
4.5 KiB
C#

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.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)
{
//事件服务
_EventAggregator = eventAggregator;
FreeSql = freeSql;
ConfigService = configService;
DialogService = dialogService;
CurUserDto = new UserDto() { IsEnable = true };
}
private IEventAggregator _EventAggregator { get; set; }
public IFreeSql FreeSql { get; }
public ConfigService ConfigService { get; }
public IDialogService DialogService { 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("登录成功!", "提示");
CurUserDto.Level = ListUser.FirstOrDefault().Level;
CurUserDto.Password = "";
ConfigService.CurUserDto = CurUserDto;
}
else
{
MessageBox.Show("密码错误!", "提示");
}
}
else
{
MessageBox.Show("当前用户不存在!", "提示");
}
}
//UserManageCmd
private DelegateCommand _UserManageCmd;
/// <summary>
/// 用户登录命令
/// </summary>
public DelegateCommand UserManageCmd
{
set
{
_UserManageCmd = value;
}
get
{
if (_UserManageCmd == null)
{
_UserManageCmd = new DelegateCommand(() => UserManageCmdMethod());
}
return _UserManageCmd;
}
}
/// <summary>
/// 用户管理
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void UserManageCmdMethod()
{
if (CurUserDto != null && 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)
{
//取消
}
});
}
}
}