docs: 完善文档

This commit is contained in:
Fu Diwei
2021-08-02 16:26:14 +08:00
parent 73bdd09a6c
commit fe43671d6f
4 changed files with 50 additions and 20 deletions

View File

@@ -39,22 +39,24 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Controllers
[Route("a-{app_id}/message-push")]
public IActionResult VerifyMessage(
[FromRoute(Name = "app_id")] string appId,
[FromQuery(Name = "signature")] string signature,
[FromQuery(Name = "timestamp")] string timestamp,
[FromQuery(Name = "nonce")] string nonce,
[FromQuery(Name = "signature")] string signature,
[FromQuery(Name = "echostr")] string echoString)
{
// 验证服务器推送
// 文档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))
var client = _wechatApiHttpClientFactory.Create(appId);
bool valid = client.VerifyEventSignature(
callbackTimestamp: timestamp,
callbackNonce: nonce,
callbackSignature: signature
);
if (!valid)
{
return Content("fail");
}
return Content(echoString);
}
@@ -71,10 +73,8 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Controllers
string content = await reader.ReadToEndAsync();
_logger.LogInformation("接收到微信推送的数据:{0}", content);
var xDoc = XDocument.Parse(content);
string msgType = xDoc.Root!.Element("MsgType")!.Value.ToUpper();
var client = _wechatApiHttpClientFactory.Create(appId);
var msgType = client.DeserializeEventFromXml(content).MessageType;
switch (msgType)
{
case "TEXT":