1. 新增通用默认状态枚举 DefaultStatusEnum,替换原有分散的状态枚举 2. 重构用户体系:拆分 WxUser 独立表存储微信身份,Users 表改为角色子用户表并关联 WxUser 3. 重构勋章模块:新增系统/期刊勋章类型,调整 JournalId 为可空,新增勋章状态字段 4. 重构微信认证流程:基于 WxUser 生成 Token,支持多子用户管理 5. 清理冗余枚举文件,重构多处业务逻辑适配新的数据结构 6. 修复用户手机号关联逻辑,迁移手机号字段至 WxUser 表
158 lines
5.7 KiB
C#
158 lines
5.7 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using QYZH.InteractiveMagazine.IService;
|
||
|
||
using QYZH.InteractiveMagazine.Models.Common;
|
||
using QYZH.InteractiveMagazine.Models.Dto;
|
||
using QYZH.InteractiveMagazine.Models.WeChat;
|
||
|
||
namespace QYZH.InteractiveMagazine.WebApi.Controllers.WeChat;
|
||
|
||
/// <summary>
|
||
/// 微信小程序认证控制器
|
||
/// </summary>
|
||
public class WeChatAuthController : WeChatBaseController
|
||
{
|
||
private readonly IWeChatAuthService _weChatAuthService;
|
||
private readonly ILogger<WeChatAuthController> _logger;
|
||
|
||
public WeChatAuthController(IWeChatAuthService weChatAuthService, ILogger<WeChatAuthController> logger)
|
||
{
|
||
_weChatAuthService = weChatAuthService;
|
||
_logger = logger;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 微信小程序登录(首次仅创建 WxUser,不自动创建 User)
|
||
/// </summary>
|
||
/// <param name="input">登录输入(含微信 code 和可选的手机号 code)</param>
|
||
/// <returns>登录结果(含 Token 和用户列表)</returns>
|
||
[AllowAnonymous]
|
||
[HttpPost("login")]
|
||
public async Task<BaseResponse<WeChatLoginOutput>> LoginAsync([FromBody] WeChatLoginInput input)
|
||
{
|
||
try
|
||
{
|
||
var result = await _weChatAuthService.LoginAsync(input);
|
||
return Success(result);
|
||
}
|
||
catch (BusinessException ex)
|
||
{
|
||
_logger.LogWarning(ex, "微信登录业务异常: {Message}", ex.Message);
|
||
return BaseResponse<WeChatLoginOutput>.Fail(ex.Message);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "微信登录系统异常");
|
||
return BaseResponse<WeChatLoginOutput>.Fail("微信登录失败,请稍后重试");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 微信小程序快捷登录(通过 OpenId 直接登录,用户需已存在)
|
||
/// </summary>
|
||
/// <param name="input">快捷登录输入(含 OpenId)</param>
|
||
/// <returns>登录结果(含 Token 和用户列表)</returns>
|
||
[AllowAnonymous]
|
||
[HttpPost("quickLogin")]
|
||
public async Task<BaseResponse<WeChatLoginOutput>> QuickLoginAsync([FromBody] WeChatQuickLoginInput input)
|
||
{
|
||
try
|
||
{
|
||
var result = await _weChatAuthService.QuickLoginAsync(input);
|
||
return Success(result);
|
||
}
|
||
catch (BusinessException ex)
|
||
{
|
||
_logger.LogWarning(ex, "微信快捷登录业务异常: {Message}", ex.Message);
|
||
return BaseResponse<WeChatLoginOutput>.Fail(ex.Message);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "微信快捷登录系统异常");
|
||
return BaseResponse<WeChatLoginOutput>.Fail("快捷登录失败,请稍后重试");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 切换用户(同一 WxUser 下切换 User 身份)
|
||
/// </summary>
|
||
[HttpPost("switchUser")]
|
||
public async Task<BaseResponse<WeChatSwitchUserOutput>> SwitchUserAsync([FromBody] WeChatSwitchUserInput input)
|
||
{
|
||
try
|
||
{
|
||
var wxUserId = GetCurrentWxUserId();
|
||
if (wxUserId == null)
|
||
return BaseResponse<WeChatSwitchUserOutput>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||
|
||
var result = await _weChatAuthService.SwitchUserAsync(wxUserId.Value, input);
|
||
return Success(result);
|
||
}
|
||
catch (BusinessException ex)
|
||
{
|
||
_logger.LogWarning(ex, "切换用户业务异常: {Message}", ex.Message);
|
||
return BaseResponse<WeChatSwitchUserOutput>.Fail(ex.Message);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "切换用户系统异常");
|
||
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("新增用户失败,请稍后重试");
|
||
}
|
||
}
|
||
}
|