using FreeSql; using MAX.Core.Cryptography; using MAX.Core.DependencyInjection; using StoreHub.Framework.Models; using StoreHub.Framework.Repositorys; using StoreHub.Services.Dtos; using StoreHub.Services.Mappers; namespace StoreHub.Services; /// /// 用户业务服务:登录校验、用户查询等用例。 /// public interface IUserService { /// /// 校验用户名/密码,成功返回用户 DTO,失败返回 null。 /// Task LoginAsync(string userName, string password); /// /// 根据用户ID获取用户 DTO,不存在返回 null。 /// Task GetByIdAsync(int id); } /// /// 用户业务服务实现(,由 AddStoreHub 自动注册)。 /// public class UserService : IUserService, IScopedDependency { private readonly IUserRepository _userRepository; public UserService(IUserRepository userRepository) { _userRepository = userRepository; } /// public async Task LoginAsync(string userName, string password) { if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(password)) { return null; } var sql = _userRepository.Where(u => u.UserName == userName).ToSql(); User? user = await _userRepository.Where(u => u.UserName == userName).FirstAsync(); string pwd = MD5Helper.Md5_32(password); if (user is null || !string.Equals(user.Password, pwd, StringComparison.OrdinalIgnoreCase)) { return null; } return UserMapper.ToDto(user); } /// public async Task GetByIdAsync(int id) { User? user = await _userRepository.Where(u => u.Id == id).FirstAsync(); return user is null ? null : UserMapper.ToDto(user); } }