1、新建UserDto.cs
public class UserDto : BaseDto
{
private string userName;
/// <summary>
/// 用户名
/// </summary>
public string UserName
{
get { return userName; }
set { userName = value;OnPropertyChanged(); }
}
private string account;
/// <summary>
/// 账户
/// </summary>
public string Account
{
get { return account; }
set { account = value; OnPropertyChanged(); }
}
private string password;
/// <summary>
/// 密码
/// </summary>
public string Password
{
get { return password; }
set { password = value; OnPropertyChanged(); }
}
}
2、新增ILoginService接口
public interface ILoginService
{
Task<ApiResponse> LoginAsync(string Account, string Password);
Task<ApiResponse> 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;
}
/// <summary>
/// 登录
/// </summary>
/// <param name="Account"></param>
/// <param name="Password"></param>
/// <returns></returns>
public async Task<ApiResponse> LoginAsync(string Account, string Password)
{
try
{
var repository = unitOfWork.GetRepository<User>();
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);
}
}
/// <summary>
/// 注册
/// </summary>
/// <param name="userDto"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public async Task<ApiResponse> RegisterAsync(UserDto userDto)
{
try
{
var user = mapper.Map<User>(userDto);
var repository = unitOfWork.GetRepository<User>();
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
/// <summary>
/// 登录
/// </summary>
public class LoginController : BaseApiController
{
private readonly ILoginService loginService;
public LoginController(ILoginService loginService)
{
this.loginService = loginService;
}
[HttpGet]
public async Task<ApiResponse> Login(string Account, string PassWord)
{
return await loginService.LoginAsync(Account, PassWord);
}
[HttpPost]
public async Task<ApiResponse> Register([FromBody] UserDto userDto)
{
return await loginService.RegisterAsync(userDto);
}
}
5、添加AutoMapper映射关系
CreateMap<User, UserDto>().ReverseMap();
6、在program.cs里面添加服务
builder.Services.AddTransient<ILoginService, LoginService>();
7、F5运行项目文章来源:https://www.toymoban.com/news/detail-693098.html
文章来源地址https://www.toymoban.com/news/detail-693098.html
到了这里,关于WPF实战项目十四(API篇):登录注册接口的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!