创建注册登录接口
添加文件
创建文件
+ MyToDo.Api
./Controllers/LoginController.cs
./Service/ILoginService.cs
./Service/LoginService.cs
-
MyToDo.Share
./Dtos/UserDto.cs
LoginController.cs
using Microsoft.AspNetCore.Mvc;
using MyToDo.Api.Context;
using MyToDo.Api.Service;
using MyToDo.Share.Dtos;
using MyToDo.Share.Parameters;namespace MyToDo.Api.Controllers
{[ApiController][Route("api/[controller]/[action]")]public class LoginController : ControllerBase{private readonly ILoginService service;public LoginController(ILoginService tService){this.service = tService;}[HttpGet]public async Task<ApiReponse> LoginAsync(string Account, string PassWord) => await service.LoginAsync(Account,PassWord);[HttpPost]public async Task<ApiReponse> Resgiter([FromBody] UserDto param) => await service.Resgiter(param);}
}
ILoginService.cs
using MyToDo.Share.Dtos;namespace MyToDo.Api.Service
{public interface ILoginService{/// <summary>/// 登录/// </summary>/// <param name="Account">登录名</param>/// <param name="PassWord">登录密码</param>/// <returns></returns>Task<ApiReponse> LoginAsync(string Account,string PassWord);/// <summary>/// 注册/// </summary>/// <returns></returns>Task<ApiReponse> Resgiter(UserDto user);}
}
LoginService.cs
using Arch.EntityFrameworkCore.UnitOfWork;
using AutoMapper;
using MyToDo.Api.Context;
using MyToDo.Share.Dtos;
using System.Diagnostics.Eventing.Reader;namespace MyToDo.Api.Service
{public class LoginService : ILoginService{private readonly IUnitOfWork work;private readonly IMapper mapper;public LoginService(IUnitOfWork work, IMapper mapper){this.work = work;this.mapper = mapper;}public async Task<ApiReponse> LoginAsync(string Account, string PassWord){try{if(Account==null || PassWord==null)return new ApiReponse("账号或密码为空", false);var model = await work.GetRepository<User>().GetFirstOrDefaultAsync(predicate: x => (x.Account.Equals(Account)) && (x.Password.Equals(PassWord)));if(model == null){return new ApiReponse("账号或密码错误",false); }return new ApiReponse(true,model);}catch (Exception ex){return new ApiReponse("登录失败", false); ;}}public async Task<ApiReponse> Resgiter(UserDto user){try{var model = mapper.Map<User>(user);var repository = work.GetRepository<User>();//.GetFirstOrDefaultAsync(predicate: x => x.Account.Equals(model.Account));var usermodel = await repository.GetFirstOrDefaultAsync(predicate: x => x.Account.Equals(model.Account));if(usermodel != null) return new ApiReponse("当前账户已存在",false); model.CreateDate = DateTime.Now;await repository.InsertAsync(model);if(await work.SaveChangesAsync()>0)return new ApiReponse(true, model);return new ApiReponse("注册失败,请稍后重试", false);}catch (Exception ex){return new ApiReponse($"注册失败.{ex.Message}", false);}}}
}
UserDto.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace MyToDo.Share.Dtos
{public class UserDto:BaseDto{private string? userName;private string? passWord;private string? account;/// <summary>/// 账户/// </summary>public string? Account{get { return account; }set { account = value; OnPropertyChanged(); }}/// <summary>/// 密码/// </summary>public string PassWord{get { return passWord; }set { passWord = value; OnPropertyChanged(); }}/// <summary>/// 用户名/// </summary>public string UserName{get { return userName; }set { userName = value; OnPropertyChanged(); }}}
}
依赖注入
Program.cs 添加
.AddCustomRepository<User, UserRepository>();
AutoMapperProfilec.s 添加
builder.Services.AddTransient<ILoginService, LoginService>();