using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using QYZH.InteractiveMagazine.IService; using QYZH.InteractiveMagazine.IService.Dto; using QYZH.InteractiveMagazine.Models.Common; using QYZH.InteractiveMagazine.Models.Dto; 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; } /// /// 微信小程序一键登录 /// /// 登录输入(含微信 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 下切换身份) /// /// 切换用户输入(含目标用户ID) /// 切换结果(含新 Token 和目标用户详情) [HttpPost("switchUser")] public async Task> SwitchUserAsync([FromBody] WeChatSwitchUserInput input) { try { var userId = GetCurrentUserId(); if (userId == null) { return BaseResponse.Fail(ResultCode.DENY, "未获取到用户信息"); } var result = await _weChatAuthService.SwitchUserAsync(userId.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("切换用户失败,请稍后重试"); } } }