refactor: 重构项目基础架构与实体体系
- 替换原有自定义生命周期接口为通用基础服务体系 - 将所有实体基类替换为带雪花ID的SqlSugarBaseEntity - 迁移DTO到Models项目并统一管理 - 移除冗余的Repository层实现,改用通用基础服务 - 添加Yitter.IdGenerator雪花ID生成支持 - 重构SqlSugar数据库上下文与依赖注入配置 - 新增微信用户、用户勋章、背包等实体与配套服务 - 整理分页查询与结果封装类 - 优化WebApi控制器结构
This commit is contained in:
@ -6,31 +6,23 @@ using QYZH.InteractiveMagazine.Infrastructure.Cache;
|
||||
using QYZH.InteractiveMagazine.IService;
|
||||
using QYZH.InteractiveMagazine.IService.Dto;
|
||||
using QYZH.InteractiveMagazine.Models.Common;
|
||||
using QYZH.InteractiveMagazine.Models.Dto;
|
||||
using QYZH.InteractiveMagazine.Models.Entity;
|
||||
using QYZH.InteractiveMagazine.Models.Settings;
|
||||
using QYZH.InteractiveMagazine.Repository;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.Service;
|
||||
|
||||
public class AdminAuthService : IAdminAuthService
|
||||
public class AdminAuthService(BaseRepository<AdminUser> adminUserRepository, IConfiguration configuration, ILogger<AdminAuthService> logger) : BaseRepository<AdminUser>, IAdminAuthService
|
||||
{
|
||||
private readonly IAdminUserRepository _adminUserRepository;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ILogger<AdminAuthService> _logger;
|
||||
|
||||
private const string TokenKeyPrefix = "InteractiveMagazine:AdminAuth:Token";
|
||||
private const string UserInfoKeyPrefix = "InteractiveMagazine:AdminAuth:UserInfo";
|
||||
|
||||
public AdminAuthService(IAdminUserRepository adminUserRepository, IConfiguration configuration, ILogger<AdminAuthService> logger)
|
||||
{
|
||||
_adminUserRepository = adminUserRepository;
|
||||
_configuration = configuration;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<AdminLoginOutput> LoginAsync(AdminLoginInput input)
|
||||
{
|
||||
_logger.LogInformation("管理员登录尝试,用户名: {UserName}", input.UserName);
|
||||
logger.LogInformation("管理员登录尝试,用户名: {UserName}", input.UserName);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(input.UserName))
|
||||
{
|
||||
@ -42,22 +34,22 @@ public class AdminAuthService : IAdminAuthService
|
||||
throw new BusinessException("密码不能为空", 400);
|
||||
}
|
||||
|
||||
var adminUser = await _adminUserRepository.GetByUserNameAsync(input.UserName);
|
||||
var adminUser = await adminUserRepository.GetFirstAsync(a => a.UserName == input.UserName);
|
||||
if (adminUser == null)
|
||||
{
|
||||
_logger.LogWarning("管理员登录失败,用户名不存在: {UserName}", input.UserName);
|
||||
logger.LogWarning("管理员登录失败,用户名不存在: {UserName}", input.UserName);
|
||||
throw new BusinessException("用户名或密码错误", 401);
|
||||
}
|
||||
|
||||
if (!BCrypt.Net.BCrypt.Verify(input.Password, adminUser.PasswordHash))
|
||||
{
|
||||
_logger.LogWarning("管理员登录失败,密码错误: {UserName}", input.UserName);
|
||||
logger.LogWarning("管理员登录失败,密码错误: {UserName}", input.UserName);
|
||||
throw new BusinessException("用户名或密码错误", 401);
|
||||
}
|
||||
|
||||
if (adminUser.Status != "Active")
|
||||
{
|
||||
_logger.LogWarning("管理员登录失败,账号已禁用: {UserName}", input.UserName);
|
||||
logger.LogWarning("管理员登录失败,账号已禁用: {UserName}", input.UserName);
|
||||
throw new BusinessException("账号已被禁用,请联系系统管理员", 403);
|
||||
}
|
||||
|
||||
@ -67,7 +59,7 @@ public class AdminAuthService : IAdminAuthService
|
||||
|
||||
await RedisHelper.StringSetAsync($"{TokenKeyPrefix}:{adminUser.Id}", token, TimeSpan.FromMinutes(jwtSettings.ExpiryMinutes));
|
||||
|
||||
_logger.LogInformation("管理员登录成功,用户名: {UserName}, ID: {UserId}", input.UserName, adminUser.Id);
|
||||
logger.LogInformation("管理员登录成功,用户名: {UserName}, ID: {UserId}", input.UserName, adminUser.Id);
|
||||
|
||||
return new AdminLoginOutput
|
||||
{
|
||||
@ -80,21 +72,21 @@ public class AdminAuthService : IAdminAuthService
|
||||
|
||||
public async Task LogoutAsync(long userId)
|
||||
{
|
||||
_logger.LogInformation("管理员登出,ID: {UserId}", userId);
|
||||
logger.LogInformation("管理员登出,ID: {UserId}", userId);
|
||||
|
||||
await RedisHelper.KeyDeleteAsync($"{TokenKeyPrefix}:{userId}");
|
||||
|
||||
_logger.LogInformation("管理员登出成功,ID: {UserId}", userId);
|
||||
logger.LogInformation("管理员登出成功,ID: {UserId}", userId);
|
||||
}
|
||||
|
||||
public async Task<AdminUserInfoOutput> GetAdminInfoAsync(long userId)
|
||||
{
|
||||
_logger.LogInformation("获取管理员信息,ID: {UserId}", userId);
|
||||
logger.LogInformation("获取管理员信息,ID: {UserId}", userId);
|
||||
|
||||
var adminUser = await _adminUserRepository.GetByIdAsync(userId);
|
||||
var adminUser = await adminUserRepository.GetByIdAsync(userId);
|
||||
if (adminUser == null)
|
||||
{
|
||||
_logger.LogWarning("未找到管理员,ID: {UserId}", userId);
|
||||
logger.LogWarning("未找到管理员,ID: {UserId}", userId);
|
||||
throw new BusinessException("用户不存在", 404);
|
||||
}
|
||||
|
||||
@ -109,7 +101,7 @@ public class AdminAuthService : IAdminAuthService
|
||||
|
||||
public async Task ChangePasswordAsync(long userId, string oldPassword, string newPassword)
|
||||
{
|
||||
_logger.LogInformation("管理员修改密码尝试,ID: {UserId}", userId);
|
||||
logger.LogInformation("管理员修改密码尝试,ID: {UserId}", userId);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(oldPassword))
|
||||
{
|
||||
@ -121,22 +113,22 @@ public class AdminAuthService : IAdminAuthService
|
||||
throw new BusinessException("新密码不能为空", 400);
|
||||
}
|
||||
|
||||
var adminUser = await _adminUserRepository.GetByIdAsync(userId);
|
||||
var adminUser = await adminUserRepository.GetByIdAsync(userId);
|
||||
if (adminUser == null)
|
||||
{
|
||||
_logger.LogWarning("未找到管理员,ID: {UserId}", userId);
|
||||
logger.LogWarning("未找到管理员,ID: {UserId}", userId);
|
||||
throw new BusinessException("用户不存在", 404);
|
||||
}
|
||||
|
||||
if (!BCrypt.Net.BCrypt.Verify(oldPassword, adminUser.PasswordHash))
|
||||
{
|
||||
_logger.LogWarning("管理员修改密码失败,原密码错误,ID: {UserId}", userId);
|
||||
logger.LogWarning("管理员修改密码失败,原密码错误,ID: {UserId}", userId);
|
||||
throw new BusinessException("原密码错误", 400);
|
||||
}
|
||||
|
||||
adminUser.PasswordHash = BCrypt.Net.BCrypt.HashPassword(newPassword);
|
||||
|
||||
var result = await _adminUserRepository.UpdateAsync(adminUser);
|
||||
var result = await adminUserRepository.UpdateAsync(adminUser);
|
||||
if (!result)
|
||||
{
|
||||
throw new BusinessException("修改密码失败", 500);
|
||||
@ -144,12 +136,12 @@ public class AdminAuthService : IAdminAuthService
|
||||
|
||||
await RedisHelper.KeyDeleteAsync($"{TokenKeyPrefix}:{userId}");
|
||||
|
||||
_logger.LogInformation("管理员修改密码成功,ID: {UserId}", userId);
|
||||
logger.LogInformation("管理员修改密码成功,ID: {UserId}", userId);
|
||||
}
|
||||
|
||||
private JwtSettings GetJwtSettings()
|
||||
{
|
||||
var jwtSettings = _configuration.GetSection("JwtSettings").Get<JwtSettings>()
|
||||
var jwtSettings = configuration.GetSection("JwtSettings").Get<JwtSettings>()
|
||||
?? new JwtSettings
|
||||
{
|
||||
Issuer = "QYZH.InteractiveMagazine",
|
||||
|
||||
Reference in New Issue
Block a user