• Senparc 使用笔记


    以前就知道Senparc 这个组件,但是没有仔细研究过,因为我在公司不是负责微信这一块的。

    上一周公司突然要开发微信企业号,接到任务后要求第二天上线,他妈的!

    我和另外一个同事分工,我做功能他调接口,因为他一直在负责微信开发。

    中午的时候我的模块可以测试了,我开始关注他的接口是否调通,结果发现困难重重,他在自己一个个写功能,关键是他还没搞明白到底需要调用那几个接口。

    我感觉到来不及了,突然想起Senparc应该都有封装,拿过来用就可以了。

    下载了demo进行分析,半天没高清微信消息在哪里处理。最后从别人的例子里面发现有个QyMessageHandler的东东,看了看是个抽象类,所有处理消息的方法都在里面,不过是虚方法。

    既然是抽象类,那么真正的处理过程应该是在其子类的重写方法里面完成了。

    然后我自定义了个类QyCustomMessageHandler 继承QyMessageHandler 并重写了其所有虚方法和抽象方法。

    然后在网页的处理类中这样写(我这里是MVC)

            /// <summary>
            /// 微信后台验证地址(使用Post),微信企业后台应用的“修改配置”的Url填写如:http://weixin.senparc.com/qy
            /// </summary>
            [HttpPost]
            public ActionResult Index(PostModel postModel)
            {
                var maxRecordCount = 10;

                postModel.Token = CommonModel.Token;
                postModel.EncodingAESKey = CommonModel.EncodingAESKey;
                postModel.CorpId = CommonModel.CorpId;
                Senparc.Weixin.QY.CommonAPIs.AccessTokenContainer.Register(CommonModel.CorpId, CommonModel.Corpsecret);

                //自定义MessageHandler,对微信请求的详细判断操作都在这里面。
                var messageHandler = new QyCustomMessageHandler(Request.InputStream, postModel, maxRecordCount);

                if (messageHandler.RequestMessage == null)
                {
                    //验证不通过或接受信息有错误
                    VPClub.Log.EasyLog.Error("微信回调验证不通过或有错误", "messageHandler.RequestMessage == null");
                }

                try
                {
                    //执行微信处理过程
                    messageHandler.Execute();

                    //////测试时可开启此记录,帮助跟踪数据,使用前请确保App_Data文件夹存在,且有读写权限。
                    //messageHandler.RequestDocument.Save(Server.MapPath("~/App_Data/Qy/" + DateTime.Now.Ticks + "_Request_" + messageHandler.RequestMessage.FromUserName + ".txt"));
                    //////测试时可开启,帮助跟踪数据
                    //messageHandler.ResponseDocument.Save(Server.MapPath("~/App_Data/Qy/" + DateTime.Now.Ticks + "_Response_" + messageHandler.ResponseMessage.ToUserName + ".txt"));
                    //messageHandler.FinalResponseDocument.Save(Server.MapPath("~/App_Data/Qy/" + DateTime.Now.Ticks + "_FinalResponse_" + messageHandler.ResponseMessage.ToUserName + ".txt"));

                    //自动返回加密后结果
                    return new FixWeixinBugWeixinResult(messageHandler);//为了解决官方微信5.0软件换行bug暂时添加的方法,平时用下面一个方法即可
                }
                catch (Exception ex)
                {
                    VPClub.Log.EasyLog.Error("微信回调错误", ex);
                    return Content("");
                }
            }

  • 相关阅读:
    Oracle EXP
    Using Spring in Web and WinForms
    System.ComponentModel(未完...)
    工作必须得到强势方的支持!
    book.Save()还是bookManager.Save(book)?
    C#中常用的Attribute搜集(刚开始...)
    领域模型是否能够屏蔽数据库?
    合格代码的最基本的标准
    关于配置系统的设计
    对象分类
  • 原文地址:https://www.cnblogs.com/westfruit/p/5226112.html
Copyright © 2020-2023  润新知