using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using QYZH.InteractiveMagazine.IService;
using QYZH.InteractiveMagazine.IService.Dto;
using QYZH.InteractiveMagazine.Models.Common;
using QYZH.InteractiveMagazine.Models.Settings;
using QYZH.InteractiveMagazine.Infrastructure.Auth;
namespace QYZH.InteractiveMagazine.Service;
///
/// 认证服务实现
///
public class AuthService : IAuthService
{
private readonly IConfiguration _configuration;
private readonly ILogger _logger;
///
/// 演示用管理员账号
///
private const string DemoAccount = "admin";
///
/// 演示用管理员密码
///
private const string DemoPassword = "123456";
///
/// 构造函数
///
/// 配置
/// 日志记录器
public AuthService(IConfiguration configuration, ILogger logger)
{
_configuration = configuration;
_logger = logger;
}
///
/// 用户登录
///
/// 账号
/// 密码
/// 登录结果
public async Task LoginAsync(string account, string password)
{
_logger.LogInformation("用户登录尝试,账号: {Account}", account);
if (string.IsNullOrWhiteSpace(account))
{
throw new BusinessException("账号不能为空", 400);
}
if (string.IsNullOrWhiteSpace(password))
{
throw new BusinessException("密码不能为空", 400);
}
// 演示版本:硬编码验证账号密码
if (account != DemoAccount || password != DemoPassword)
{
_logger.LogWarning("用户登录失败,账号: {Account}", account);
throw new BusinessException("账号或密码错误", 401);
}
// 获取JWT配置
var jwtSettings = GetJwtSettings();
// 生成令牌
var token = JwtHelper.GenerateToken(1, "管理员", jwtSettings);
var refreshToken = JwtHelper.GenerateToken(1, "管理员", jwtSettings);
_logger.LogInformation("用户登录成功,账号: {Account}", account);
return new LoginOutput
{
Token = token,
RefreshToken = refreshToken,
UserId = 1,
UserName = "管理员"
};
}
///
/// 用户注册
///
/// 注册输入参数
/// 是否成功
public async Task RegisterAsync(RegisterInput input)
{
_logger.LogInformation("用户注册尝试,账号: {Account}", input?.Account);
if (input == null)
{
throw new BusinessException("注册参数不能为空", 400);
}
if (string.IsNullOrWhiteSpace(input.Account))
{
throw new BusinessException("账号不能为空", 400);
}
if (string.IsNullOrWhiteSpace(input.Password))
{
throw new BusinessException("密码不能为空", 400);
}
if (string.IsNullOrWhiteSpace(input.UserName))
{
throw new BusinessException("用户名不能为空", 400);
}
// 演示版本:模拟注册成功
_logger.LogInformation("用户注册成功,账号: {Account}, 用户名: {UserName}", input.Account, input.UserName);
return await Task.FromResult(true);
}
///
/// 刷新令牌
///
/// 刷新令牌
/// 新的登录结果
public async Task RefreshTokenAsync(string refreshToken)
{
_logger.LogInformation("令牌刷新尝试");
if (string.IsNullOrWhiteSpace(refreshToken))
{
throw new BusinessException("刷新令牌不能为空", 400);
}
var jwtSettings = GetJwtSettings();
// 生成新的令牌
var newToken = JwtHelper.GenerateToken(1, "管理员", jwtSettings);
var newRefreshToken = JwtHelper.GenerateToken(1, "管理员", jwtSettings);
_logger.LogInformation("令牌刷新成功");
return new LoginOutput
{
Token = newToken,
RefreshToken = newRefreshToken,
UserId = 1,
UserName = "管理员"
};
}
///
/// 获取JWT配置
///
/// JWT配置对象
private JwtSettings GetJwtSettings()
{
var jwtSettings = _configuration.GetSection("JwtSettings").Get()
?? new JwtSettings
{
Issuer = "QYZH.InteractiveMagazine",
Audience = "QYZH.InteractiveMagazine.Client",
SecretKey = "QYZH_InteractiveMagazine_SecretKey_2024",
ExpiryMinutes = 120
};
if (string.IsNullOrWhiteSpace(jwtSettings.SecretKey))
{
throw new BusinessException("JWT配置不完整", 500);
}
return jwtSettings;
}
}