微信公众号 ASP.Net Core API开发 接收消息的实现

2021-04-12  乐帮网

netcore 微信公众号

本示例是使用ASP.Net Core,接受和发送消息的一部分,在微信公众号后端配置开发地址的接口。能够接收处理公众号的回调消息。直接上代码把主类贴出来。目前我自己的公众号正在使用。

 [ApiController]
    [Route("api/[controller]")]
    public class MainController : ControllerBase
    {
        private ILogger<PortalController> _logger;
        private readonly IConfiguration _configuration;
        private readonly IHttpClientFactory _clientFactory;
        private readonly IWxEventHandler _wxEventHandler;
        private readonly IWxTextHandler _wxTextHandler;
        private readonly IWxImageHandler _wxImageHandler;
        private readonly IWxVoiceHandler _wxVoiceHandler;
        private readonly IWxVideoHandler _wxVideoHandler;
        private readonly IWxLinkHandler _wxLinkHandler;

        public MainController(ILogger<PortalController> logger, IConfiguration configuration, IHttpClientFactory clientFactory, IWxEventHandler wxEventHandler, IWxTextHandler wxTextHandler, IWxImageHandler wxImageHandler,
            IWxVoiceHandler wxVoiceHandler, IWxVideoHandler wxVideoHandler, IWxLinkHandler wxLinkHandler)
        {
            _logger = logger;
            _configuration = configuration;
            _clientFactory = clientFactory;
            _wxEventHandler = wxEventHandler;
            _wxTextHandler = wxTextHandler;
            _wxImageHandler = wxImageHandler;
            _wxVoiceHandler = wxVoiceHandler;
            _wxVideoHandler = wxVideoHandler;
            _wxLinkHandler = wxLinkHandler;
        }
        [HttpGet]
        public IActionResult Get(string signature, string timestamp, string nonce, string echostr)
        {
            _logger.LogInformation("Portal signature:{0},timestamp:{1},nonce:{2},echostr:{3}", signature, timestamp, nonce, echostr);
            if (string.IsNullOrEmpty(signature) || string.IsNullOrEmpty(timestamp) || string.IsNullOrEmpty(nonce) || string.IsNullOrEmpty(echostr))
                return Ok();
            string token = _configuration["Wx_Sign"];
            if (WxPublic.GetSignature(timestamp, nonce, token) != signature)
                return Ok();
            return Content(echostr, "text/html", Encoding.UTF8);
        }
        [HttpPost]
        public async Task<OkObjectResult> Post()
        {
            try
            {
                string content = string.Empty;
                HttpContext.Request.EnableBuffering();
                using (var reader = new StreamReader(HttpContext.Request.Body, encoding: Encoding.UTF8))
                {
                    content = await reader.ReadToEndAsync();
                    HttpContext.Request.Body.Position = 0;
                }
                _logger.LogInformation("Post body:{0}", content);

                var reqMsg = RequestFactory.Parse(content);

                if (reqMsg == null)
                {
                    _logger.LogInformation("Portal Do Not Defind.");
                    return Ok("success");
                }
                ResBase respones = null;
                switch (reqMsg.MsgType)
                {
                    case WxReqDefine.Event:
                        respones = _wxEventHandler.Process(reqMsg);
                        break;
                    case WxReqDefine.Text:
                        respones = _wxTextHandler.Process(reqMsg as RecMsgText);
                        break;
                    case WxReqDefine.Image:
                        respones = _wxImageHandler.Process(reqMsg as RecMsgImage);
                        break;
                    case WxReqDefine.Voice:
                        respones = _wxVoiceHandler.Process(reqMsg as RecMsgVoice);
                        break;
                    case WxReqDefine.Video:
                        respones = _wxVideoHandler.Process(reqMsg as RecMsgVideo);
                        break;
                    case WxReqDefine.Link:
                        respones = _wxLinkHandler.Process(reqMsg as RecMsgLink);
                        break;
                    default:
                        break;

                }
                if (respones != null)
                {
                    _logger.LogInformation("Portal Respones:{0}.",respones.ToString());
                    return Ok(respones.ToString());
                }
            }
            catch (Exception ex)
            {
                _logger.LogError( ex, "Portal Post");
            }

            return Ok("success");
        }

    }

 

公众号二维码

关注我的微信公众号
在公众号里留言交流
投稿邮箱:1052839972@qq.com

庭院深深深几许?杨柳堆烟,帘幕无重数。
玉勒雕鞍游冶处,楼高不见章台路。
雨横风狂三月暮。门掩黄昏,无计留春住。
泪眼问花花不语,乱红飞过秋千去。

欧阳修

付款二维码

如果感觉对您有帮助
欢迎向作者提供捐赠
这将是创作的最大动力