本次提交完成了多个核心业务模块的开发与优化: 1. 宠物模块:新增宠物实体、服务接口与实现,支持创建默认宠物、激活、喂养、进化以及喂养记录查询 2. 签到模块:新增签到实体、服务接口、控制器以及相关DTO,支持用户签到和签到信息查询,新增成长值奖励字段 3. 期刊绑定模块:新增用户期刊关联实体、服务接口与控制器,支持扫码绑定期刊、解绑和查询绑定列表 4. 补偿任务模块:新增补偿任务实体、服务接口与实现,用于处理业务失败后的异步重试补偿 5. 优化微信登录流程:拆分登录与快捷登录接口,支持手机号获取,新增首次登录自动创建默认宠物逻辑 6. 调整基础路由与实体状态:修改微信控制器路由前缀,更新宠物状态枚举与默认值
107 lines
3.9 KiB
C#
107 lines
3.9 KiB
C#
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 和可选的手机号 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>
|
||
/// 切换用户(同一 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("切换用户失败,请稍后重试");
|
||
}
|
||
}
|
||
}
|