WPF实战学习笔记30-登录、注册服务添加

登录、注册服务添加

  • 添加注册数据类型
  • 添加注册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


    
        
    
    
        
        
            
                
                    
                    

                    
                    
                        
                            
                        
                    

                    

添加注册、登录、退出等功能实现以及功能的字段

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 定义命令
            /// 
            /// 执行登录|推出等相关命令
            /// 
            public DelegateCommand ExecuteCommand { get; set; }
        #endregion

            #region 定义属性

            public int SelectIndex
        {
            get { return selectIndex; }
            set { selectIndex = value; RaisePropertyChanged(); }
        }

        /// 
        /// 注册区域字段
        /// 
        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 命令方法
            /// 
            /// ExecuteCommand对应的方法
            /// 
            /// 
            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 RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            LoginOut();
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {

        }
        #endregion

            public LoginViewModel(ILoginService loginService, IEventAggregator aggregator)
        {
            //初始化对象
            RUserDto=new ResgiterUserDto();


            ExecuteCommand = new DelegateCommand (Execute);
            this.loginService = loginService;
            this.aggregator = aggregator;
        }

    }
}

修改bug

  1. UserDto的UserName更改为可null类型

    修改文件:MyToDo.Share.Models.UserDto.cs

    public string UserName
    

    –>

		public string? UserName
  1. Resgiter 添加加密方法

    修改文件:MyToDo.Api.Service.cs

    model.CreateDate = DateTime.Now;后添加

    model.Password = model.Password.GetMD5();
    
  2. 修改控制器

    修改文件:MyToDo.Api.Controllers.LoginController

     [HttpGet]
            public async Task LoginAsync(string Account, string PassWord) => await service.LoginAsync(Account,PassWord);
    

    修改为

    [HttpPost]
            public async Task Login([FromBody] UserDto param) => await service.LoginAsync(param.Account, param.PassWord);
    

你可能感兴趣的:(WPF实战学习笔记,wpf,学习,笔记)