mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-09-19 01:58:14 +08:00
docs: 完善微信公众平台模块的示例项目
This commit is contained in:
@@ -4,6 +4,8 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml;
|
||||
using System.Xml.Linq;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
@@ -11,6 +13,7 @@ using Microsoft.Extensions.Options;
|
||||
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Controllers
|
||||
{
|
||||
using SKIT.FlurlHttpClient.Wechat.Security;
|
||||
using SKIT.FlurlHttpClient.Wechat.Api.Events;
|
||||
|
||||
[ApiController]
|
||||
[Route("notify")]
|
||||
@@ -20,22 +23,26 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Controllers
|
||||
|
||||
private readonly Options.WechatOptions _wechatOptions;
|
||||
|
||||
private readonly Services.HttpClients.IWechatApiHttpClientFactory _wechatApiHttpClientFactory;
|
||||
|
||||
public WechatNotifyController(
|
||||
ILoggerFactory loggerFactory,
|
||||
IOptions<Options.WechatOptions> wechatOptions)
|
||||
IOptions<Options.WechatOptions> wechatOptions,
|
||||
Services.HttpClients.IWechatApiHttpClientFactory wechatApiHttpClientFactory)
|
||||
{
|
||||
_logger = loggerFactory.CreateLogger(GetType());
|
||||
_wechatOptions = wechatOptions.Value;
|
||||
_wechatApiHttpClientFactory = wechatApiHttpClientFactory;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("message-push")]
|
||||
[Route("a-{app_id}/message-push")]
|
||||
public IActionResult VerifyMessage(
|
||||
[FromQuery(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)
|
||||
[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)
|
||||
{
|
||||
// 验证服务器推送
|
||||
// 文档:https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html
|
||||
@@ -53,8 +60,9 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Controllers
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("message-push")]
|
||||
public async Task<IActionResult> ReceiveMessage()
|
||||
[Route("a-{app_id}/message-push")]
|
||||
public async Task<IActionResult> ReceiveMessage(
|
||||
[FromRoute(Name = "app_id")] string appId)
|
||||
{
|
||||
// 接收服务器推送
|
||||
// 文档:https://developers.weixin.qq.com/miniprogram/dev/framework/server-ability/message-push.html
|
||||
@@ -63,6 +71,27 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Controllers
|
||||
string content = await reader.ReadToEndAsync();
|
||||
_logger.LogInformation("接收到微信推送的数据:{0}", content);
|
||||
|
||||
var xDoc = XDocument.Parse(content);
|
||||
string @event = xDoc.Root!.Element("Event")!.Value.ToUpper();
|
||||
|
||||
var client = _wechatApiHttpClientFactory.Create(appId);
|
||||
switch (@event)
|
||||
{
|
||||
case "TEXT":
|
||||
{
|
||||
var eventModel = client.DeserializeEventFromXml<TextMessageEvent>(content);
|
||||
// Do Something
|
||||
}
|
||||
break;
|
||||
|
||||
case "IMAGE":
|
||||
{
|
||||
var eventModel = client.DeserializeEventFromXml<ImageMessageEvent>(content);
|
||||
// Do Something
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return Content("success");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user