• Web API实现微信公众平台开发-接收数据Post


    介绍

    当普通微信用户向公众账号发消息时,微信服务器将POST消息的XML数据包到开发者填写的URL上。在微信用户和公众号产生交互的过程中,用户的某些操作会使得微信服务器通过事件推送的形式通知到开发者在开发者中心处设置的服务器地址,从而开发者可以获取到该信息。其中,某些事件推送在发生后,是允许开发者回复用户的,某些则不允许。

    代码实现

    1、接收参数。

    var postModel = GetPostModel(request);

    2、验证签名。

     if (!CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, Token))
                    {
                        return new HttpResponseMessage
                        {
                            Content = new StringContent("参数错误!")
                        };
                    }

    3、数据处理。

     [HttpPost]
            [ActionName("CallBack")]
            public HttpResponseMessage HandleMsgFromWeChat(HttpRequestMessage request)
            {
                try
                {
                    var postModel = GetPostModel(request);
                    if (!CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, Token))
                    {
                        return new HttpResponseMessage
                        {
                            Content = new StringContent("参数错误!")
                        };
                    }
                    string xmlContent = request.Content.ReadAsStringAsync().Result;
    
                    string domain = BqoolWebSite.WebSiteMapping[BqoolWebSiteType.BigCRMWebService][CommonSetting.GetEnvironmentMode()];
                    string url = domain + "/api/WeChat/ProcessXml";
                    var data = new ProcessXmlParam()
                    {
                        Account = "WeChat",
                        UpdateUser = "WeChat",
                        Date = DateTime.UtcNow.ToUnixTimestamp(),
                        Action = BaseRequestParam.ActionType.Add,
                        xml = xmlContent
                    };
                    _nLogger.Info($"WeChat ProcessXml Call BigCRM Web - Url : {url}, data : {data.ToJsonString()}");
                    var result = HttpTools.EncryptPost<ProcessXmlParam, ApiResult>(url, data);
                    _nLogger.Info($"WeChat ProcessXml Call BigCRM Web Result - Url : {url}, result : {result.ToJsonString()}");
                    if (result.Success && result.Data != null)
                    {
                        return new HttpResponseMessage
                        {
                            Content = new StringContent(result.Data.ToString())
                        };
                    }
                    return new HttpResponseMessage
                    {
                        Content = new StringContent("错误!")
                    };
                }
                catch (Exception ex)
                {
                    _nLogger.Error(ex);
                    return new HttpResponseMessage
                    {
                        Content = new StringContent("")
                    };
                }
            }
    
            /// <summary>
            /// 验签参数
            /// </summary>
            /// <param name="content"></param>
            /// <returns></returns>
            private PostModel GetPostModel(HttpRequestMessage content)
            {
                string signature = (from kvp in content.GetQueryNameValuePairs()
                                    where kvp.Key == "signature"
                                    select kvp.Value).FirstOrDefault();
    
                string timestamp = (from kvp in content.GetQueryNameValuePairs()
                                    where kvp.Key == "timestamp"
                                    select kvp.Value).FirstOrDefault();
    
                string nonce = (from kvp in content.GetQueryNameValuePairs()
                                where kvp.Key == "nonce"
                                select kvp.Value).FirstOrDefault();
    
                return new PostModel()
                {
                    Signature = signature,
                    Timestamp = timestamp,
                    Nonce = nonce
                };
            }

    总结

    其实整个api过程是很简单的,主要的是自己业务逻辑的处理。

  • 相关阅读:
    【重要】ASCII码表
    深入了解php opcode缓存原理
    php 请求参数限制
    【Demo】 生成二维码 和 条形码
    【Demo】HTML5 拍照上传
    jq cookie的使用
    vue.js中的v-for输出数组理解
    js无限轮播的写法
    HTML5视频的使用总结
    angular报错总计
  • 原文地址:https://www.cnblogs.com/lyl6796910/p/14391793.html
Copyright © 2020-2023  润新知