登录、注册服务添加
- 添加注册数据类型
- 添加注册UI
- 修改bug
- UserDto的UserName更改为可null类型
- Resgiter 添加加密方法
- 修改控制器
添加注册数据类型
添加文件MyToDo.Share.Models.ResgiterUserDto.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace MyToDo.Share.Models
{public class ResgiterUserDto: BaseDto{private string userName;public string UserName{get { return userName; }set { userName = value; OnPropertyChanged(); }}private string account;public string Account{get { return account; }set { account = value; OnPropertyChanged(); }}private string passWord;public string PassWord{get { return passWord; }set { passWord = value; OnPropertyChanged(); }}private string newpassWord;public string NewPassWord{get { return newpassWord; }set { newpassWord = value; OnPropertyChanged(); }}}
}
添加注册UI
修改文件:Mytodo.Views.LoginView.xaml
<UserControlx:Class="Mytodo.Views.LoginView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:i="http://schemas.microsoft.com/xaml/behaviors"xmlns:local="clr-namespace:Mytodo.Views"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:md="http://materialdesigninxaml.net/winfx/xaml/themes"xmlns:pass="clr-namespace:Mytodo.Extensions"xmlns:prism="http://prismlibrary.com/"mc:Ignorable="d"><prism:Dialog.WindowStyle><Style TargetType="Window"><Setter Property="Width" Value="600" /><Setter Property="Height" Value="400" /><Setter Property="SizeToContent" Value="WidthAndHeight" /><Setter Property="ResizeMode" Value="NoResize" /><Setter Property="prism:Dialog.WindowStartupLocation" Value="CenterScreen" /></Style></prism:Dialog.WindowStyle><Grid Width="350" Height="400"><md:Snackbarx:Name="LoginSnakeBar"Grid.ColumnSpan="2"Panel.ZIndex="1"MessageQueue="{md:MessageQueue}" /><md:Transitioner SelectedIndex="{Binding SelectIndex, FallbackValue=0}"><md:TransitionerSlide><DockPanel Margin="15,5,15,20" VerticalAlignment="Center"><md:PackIconWidth="100"Height="80"HorizontalAlignment="Center"VerticalAlignment="Center"md:ThemeAssist.Theme="Dark"DockPanel.Dock="Top"Foreground="SpringGreen"Kind="AccountOutline" /><TextBlockMargin="0,10"DockPanel.Dock="Top"FontSize="22"FontWeight="Bold"Text="欢迎使用" /><TextBoxMargin="0,10"md:HintAssist.Hint="请输入账号"DockPanel.Dock="Top"Text="{Binding Account}" /><PasswordBoxMargin="0,10"md:HintAssist.Hint="请输入密码"pass:PassWordExtensions.PassWord="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"DockPanel.Dock="Top"><i:Interaction.Behaviors><pass:PasswordBehavior /></i:Interaction.Behaviors></PasswordBox><ButtonCommand="{Binding ExecuteCommand}"CommandParameter="Login"Content="登录系统"DockPanel.Dock="Top" /><DockPanel Margin="0,5" LastChildFill="False"><TextBlock Text="注册账号"><i:Interaction.Triggers><i:EventTrigger EventName="MouseLeftButtonDown"><i:InvokeCommandAction Command="{Binding ExecuteCommand}" CommandParameter="ResgiterPage" /></i:EventTrigger></i:Interaction.Triggers></TextBlock><TextBlock DockPanel.Dock="Right" Text="忘记密码?" /></DockPanel></DockPanel></md:TransitionerSlide><md:TransitionerSlide><DockPanel Margin="15" VerticalAlignment="Center"><md:PackIconWidth="100"Height="80"HorizontalAlignment="Center"VerticalAlignment="Center"md:ThemeAssist.Theme="Dark"DockPanel.Dock="Top"Foreground="SpringGreen"Kind="AccountOutline" /><TextBlockMargin="0,10"DockPanel.Dock="Top"FontSize="22"FontWeight="Bold"Text="注册账号" /><TextBoxMargin="0,5"md:HintAssist.Hint="请输入用户名"DockPanel.Dock="Top"Text="{Binding RUserDto.Account}" /><TextBoxMargin="0,5"md:HintAssist.Hint="请输入账号"DockPanel.Dock="Top"Text="{Binding RUserDto.UserName}" /><PasswordBoxMargin="0,5"md:HintAssist.Hint="请输入密码"pass:PassWordExtensions.PassWord="{Binding RUserDto.PassWord, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"DockPanel.Dock="Top"><i:Interaction.Behaviors><pass:PasswordBehavior /></i:Interaction.Behaviors></PasswordBox><PasswordBoxMargin="0,5"md:HintAssist.Hint="请再次输入密码"pass:PassWordExtensions.PassWord="{Binding RUserDto.NewPassWord, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"DockPanel.Dock="Top"><i:Interaction.Behaviors><pass:PasswordBehavior /></i:Interaction.Behaviors></PasswordBox><ButtonCommand="{Binding ExecuteCommand}"CommandParameter="Resgiter"Content="注册账号"DockPanel.Dock="Top" /><ButtonMargin="0,10"Command="{Binding ExecuteCommand}"CommandParameter="Return"Content="返回登录"DockPanel.Dock="Top"Style="{StaticResource MaterialDesignOutlinedButton}" /></DockPanel></md:TransitionerSlide></md:Transitioner></Grid>
</UserControl>
添加注册、登录、退出等功能实现以及功能的字段
using AutoMapper;
using Mytodo.Extensions;
using Mytodo.Service;
using MyToDo.Api.Context;
using MyToDo.Share;
using MyToDo.Share.Models;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Shapes;namespace Mytodo.ViewModels
{public class LoginViewModel : BindableBase, IDialogAware{#region 定义命令/// <summary>/// 执行登录|推出等相关命令/// </summary>public DelegateCommand<string> ExecuteCommand { get; set; }#endregion#region 定义属性public int SelectIndex{get { return selectIndex; }set { selectIndex = value; RaisePropertyChanged(); }}/// <summary>/// 注册区域字段/// </summary>public ResgiterUserDto RUserDto{get { return rUserDto; }set { rUserDto = value; RaisePropertyChanged(); }}public string Password{get { return password; }set { password = value; }}public string Account{get { return username; }set { username = value; }}#endregion#region 定义重要字段#endregion#region 定义普通字段private int selectIndex;private ResgiterUserDto rUserDto;private string password;private string username;private readonly ILoginService loginService;private readonly IEventAggregator aggregator;#endregion#region 命令方法/// <summary>/// ExecuteCommand对应的方法/// </summary>/// <param name="obj"></param>private void Execute(string obj){switch (obj){case "Login": Login(); break;case "LoginOut": LoginOut(); break;case "Resgiter": Resgiter(); break;case "ResgiterPage": SelectIndex = 1; break;case "Return": SelectIndex = 0; break;}}async private void Resgiter(){if (string.IsNullOrWhiteSpace(RUserDto.Account) ||string.IsNullOrWhiteSpace(RUserDto.UserName) ||string.IsNullOrWhiteSpace(RUserDto.PassWord) ||string.IsNullOrWhiteSpace(RUserDto.NewPassWord)){//aggregator.SendMessage("请输入完整的注册信息!", "Login");return;}if (RUserDto.PassWord != RUserDto.NewPassWord){//aggregator.SendMessage("密码不一致,请重新输入!", "Login");return;}var resgiterResult = await loginService.Resgiter(new UserDto(){Account = RUserDto.Account,UserName = RUserDto.UserName,PassWord = RUserDto.PassWord});if (resgiterResult != null && resgiterResult.Status){//aggregator.SendMessage("注册成功", "Login");//注册成功,返回登录页页面SelectIndex = 0;}// else//aggregator.SendMessage(resgiterResult.Message, "Login");}async private void LoginOut(){if (string.IsNullOrWhiteSpace(Account) ||string.IsNullOrWhiteSpace(Password)){return;}var loginResult = await loginService.Login(new UserDto(){Account = Account,PassWord = Password});if (loginResult != null && loginResult.Status){RequestClose?.Invoke(new DialogResult(ButtonResult.OK));}else{//登录失败提示...//aggregator.SendMessage(loginResult.Message, "Login");}}async private void Login(){if (string.IsNullOrWhiteSpace(Account) ||string.IsNullOrWhiteSpace(Password)){return;}var loginResult = await loginService.Login(new UserDto(){Account = Account,PassWord = Password});if (loginResult != null && loginResult.Status){RequestClose?.Invoke(new DialogResult(ButtonResult.OK));}else{//登录失败提示...//aggregator.SendMessage(loginResult.Message, "Login");}}#endregion#region 启动项#endregion#region 继承public string Title { get; set; } = "Todo";public event Action<IDialogResult> RequestClose;public bool CanCloseDialog(){return true;}public void OnDialogClosed(){LoginOut();}public void OnDialogOpened(IDialogParameters parameters){}#endregionpublic LoginViewModel(ILoginService loginService, IEventAggregator aggregator){//初始化对象RUserDto=new ResgiterUserDto();ExecuteCommand = new DelegateCommand<string> (Execute);this.loginService = loginService;this.aggregator = aggregator;}}
}
修改bug
-
UserDto的UserName更改为可null类型
修改文件:MyToDo.Share.Models.UserDto.cs
public string UserName
–>
public string? UserName
-
Resgiter 添加加密方法
修改文件:MyToDo.Api.Service.cs
在
model.CreateDate = DateTime.Now;
后添加model.Password = model.Password.GetMD5();
-
修改控制器
修改文件:MyToDo.Api.Controllers.LoginController
[HttpGet]public async Task<ApiReponse> LoginAsync(string Account, string PassWord) => await service.LoginAsync(Account,PassWord);
修改为
[HttpPost]public async Task<ApiReponse> Login([FromBody] UserDto param) => await service.LoginAsync(param.Account, param.PassWord);