创建注册登录接口
添加文件
创建文件
+ MyToDo.Api
./Controllers/LoginController.cs
./Service/ILoginService.cs
./Service/LoginService.cs
-
MyToDo.Share文章来源:https://www.toymoban.com/news/detail-614379.html
./Dtos/UserDto.cs文章来源地址https://www.toymoban.com/news/detail-614379.html
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>();
到了这里,关于WPF实战学习笔记13-创建注册登录接口的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!