2021-07-27 18:37:11 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-07-31 21:01:55 +08:00
|
|
|
|
using System.Xml;
|
|
|
|
|
using System.Xml.Linq;
|
2021-07-27 18:37:11 +08:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
|
|
|
|
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Controllers
|
|
|
|
|
{
|
2021-07-30 14:09:25 +08:00
|
|
|
|
using SKIT.FlurlHttpClient.Wechat.Security;
|
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]
|
|
|
|
|
[Route("notify")]
|
|
|
|
|
public class WechatNotifyController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
private readonly Options.WechatOptions _wechatOptions;
|
|
|
|
|
|
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
|
|
|
|
IOptions<Options.WechatOptions> wechatOptions,
|
|
|
|
|
Services.HttpClients.IWechatApiHttpClientFactory wechatApiHttpClientFactory)
|
2021-07-27 18:37:11 +08:00
|
|
|
|
{
|
|
|
|
|
_logger = loggerFactory.CreateLogger(GetType());
|
|
|
|
|
_wechatOptions = wechatOptions.Value;
|
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 = "signature")] string signature,
|
|
|
|
|
[FromQuery(Name = "timestamp")] string timestamp,
|
|
|
|
|
[FromQuery(Name = "nonce")] string nonce,
|
|
|
|
|
[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
|
|
|
|
|
|
|
|
|
|
var wechatAccount = _wechatOptions.Accounts?.FirstOrDefault(e => e.AppId == appId);
|
|
|
|
|
if (wechatAccount == null)
|
|
|
|
|
return Content("fail");
|
|
|
|
|
|
|
|
|
|
ISet<string> set = new SortedSet<string>() { _wechatOptions.CallbackToken, timestamp!, nonce! };
|
|
|
|
|
string sign = SHA1Utility.Hash(string.Concat(set));
|
|
|
|
|
if (!string.Equals(sign, signature, StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
return Content("fail");
|
|
|
|
|
|
|
|
|
|
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 xDoc = XDocument.Parse(content);
|
2021-08-02 12:43:01 +08:00
|
|
|
|
string msgType = xDoc.Root!.Element("MsgType")!.Value.ToUpper();
|
2021-07-31 21:01:55 +08:00
|
|
|
|
|
|
|
|
|
var client = _wechatApiHttpClientFactory.Create(appId);
|
2021-08-02 12:43:01 +08:00
|
|
|
|
switch (msgType)
|
2021-07-31 21:01:55 +08:00
|
|
|
|
{
|
|
|
|
|
case "TEXT":
|
|
|
|
|
{
|
|
|
|
|
var eventModel = client.DeserializeEventFromXml<TextMessageEvent>(content);
|
|
|
|
|
// Do Something
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "IMAGE":
|
|
|
|
|
{
|
|
|
|
|
var eventModel = client.DeserializeEventFromXml<ImageMessageEvent>(content);
|
|
|
|
|
// Do Something
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-27 18:37:11 +08:00
|
|
|
|
return Content("success");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|