/// <summary>
/// 用户登录注册控制器
/// </summary>
[ApiController]
[Route("api/[controller]/[action]")]
public class LoginController: ControllerBase
{
}
所有的Dto类都要实现通知绑定的原因是:客户端项目是WPF,WPF在进行属性值绑定要通知前端更新的时候,需要用到这个功能。即属性通知绑定。这一章节有介绍!
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(); }
}
}
public class AutoMapperProFile:MapperConfigurationExpression
{
public AutoMapperProFile()
{
/// 实体类和数据传输类进行映射
CreateMap<ToDo, ToDoDto>().ReverseMap();
CreateMap<Memo, MemoDto>().ReverseMap();
CreateMap<User, UserDto>().ReverseMap();
}
}
接口定义和接口实现命名规范,这章节也有介绍。后续所有章节不再带入了。
public interface ILoginService
{
/// <summary>
/// 用户登录
/// </summary>
/// <param name="account">账户名</param>
/// <param name="password">密码</param>
/// <returns></returns>
Task<ApiResponse> LoginAsync(string account,string password);
/// <summary>
/// 用户注册
/// </summary>
/// <param name="user">用户数据实体</param>
/// <returns></returns>
Task<ApiResponse> ResgiterAsync(UserDto user);
}
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<ApiResponse> LoginAsync(string account, string password)
{
try
{
var model= await work.GetRepository<User>().GetFirstOrDefaultAsync(predicate:
i=>(i.Account.Equals(account)) && (i.PassWord.Equals(password)));
if (model == null) return new ApiResponse("账号或密码错误,请重试!");
return new ApiResponse(true,model);//成功则返回该账户的所有数据实体
}
catch (Exception ex)
{
return new ApiResponse(false,$"登录失败,{ex.Message}");
}
}
public async Task<ApiResponse> ResgiterAsync(UserDto user)
{
try
{
var model=mapper.Map<User>(user);//把数据传输类转换成实体类
var repository= work.GetRepository<User>();
var userModel=await repository.GetFirstOrDefaultAsync(predicate:x=>x.Account.Equals(model.Account)); //根据账户查,该账户是否已存在
if(userModel != null) return new ApiResponse($"当前账户{model.Account}已存在,请重新注册!");
model.CreateDate = DateTime.Now;
await repository.InsertAsync(model);
if(await work.SaveChangesAsync() > 0)
{
return new ApiResponse(true,model);//成功则返回新用户所有数据实体
}
return new ApiResponse($"账户{model.Account}注册失败!");
}
catch (Exception ex)
{
return new ApiResponse($"注册账户失败!{ex.Message}");
}
}
}
predicate 是一个
参数,用于指定查询的条件。它是一个匿名函数或Lambda表达式,用于筛选集合中的元素,返回一个布尔值来指示元素是否满足条件。- 新用户进行注册的时候,可以把密码进行MD5加密或其他方式加密后再存储到数据库,这里就不再进行实现了。
/// <summary>
/// 用户登录注册控制器
/// </summary>
[ApiController]
[Route("api/[controller]/[action]")]
public class LoginController: ControllerBase
{
private readonly ILoginService service;
public LoginController(ILoginService service)
{
this.service = service;
}
[HttpGet]
public async Task<ApiResponse> Login(string account, string password) => await service.LoginAsync(account, password);
[HttpPost]
public async Task<ApiResponse> Resgiter([FromBody] UserDto user) => await service.ResgiterAsync(user);
}
builder.Services.AddTransient<ILoginService, LoginService>();