Files
QYZH.InteractiveMagazine/QYZH.InteractiveMagazine.WebApi/Controllers/WeChat/WeChatAuthController.cs
glz 018d7e1733 feat(wechat-auth): 实现微信同一OpenId下多用户切换功能
1. 新增用户实体字段IsLastOnline标记上次在线用户
2. 重构微信登录逻辑,返回同一OpenId下的所有用户列表而非单用户
3. 新增SwitchUserAsync服务方法与控制器接口,支持同OpenId下切换用户
4. 添加用户实体到输出DTO的映射工具方法
5. 实现切换用户时的身份校验、Token刷新与在线状态更新
2026-06-03 18:28:45 +08:00

81 lines
2.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
/// <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>
/// 微信小程序一键登录
/// </summary>
/// <param name="input">登录输入(含微信 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">切换用户输入含目标用户ID</param>
/// <returns>切换结果(含新 Token 和目标用户详情)</returns>
[HttpPost("switchUser")]
public async Task<BaseResponse<WeChatSwitchUserOutput>> SwitchUserAsync([FromBody] WeChatSwitchUserInput input)
{
try
{
var userId = GetCurrentUserId();
if (userId == null)
{
return BaseResponse<WeChatSwitchUserOutput>.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<WeChatSwitchUserOutput>.Fail(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "切换用户系统异常");
return BaseResponse<WeChatSwitchUserOutput>.Fail("切换用户失败,请稍后重试");
}
}
}