CallbackController.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using Microsoft.AspNetCore.Mvc;
  2. using OASystem.Domain.Dtos.CallBack.QiYeWeChat;
  3. using OASystem.Domain.Dtos.SmallFun;
  4. using OASystem.Infrastructure.Repositories.Business;
  5. using ILogger = Microsoft.Extensions.Logging.ILogger;
  6. namespace OASystem.API.Controllers
  7. {
  8. /// <summary>
  9. /// 回调地址
  10. /// </summary>
  11. [Route("/callback")]
  12. public class CallbackController : Controller
  13. {
  14. private readonly IMapper _mapper;
  15. private readonly ILogger<CallbackController> _logger;
  16. #region 企业微信 通讯录通知回调key And token
  17. private readonly string _qiYeWechat_Token = "WWiCDK";
  18. private readonly string _qiYeWechat_EncodingAESKey = "3BWKiWnvp6xJGQ5oD3TBaOKYniNgX1g6kZZEehbM3ym";
  19. private readonly string _qiYeWechat_CorpId = "wwe978bef5495a0728";
  20. #endregion
  21. public CallbackController(IMapper mapper, ILogger<CallbackController> logger)
  22. {
  23. _mapper = mapper;
  24. _logger = logger;
  25. }
  26. #region 企业微信回调
  27. /// <summary>
  28. /// 回调通知
  29. /// </summary>
  30. /// <returns></returns>
  31. [Route("memberschange")]
  32. [HttpGet, HttpPost]
  33. public async Task<ActionResult> ApproveCallBack(string msg_signature, string timestamp, string nonce, string echostr)
  34. {
  35. _logger.LogInformation("【企业微信】【通讯录助手】【回调】进入回调");
  36. ApproveCallBackInputDTO input = new ApproveCallBackInputDTO();
  37. input.msg_signature = msg_signature;
  38. input.timestamp = timestamp;
  39. input.nonce = nonce;
  40. input.echostr = echostr;
  41. _logger.LogInformation("【企业微信】【通讯录助手】【回调】【参数】"+ input.ToJson());
  42. if (HttpContext.Request.Method == System.Net.Http.HttpMethod.Get.Method)
  43. {
  44. var model = await VerifyURLCallBack(input);
  45. return Content(model, "text/xml");
  46. }
  47. if (HttpContext.Request.Method == System.Net.Http.HttpMethod.Post.Method)
  48. {
  49. var stream = Request.Body;
  50. var model = await ApproveCallBack(stream, input);
  51. return Content(model, "text/xml");
  52. }
  53. _logger.LogInformation("【企业微信】【通讯录助手】【回调】回调成功");
  54. return Content("ok", "text/xml");
  55. }
  56. /// <summary>
  57. /// 验证URL有效性
  58. /// </summary>
  59. /// <returns></returns>
  60. private async Task<string> VerifyURLCallBack(ApproveCallBackInputDTO input)
  61. {
  62. int ret = 0;
  63. string sEchoStr = "";
  64. try
  65. {
  66. //企业微信官方加解密校验解析类
  67. Tencent.WXBizMsgCrypt wxcpt = new Tencent.WXBizMsgCrypt(_qiYeWechat_Token, _qiYeWechat_EncodingAESKey, _qiYeWechat_CorpId);
  68. string sReqMsgSig = input.msg_signature;
  69. string sReqTimeStamp = input.timestamp;
  70. string sReqNonce = input.nonce;
  71. string sReqEchostr = input.echostr;
  72. //企业微信官方验证URL
  73. ret = wxcpt.VerifyURL(sReqMsgSig, sReqTimeStamp, sReqNonce, sReqEchostr, ref sEchoStr);
  74. if (ret != 0)
  75. {
  76. throw new Exception($"ERR: VerifyURL fail, ret: {ret}");
  77. }
  78. return sEchoStr;
  79. }
  80. catch (Exception ex)
  81. {
  82. return ex.Message;
  83. }
  84. }
  85. /// <summary>
  86. /// 回调通知处理业务
  87. /// </summary>
  88. /// <returns></returns>
  89. private async Task<string> ApproveCallBack(Stream context, ApproveCallBackInputDTO input)
  90. {
  91. var sReqData = "";
  92. int ret = 0;
  93. string sMsg = "";
  94. try
  95. {
  96. //企业微信官方加解密校验解析类
  97. Tencent.WXBizMsgCrypt wxcpt = new Tencent.WXBizMsgCrypt(_qiYeWechat_Token, _qiYeWechat_EncodingAESKey, _qiYeWechat_CorpId);
  98. string sReqMsgSig = input.msg_signature;
  99. string sReqTimeStamp = input.timestamp;
  100. string sReqNonce = input.nonce;
  101. string sReqEchostr = input.echostr;
  102. // Post请求的密文数据
  103. using (var reader = new StreamReader(context))
  104. {
  105. sReqData = await reader.ReadToEndAsync();
  106. }
  107. //回调数据
  108. // 解析之后的明文
  109. ret = wxcpt.DecryptMsg(sReqMsgSig, sReqTimeStamp, sReqNonce, sReqData, ref sMsg);
  110. if (ret != 0)
  111. {
  112. throw new Exception($"ERR: Decrypt Fail, ret: {ret}");
  113. }
  114. // ret==0表示解密成功,sMsg表示解密之后的明文xml串
  115. //下一步处理实际业务数据了
  116. return sMsg;
  117. }
  118. catch (Exception ex)
  119. {
  120. throw new Exception(ex.Message);
  121. }
  122. }
  123. #endregion
  124. }
  125. }