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;
///
/// 微信小程序认证控制器
///
public class WeChatAuthController : WeChatBaseController
{
private readonly IWeChatAuthService _weChatAuthService;
private readonly ILogger _logger;
public WeChatAuthController(IWeChatAuthService weChatAuthService, ILogger logger)
{
_weChatAuthService = weChatAuthService;
_logger = logger;
}
///
/// 微信小程序登录(首次仅创建 WxUser,不自动创建 User)
///
/// 登录输入(含微信 code 和可选的手机号 code)
/// 登录结果(含 Token 和用户列表)
[AllowAnonymous]
[HttpPost("login")]
public async Task> 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.Fail(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "微信登录系统异常");
return BaseResponse.Fail("微信登录失败,请稍后重试");
}
}
///
/// 微信小程序快捷登录(通过 OpenId 直接登录,用户需已存在)
///
/// 快捷登录输入(含 OpenId)
/// 登录结果(含 Token 和用户列表)
[AllowAnonymous]
[HttpPost("quickLogin")]
public async Task> 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.Fail(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "微信快捷登录系统异常");
return BaseResponse.Fail("快捷登录失败,请稍后重试");
}
}
///
/// 切换用户(同一 WxUser 下切换 User 身份)
///
[HttpPost("switchUser")]
public async Task> SwitchUserAsync([FromBody] WeChatSwitchUserInput input)
{
try
{
var wxUserId = GetCurrentWxUserId();
if (wxUserId == null)
return BaseResponse.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.Fail(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "切换用户系统异常");
return BaseResponse.Fail("切换用户失败,请稍后重试");
}
}
///
/// 获取当前微信用户下的所有用户列表
///
[HttpGet("users")]
public async Task>> GetUsersAsync()
{
try
{
var wxUserId = GetCurrentWxUserId();
if (wxUserId == null)
return BaseResponse>.Fail(ResultCode.DENY, "未获取到用户信息");
var result = await _weChatAuthService.GetUsersAsync(wxUserId.Value);
return Success(result);
}
catch (BusinessException ex)
{
_logger.LogWarning(ex, "获取用户列表业务异常: {Message}", ex.Message);
return BaseResponse>.Fail(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "获取用户列表系统异常");
return BaseResponse>.Fail("获取用户列表失败,请稍后重试");
}
}
///
/// 在当前微信用户下新增用户(子用户/角色)
///
[HttpPost("createUser")]
public async Task> CreateUserAsync([FromBody] CreateChildUserInput input)
{
try
{
var wxUserId = GetCurrentWxUserId();
if (wxUserId == null)
return BaseResponse.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.Fail(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "新增用户系统异常");
return BaseResponse.Fail("新增用户失败,请稍后重试");
}
}
}