微信公众平台开发 - 接入公众号(.NetCore+WebApi)

1.使用WebApi来配置公众号服务器地址,get用于签名,post用于接口,需要注意路由的定义

 

 

namespace Tzj.WeiXinSite.Controllers.Api
{
    [Route("api/[controller]")]
    [ApiController]
    public class TengXunController : BaseApiController
    {
        const string appId = "";
        const string token = "";
        const string doMain = "";

        [HttpGet("TzjWeChatApi")]
        public ActionResult GetTzjWeChatApi()
        {
            Request.Query.TryGetValue("signature", out StringValues signature);
            Request.Query.TryGetValue("timestamp", out StringValues timestamp);
            Request.Query.TryGetValue("nonce", out StringValues nonce);
            Request.Query.TryGetValue("echostr", out StringValues echostr);

            WeChatRequestApiQQ webChat = new WeChatRequestApiQQ();
            string activeStr = webChat.CheckSignature(token, signature, timestamp, nonce, echostr);
            if (activeStr.Length > 0)
            {
                return Content(activeStr);
            }
            else
            {
                return Content("");
            }
        }

        [HttpPost("TzjWeChatApi")]
        public ActionResult PostTzjWeChatApi()
        {
            string reMesg = "你好,很高兴认识你~";
            try
            {
                StreamReader stream = new StreamReader(Request.Body);
                string messageStr = stream.ReadToEndAsync().GetAwaiter().GetResult();
                if (string.IsNullOrEmpty(messageStr))
                {
                    return Content("");
                }
                Log4netHelper.Error(new Exception("messageStr=" + messageStr));
                var message = ReceivingStandardMessages.Parse(messageStr);
                if (message is ReceivingStandardMessages.EventMessage)
                {
                    Log4netHelper.Error(new Exception("EventMessage=" + messageStr));
                    reMesg = WeChatOnRecieveEvent(message);
                }
                else if (message is ReceivingStandardMessages.TextMessage)
                {
                    Log4netHelper.Error(new Exception("TextMessage=" + messageStr));
                    reMesg = GetReplyText(message);
                }
                else
                {
                    reMesg = "";
                }

                Log4netHelper.Error(new Exception("reMesg=" + reMesg));
            }
            catch (Exception ex)
            {
                Log4netHelper.Error(ex);
            }
            return Content(reMesg);
        }

        private static string WeChatOnRecieveEvent(ReceivingStandardMessages.Message message)
        {
            var textMessage = (ReceivingStandardMessages.EventMessage)message;
            var eventType = textMessage.Event;
            switch (eventType)
            {
                case "subscribe":
                    //var reMesg = GetReplyArticleNews(message);
                    var reMesg = ReplyArticle(message.FromUserName, message.ToUserName);
                    return reMesg;
                case "UNSUBSCRIBE":
                    return "";
                case "SCAN":
                    return "";
                case "LOCATION":
                    return "";
                case "CLICK":
                    return "";
                case "VIEW":
                    return "";
                default:
                    return "";
            }
        }

        private static string GetReplyText(ReceivingStandardMessages.Message message)
        {
            var reMesg = "";
            var textMessage = (ReceivingStandardMessages.TextMessage)message;
            var content = textMessage.Content;
            var replyMessage = new PassiveUserReplyMessage.TextMessage
            {
                Content = "",
                CreateTime = DateTimeOffset.Now.ToUnixTimeMilliseconds(),
                ToUserName = message.FromUserName,
                FromUserName = message.ToUserName,
                MsgType = "text",
            };
            if (content != null)
            {
                var result = "自定义回复正在建设!";
                replyMessage.Content = result;
            }
            var _mesg = PassiveUserReplyMessage.Parse(replyMessage);
            if (_mesg != null)
            {
                reMesg = _mesg;
            }
            return reMesg;
        }

        private static string GetReplyArticleNews(ReceivingStandardMessages.Message message)
        {
            var reMesg = "";
            List<PassiveUserReplyMessage.Article> articleList = BuildArticles();

            var replyMessage = new PassiveUserReplyMessage.ArticleMessage
            {
                ArticleCount = 3,
                CreateTime = DateTimeOffset.Now.ToUnixTimeMilliseconds(),
                ToUserName = message.FromUserName,
                FromUserName = message.ToUserName,
                MsgType = "news",
                Articles = articleList.ToArray()
            };

            var _mesg = PassiveUserReplyMessage.Parse(replyMessage);
            if (_mesg != null)
            {
                reMesg = _mesg;
            }
            return reMesg;
        }
    }
}

 

posted @ 2022-08-25 14:19  竹殇  阅读(588)  评论(0编辑  收藏  举报