using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using YD_Event.Application.Client.Dtos;
using YD_Event.Core.Infrastructure.WeChat;
namespace YD_Event.Application.Client;
///
/// 微信小程序接口
///
[ApiDescriptionSettings("微信小程序接口")]
[AllowAnonymous]
public class WeChatController : IDynamicApiController
{
private readonly WeChatService _weChatService;
private readonly ILogger_logger;
public WeChatController(
WeChatService weChatService,
ILoggerlogger)
{
_weChatService = weChatService;
_logger = logger;
}
///
/// 小程序 code 换取 openid 和 session_key
///
/// 包含 code 的输入参数
/// openid、session_key 等信息
[HttpPost("jscode2session")]
[DisplayName("小程序 code 换取 openid")]
public async Task Code2Session([FromBody] WeChatCode2SessionInput input)
{
_logger.LogInformation("开始调用微信 code2session 接口");
var result = await _weChatService.Code2Session(input.Code);
_logger.LogInformation("微信 code2session 接口调用成功,OpenId={OpenId}", result.OpenId);
return new WeChatCode2SessionOutput
{
OpenId = result.OpenId,
SessionKey = result.SessionKey,
UnionId = result.UnionId
};
}
}