45 lines
1.9 KiB
C#
45 lines
1.9 KiB
C#
using MAX.Framework.Validation;
|
|
using MAX.Core.DependencyInjection;
|
|
using MAX.Framework.Commons;
|
|
using Store.Users.Contracts;
|
|
using Store.Users.Contracts.Dtos;
|
|
using Store.Users.Entities;
|
|
using Store.Users.Mappers;
|
|
using Store.Users.Repositories;
|
|
namespace Store.Users.Services;
|
|
|
|
internal sealed class UserQueryService(IUserRepository repository) : IUserQueryService, ITransientDependency
|
|
{
|
|
public async Task<UserInfo> GetUser(string userName)
|
|
{
|
|
RequestValidator.Validate(new UserNameRequest { UserName = userName?.Trim()! });
|
|
var user = await repository.GetUser(userName.Trim()) ?? throw new AppException("用户不存在");
|
|
return new UserMapper().ToInfo(user);
|
|
}
|
|
/// <summary>
|
|
/// 获取主账号
|
|
/// </summary>
|
|
/// <param name="userName"></param>
|
|
/// <returns></returns>
|
|
public async Task<UserInfo> GetMainUser(string userName)
|
|
{
|
|
RequestValidator.Validate(new UserNameRequest { UserName = userName?.Trim()! });
|
|
var user = await repository.GetMainUser(userName.Trim()) ?? throw new AppException("用户不存在");
|
|
return new UserMapper().ToInfo(user);
|
|
}
|
|
}
|
|
|
|
internal sealed class UserAuthenticationService(IFreeSql orm, IUserQueryService query)
|
|
: IUserAuthenticationService, ITransientDependency
|
|
{
|
|
public async Task<UserInfo?> Authenticate(string userName, string password, CancellationToken cancellationToken = default)
|
|
{
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
if (!RequestValidator.TryValidate(new UserLoginRequest { UserName = userName?.Trim()!, Password = password }, out _))
|
|
return null;
|
|
var user = await orm.Select<UserEntity>().Where(u => u.UserName == userName.Trim()).FirstAsync(cancellationToken);
|
|
if (user == null || !UserPassword.Verify(user, password)) return null;
|
|
return await query.GetUser(user.UserName);
|
|
}
|
|
}
|