WPF实战项目十四(API篇):登录注册接口

1、新建UserDto.cs

public class UserDto : 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(); }
		}

	}

2、新增ILoginService接口

   public interface ILoginService
    {
        Task LoginAsync(string Account, string Password);
        Task RegisterAsync(UserDto userDto);

    }

3、实现LoginService接口

public class LoginService : ILoginService
    {
        private readonly IUnitOfWork unitOfWork;
        private readonly IMapper mapper;
        public LoginService(IUnitOfWork unitOfWork, IMapper mapper)
        {
            this.unitOfWork = unitOfWork;
            this.mapper = mapper;
        }
        /// 
        /// 登录
        /// 
        /// 
        /// 
        /// 
        public async Task LoginAsync(string Account, string Password)
        {
            try
            {
                var repository = unitOfWork.GetRepository();
                var model = await repository.GetFirstOrDefaultAsync(predicate: t => t.Account.Equals(Account) && t.PassWord.Equals(Password));
                if(model == null)
                {
                    return new ApiResponse(false, "用户名或密码错误!");
                }
                else
                {
                    return new ApiResponse(true, model);
                }
            }
            catch (Exception ex)
            {
                return new ApiResponse(false, ex.Message);
            }
        }
        /// 
        /// 注册
        /// 
        /// 
        /// 
        /// 
        public async Task RegisterAsync(UserDto userDto)
        {
            try
            {
                var user = mapper.Map(userDto);
                var repository = unitOfWork.GetRepository();
                var userModel = await repository.GetFirstOrDefaultAsync(predicate: t => t.Account.Equals(user.Account));
                if(userModel != null)
                {
                    return new ApiResponse(false, $"当前账号{user.Account}已存在,请重新注册!");
                }
                else
                {
                    user.CreateDate = DateTime.Now;
                    await repository.InsertAsync(user);
                    if(await unitOfWork.SaveChangesAsync() > 0)
                    {
                        return new ApiResponse(true, user);
                    }
                    else
                    {
                        return new ApiResponse(false, "注册失败!");
                    }
                }
            }
            catch (Exception ex)
            {
                return new ApiResponse(false, ex.Message);
            }
        }
    }

4、新增登录控制器LoginController

/// 
    /// 登录
    /// 
    public class LoginController : BaseApiController
    {
        private readonly ILoginService loginService;

        public LoginController(ILoginService loginService)
        {
            this.loginService = loginService;
        }

        [HttpGet]
        public async Task Login(string Account, string PassWord)
        {
            return await loginService.LoginAsync(Account, PassWord);
        }
        [HttpPost]
        public async Task Register([FromBody] UserDto userDto)
        {
            return await loginService.RegisterAsync(userDto);
        }
    }

5、添加AutoMapper映射关系

  CreateMap().ReverseMap();

6、在program.cs里面添加服务

builder.Services.AddTransient();

7、F5运行项目

WPF实战项目十四(API篇):登录注册接口_第1张图片WPF实战项目十四(API篇):登录注册接口_第2张图片WPF实战项目十四(API篇):登录注册接口_第3张图片WPF实战项目十四(API篇):登录注册接口_第4张图片WPF实战项目十四(API篇):登录注册接口_第5张图片WPF实战项目十四(API篇):登录注册接口_第6张图片

你可能感兴趣的:(WPF,.netcore,webapi,.netcore)