refactor: 重构用户与勋章体系,统一状态管理与数据结构
1. 新增通用默认状态枚举 DefaultStatusEnum,替换原有分散的状态枚举 2. 重构用户体系:拆分 WxUser 独立表存储微信身份,Users 表改为角色子用户表并关联 WxUser 3. 重构勋章模块:新增系统/期刊勋章类型,调整 JournalId 为可空,新增勋章状态字段 4. 重构微信认证流程:基于 WxUser 生成 Token,支持多子用户管理 5. 清理冗余枚举文件,重构多处业务逻辑适配新的数据结构 6. 修复用户手机号关联逻辑,迁移手机号字段至 WxUser 表
This commit is contained in:
@ -17,7 +17,7 @@ public class BagController(IWxMallService mallService, ILogger<BagController> lo
|
||||
[HttpGet("items")]
|
||||
public async Task<BaseResponse<List<UserBagOutput>>> GetBagItems([FromQuery] string? itemType = null)
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
var userId = GetCurrentWxUserId();
|
||||
if (userId == null) return Fail("未获取到用户信息") as dynamic;
|
||||
|
||||
var items = await mallService.GetBagItemsAsync(userId.Value, itemType);
|
||||
@ -30,7 +30,7 @@ public class BagController(IWxMallService mallService, ILogger<BagController> lo
|
||||
[HttpPost("useItem")]
|
||||
public async Task<BaseResponse<UseItemOutput>> UseItem([FromBody] UseItemInput input)
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
var userId = GetCurrentWxUserId();
|
||||
if (userId == null) return Fail("未获取到用户信息") as dynamic;
|
||||
|
||||
var result = await mallService.UseItemAsync(userId.Value, input);
|
||||
@ -43,7 +43,7 @@ public class BagController(IWxMallService mallService, ILogger<BagController> lo
|
||||
[HttpPost("equipSkin")]
|
||||
public async Task<BaseResponse<object>> EquipSkin([FromBody] EquipSkinInput input)
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
var userId = GetCurrentWxUserId();
|
||||
if (userId == null) return Fail("未获取到用户信息") as dynamic;
|
||||
|
||||
await mallService.EquipSkinAsync(userId.Value, input);
|
||||
|
||||
@ -29,7 +29,7 @@ public class CheckInController : WeChatBaseController
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
var userId = GetCurrentWxUserId();
|
||||
if (userId == null)
|
||||
{
|
||||
return BaseResponse<CheckInOutput>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
@ -59,7 +59,7 @@ public class CheckInController : WeChatBaseController
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
var userId = GetCurrentWxUserId();
|
||||
if (userId == null)
|
||||
{
|
||||
return BaseResponse<CheckInInfoOutput>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
|
||||
@ -19,7 +19,7 @@ public class CommunityController(IWeChatCommunityService communityService, ILogg
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
var userId = GetCurrentWxUserId();
|
||||
if (userId == null)
|
||||
{
|
||||
return BaseResponse<WxFeedOutput>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
@ -48,7 +48,7 @@ public class CommunityController(IWeChatCommunityService communityService, ILogg
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
var userId = GetCurrentWxUserId();
|
||||
if (userId == null)
|
||||
{
|
||||
return BaseResponse<WxFeedOutput>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
@ -77,7 +77,7 @@ public class CommunityController(IWeChatCommunityService communityService, ILogg
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
var userId = GetCurrentWxUserId();
|
||||
if (userId == null)
|
||||
{
|
||||
return BaseResponse<WxLikeOutput>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
@ -106,7 +106,7 @@ public class CommunityController(IWeChatCommunityService communityService, ILogg
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
var userId = GetCurrentWxUserId();
|
||||
if (userId == null)
|
||||
{
|
||||
return BaseResponse<WxLikeOutput>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
|
||||
@ -29,7 +29,7 @@ public class JournalController : WeChatBaseController
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
var userId = GetCurrentWxUserId();
|
||||
if (userId == null)
|
||||
{
|
||||
return BaseResponse<BindJournalOutput>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
@ -60,7 +60,7 @@ public class JournalController : WeChatBaseController
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
var userId = GetCurrentWxUserId();
|
||||
if (userId == null)
|
||||
{
|
||||
return BaseResponse<PageListModel<BindJournalOutput>>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
|
||||
@ -17,7 +17,7 @@ public class MallController(IWxMallService mallService, ILogger<MallController>
|
||||
[HttpGet("products")]
|
||||
public async Task<BaseResponse<List<WxProductOutput>>> GetProducts([FromQuery] string? type = null)
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
var userId = GetCurrentWxUserId();
|
||||
if (userId == null) return Fail("未获取到用户信息") as dynamic;
|
||||
|
||||
var products = await mallService.GetProductsAsync(userId.Value, type);
|
||||
@ -31,7 +31,7 @@ public class MallController(IWxMallService mallService, ILogger<MallController>
|
||||
[HttpGet("product/{id}")]
|
||||
public async Task<BaseResponse<WxProductOutput>> GetProductDetail(long id)
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
var userId = GetCurrentWxUserId();
|
||||
if (userId == null) return Fail("未获取到用户信息") as dynamic;
|
||||
|
||||
var product = await mallService.GetProductDetailAsync(userId.Value, id);
|
||||
@ -47,7 +47,7 @@ public class MallController(IWxMallService mallService, ILogger<MallController>
|
||||
[HttpPost("exchange")]
|
||||
public async Task<BaseResponse<ExchangeOutput>> Exchange([FromBody] ExchangeInput input)
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
var userId = GetCurrentWxUserId();
|
||||
if (userId == null) return Fail("未获取到用户信息") as dynamic;
|
||||
|
||||
var result = await mallService.ExchangeAsync(userId.Value, input);
|
||||
@ -61,7 +61,7 @@ public class MallController(IWxMallService mallService, ILogger<MallController>
|
||||
[HttpGet("exchangeRecords")]
|
||||
public async Task<BaseResponse<List<ExchangeRecordOutput>>> GetExchangeRecords([FromQuery] int limit = 20)
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
var userId = GetCurrentWxUserId();
|
||||
if (userId == null) return Fail("未获取到用户信息") as dynamic;
|
||||
|
||||
var records = await mallService.GetExchangeRecordsAsync(userId.Value, limit);
|
||||
|
||||
@ -18,7 +18,7 @@ public class MedalController(IMedalService medalService, ILogger<MedalController
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
var userId = GetCurrentWxUserId();
|
||||
if (userId == null)
|
||||
{
|
||||
return BaseResponse<List<WxMedalListOutput>>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
@ -47,7 +47,7 @@ public class MedalController(IMedalService medalService, ILogger<MedalController
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
var userId = GetCurrentWxUserId();
|
||||
if (userId == null)
|
||||
{
|
||||
return BaseResponse<List<WxUserMedalOutput>>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
@ -76,7 +76,7 @@ public class MedalController(IMedalService medalService, ILogger<MedalController
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
var userId = GetCurrentWxUserId();
|
||||
if (userId == null)
|
||||
{
|
||||
return BaseResponse<object>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
|
||||
@ -28,7 +28,7 @@ public class PetController : WeChatBaseController
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
var userId = GetCurrentWxUserId();
|
||||
if (userId == null)
|
||||
{
|
||||
return BaseResponse<PetOutput>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
@ -62,7 +62,7 @@ public class PetController : WeChatBaseController
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
var userId = GetCurrentWxUserId();
|
||||
if (userId == null)
|
||||
{
|
||||
return BaseResponse<FeedPetOutput>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
@ -91,7 +91,7 @@ public class PetController : WeChatBaseController
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
var userId = GetCurrentWxUserId();
|
||||
if (userId == null)
|
||||
{
|
||||
return BaseResponse<PageListModel<FeedingRecordOutput>>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
|
||||
@ -23,7 +23,7 @@ public class WeChatAuthController : WeChatBaseController
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 微信小程序登录(首次创建用户,非首次直接登录)
|
||||
/// 微信小程序登录(首次仅创建 WxUser,不自动创建 User)
|
||||
/// </summary>
|
||||
/// <param name="input">登录输入(含微信 code 和可选的手机号 code)</param>
|
||||
/// <returns>登录结果(含 Token 和用户列表)</returns>
|
||||
@ -75,22 +75,18 @@ public class WeChatAuthController : WeChatBaseController
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 切换用户(同一 OpenId 下切换身份)
|
||||
/// 切换用户(同一 WxUser 下切换 User 身份)
|
||||
/// </summary>
|
||||
/// <param name="input">切换用户输入(含目标用户ID)</param>
|
||||
/// <returns>切换结果(含新 Token 和目标用户详情)</returns>
|
||||
[HttpPost("switchUser")]
|
||||
public async Task<BaseResponse<WeChatSwitchUserOutput>> SwitchUserAsync([FromBody] WeChatSwitchUserInput input)
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId == null)
|
||||
{
|
||||
var wxUserId = GetCurrentWxUserId();
|
||||
if (wxUserId == null)
|
||||
return BaseResponse<WeChatSwitchUserOutput>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
}
|
||||
|
||||
var result = await _weChatAuthService.SwitchUserAsync(userId.Value, input);
|
||||
var result = await _weChatAuthService.SwitchUserAsync(wxUserId.Value, input);
|
||||
return Success(result);
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
@ -104,4 +100,58 @@ public class WeChatAuthController : WeChatBaseController
|
||||
return BaseResponse<WeChatSwitchUserOutput>.Fail("切换用户失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前微信用户下的所有用户列表
|
||||
/// </summary>
|
||||
[HttpGet("users")]
|
||||
public async Task<BaseResponse<List<WxUserOutput>>> GetUsersAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var wxUserId = GetCurrentWxUserId();
|
||||
if (wxUserId == null)
|
||||
return BaseResponse<List<WxUserOutput>>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
|
||||
var result = await _weChatAuthService.GetUsersAsync(wxUserId.Value);
|
||||
return Success(result);
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "获取用户列表业务异常: {Message}", ex.Message);
|
||||
return BaseResponse<List<WxUserOutput>>.Fail(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "获取用户列表系统异常");
|
||||
return BaseResponse<List<WxUserOutput>>.Fail("获取用户列表失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在当前微信用户下新增用户(子用户/角色)
|
||||
/// </summary>
|
||||
[HttpPost("createUser")]
|
||||
public async Task<BaseResponse<WxUserOutput>> CreateUserAsync([FromBody] CreateChildUserInput input)
|
||||
{
|
||||
try
|
||||
{
|
||||
var wxUserId = GetCurrentWxUserId();
|
||||
if (wxUserId == null)
|
||||
return BaseResponse<WxUserOutput>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
|
||||
var result = await _weChatAuthService.CreateUserAsync(wxUserId.Value, input);
|
||||
return Success(result);
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "新增用户业务异常: {Message}", ex.Message);
|
||||
return BaseResponse<WxUserOutput>.Fail(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "新增用户系统异常");
|
||||
return BaseResponse<WxUserOutput>.Fail("新增用户失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,10 +16,10 @@ namespace QYZH.InteractiveMagazine.WebApi.Controllers.WeChat;
|
||||
public abstract class WeChatBaseController : ControllerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取当前用户ID
|
||||
/// 获取当前微信用户ID(WxUser.Id,来自 JWT)
|
||||
/// </summary>
|
||||
/// <returns>用户ID</returns>
|
||||
protected long? GetCurrentUserId()
|
||||
/// <returns>WxUser ID</returns>
|
||||
protected long? GetCurrentWxUserId()
|
||||
{
|
||||
var userIdClaim = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier);
|
||||
if (userIdClaim != null && long.TryParse(userIdClaim.Value, out var userId))
|
||||
|
||||
Reference in New Issue
Block a user