feat: 完成多模块功能迭代与优化
1. 新增宠物皮肤初始枚举值、下拉列表接口及相关DTO 2. 完善商品实体与DTO,新增虚拟商品标记和类型ID字段 3. 重构用户认证体系,支持多用户切换并重新生成JWT令牌 4. 新增签到配置管理全套功能,包括增删改查和状态管理 5. 优化模型验证过滤器和基础响应类的命名规范 6. 新增补签功能,完善签到服务逻辑 7. 拆分用户详情DTO,新增各子数据分页查询接口 8. 重构微信控制器的用户ID获取逻辑,统一使用激活用户ID 9. 修复背包服务中补签卡的扣减逻辑 10. 新增家长姓名修改接口和相关服务实现
This commit is contained in:
@ -12,7 +12,7 @@ namespace QYZH.InteractiveMagazine.Infrastructure.Auth;
|
||||
public static class JwtHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 生成JWT令牌
|
||||
/// 生成JWT令牌(管理端 / 单用户场景)
|
||||
/// </summary>
|
||||
/// <param name="userId">用户ID</param>
|
||||
/// <param name="userName">用户名</param>
|
||||
@ -29,6 +29,39 @@ public static class JwtHelper
|
||||
new Claim(ClaimTypes.Name, userName)
|
||||
};
|
||||
|
||||
return BuildToken(claims, settings);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成JWT令牌(小程序场景,同时携带 WxUserId 和 UserId)
|
||||
/// </summary>
|
||||
/// <param name="wxUserId">微信用户ID(WxUser.Id)</param>
|
||||
/// <param name="userId">当前激活用户ID(Users.Id,无用户时为 0)</param>
|
||||
/// <param name="userName">用户名</param>
|
||||
/// <param name="settings">JWT配置</param>
|
||||
/// <returns>JWT令牌字符串</returns>
|
||||
public static string GenerateToken(long wxUserId, long userId, string userName, JwtSettings settings)
|
||||
{
|
||||
var claims = new[]
|
||||
{
|
||||
new Claim(JwtRegisteredClaimNames.Sub, userId.ToString()),
|
||||
new Claim(JwtRegisteredClaimNames.Name, userName),
|
||||
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
|
||||
new Claim(ClaimTypes.NameIdentifier, userId.ToString()),
|
||||
new Claim(ClaimTypes.Name, userName),
|
||||
new Claim(WxUserIdClaimType, wxUserId.ToString())
|
||||
};
|
||||
|
||||
return BuildToken(claims, settings);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 自定义 Claim 类型:WxUserId
|
||||
/// </summary>
|
||||
public const string WxUserIdClaimType = "WxUserId";
|
||||
|
||||
private static string BuildToken(Claim[] claims, JwtSettings settings)
|
||||
{
|
||||
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(settings.SecretKey!));
|
||||
var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
||||
|
||||
@ -77,6 +110,25 @@ public static class JwtHelper
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从Token中获取微信用户ID(WxUserId)
|
||||
/// </summary>
|
||||
/// <param name="token">JWT令牌</param>
|
||||
/// <returns>WxUserId</returns>
|
||||
public static long? GetWxUserIdFromToken(string token)
|
||||
{
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
if (tokenHandler.ReadToken(token) is JwtSecurityToken jwtToken)
|
||||
{
|
||||
var wxUserIdClaim = jwtToken.Claims.FirstOrDefault(c => c.Type == WxUserIdClaimType);
|
||||
if (long.TryParse(wxUserIdClaim?.Value, out long wxUserId))
|
||||
{
|
||||
return wxUserId;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从Token中获取用户名
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user