2021-12-28 14:05:39 +08:00
|
|
|
|
using System.IO;
|
2021-07-27 18:37:11 +08:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
2021-12-28 14:05:39 +08:00
|
|
|
|
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Controllers
|
2021-07-27 18:37:11 +08:00
|
|
|
|
{
|
2021-07-31 21:01:55 +08:00
|
|
|
|
using SKIT.FlurlHttpClient.Wechat.Api.Events;
|
2021-07-30 14:09:25 +08:00
|
|
|
|
|
2021-07-27 18:37:11 +08:00
|
|
|
|
[ApiController]
|
2021-12-28 14:05:39 +08:00
|
|
|
|
[Route("api/notify")]
|
2021-07-27 18:37:11 +08:00
|
|
|
|
public class WechatNotifyController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
2021-07-31 21:01:55 +08:00
|
|
|
|
private readonly Services.HttpClients.IWechatApiHttpClientFactory _wechatApiHttpClientFactory;
|
|
|
|
|
|
2021-07-27 18:37:11 +08:00
|
|
|
|
public WechatNotifyController(
|
|
|
|
|
ILoggerFactory loggerFactory,
|
2021-07-31 21:01:55 +08:00
|
|
|
|
Services.HttpClients.IWechatApiHttpClientFactory wechatApiHttpClientFactory)
|
2021-07-27 18:37:11 +08:00
|
|
|
|
{
|
|
|
|
|
_logger = loggerFactory.CreateLogger(GetType());
|
2021-07-31 21:01:55 +08:00
|
|
|
|
_wechatApiHttpClientFactory = wechatApiHttpClientFactory;
|
2021-07-27 18:37:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2021-07-31 21:01:55 +08:00
|
|
|
|
[Route("a-{app_id}/message-push")]
|
2021-07-27 18:37:11 +08:00
|
|
|
|
public IActionResult VerifyMessage(
|
2021-07-31 21:01:55 +08:00
|
|
|
|
[FromRoute(Name = "app_id")] string appId,
|
|
|
|
|
[FromQuery(Name = "timestamp")] string timestamp,
|
|
|
|
|
[FromQuery(Name = "nonce")] string nonce,
|
2021-08-02 16:26:14 +08:00
|
|
|
|
[FromQuery(Name = "signature")] string signature,
|
2021-07-31 21:01:55 +08:00
|
|
|
|
[FromQuery(Name = "echostr")] string echoString)
|
2021-07-27 18:37:11 +08:00
|
|
|
|
{
|
|
|
|
|
// 验证服务器推送
|
|
|
|
|
// 文档:https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html
|
|
|
|
|
|
2021-08-02 16:26:14 +08:00
|
|
|
|
var client = _wechatApiHttpClientFactory.Create(appId);
|
2021-08-02 20:21:45 +08:00
|
|
|
|
bool valid = client.VerifyEventSignatureForEcho(callbackTimestamp: timestamp, callbackNonce: nonce, callbackSignature: signature);
|
2021-08-02 16:26:14 +08:00
|
|
|
|
if (!valid)
|
|
|
|
|
{
|
2021-07-27 18:37:11 +08:00
|
|
|
|
return Content("fail");
|
2021-08-02 16:26:14 +08:00
|
|
|
|
}
|
2021-07-27 18:37:11 +08:00
|
|
|
|
|
|
|
|
|
return Content(echoString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
2021-07-31 21:01:55 +08:00
|
|
|
|
[Route("a-{app_id}/message-push")]
|
|
|
|
|
public async Task<IActionResult> ReceiveMessage(
|
|
|
|
|
[FromRoute(Name = "app_id")] string appId)
|
2021-07-27 18:37:11 +08:00
|
|
|
|
{
|
|
|
|
|
// 接收服务器推送
|
2021-08-02 12:43:01 +08:00
|
|
|
|
// 文档:https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_event_pushes.html
|
2021-07-27 18:37:11 +08:00
|
|
|
|
|
|
|
|
|
using var reader = new StreamReader(Request.Body, Encoding.UTF8);
|
|
|
|
|
string content = await reader.ReadToEndAsync();
|
|
|
|
|
_logger.LogInformation("接收到微信推送的数据:{0}", content);
|
|
|
|
|
|
2021-07-31 21:01:55 +08:00
|
|
|
|
var client = _wechatApiHttpClientFactory.Create(appId);
|
2021-08-02 16:26:14 +08:00
|
|
|
|
var msgType = client.DeserializeEventFromXml(content).MessageType;
|
2021-08-02 12:43:01 +08:00
|
|
|
|
switch (msgType)
|
2021-07-31 21:01:55 +08:00
|
|
|
|
{
|
2021-12-01 09:12:09 +08:00
|
|
|
|
case "text":
|
2021-07-31 21:01:55 +08:00
|
|
|
|
{
|
|
|
|
|
var eventModel = client.DeserializeEventFromXml<TextMessageEvent>(content);
|
|
|
|
|
// Do Something
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2021-12-01 09:12:09 +08:00
|
|
|
|
case "image":
|
2021-07-31 21:01:55 +08:00
|
|
|
|
{
|
|
|
|
|
var eventModel = client.DeserializeEventFromXml<ImageMessageEvent>(content);
|
|
|
|
|
// Do Something
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-27 18:37:11 +08:00
|
|
|
|
return Content("success");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|