Beispiel #1
0
        /// <summary>
        /// 处理请求
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public string ProcessRequest(EnterParamEntity param)
        {
            //解密成xml格式
            var requestXML = WxRequest.Request(param);

            if (string.IsNullOrWhiteSpace(requestXML))
            {
                LogHelper.WriteLog("【微信POST】,消息体为空!", LogMessageType.Info);
                return(string.Empty);
            }

            //将XML转化成对应的实体类
            var requestModel = MessageFactory.CreateRequestMessage(requestXML);

            if (requestModel == null)
            {
                LogHelper.WriteLog("【微信POST】,解析消息体为空!", LogMessageType.Info);
                return(string.Empty);
            }

            //响应微信请求消息
            BaseResponse = new WxResponse();
            string result = BaseResponse.Execute(requestModel, param);

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// 微信响应方法
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public override string Response(BaseResponseMessage response, EnterParamEntity param)
        {
            var           _Response = response as ImageResponseMessage;
            StringBuilder result    = new StringBuilder();

            result.AppendFormat("<xml>");
            result.AppendFormat("<ToUserName><![CDATA[{0}]]></ToUserName>", _Response.ToUserName);
            result.AppendFormat("<FromUserName><![CDATA[{0}]]></FromUserName>", _Response.FromUserName);
            result.AppendFormat("<CreateTime>{0}></CreateTime>", _Response.CreateTime);
            result.AppendFormat("<MsgType><![CDATA[{0}]]></MsgType>", _Response.MsgType);
            result.AppendFormat("<Image>");
            result.AppendFormat("<MediaId><![CDATA[{0}]]></MediaId>", _Response.MediaId);
            result.AppendFormat("</Image>");
            result.AppendFormat("</xml>");
            if (param.IsAes)
            {
                string data    = string.Empty;
                int    encrypt = new WXBizMsgCrypt(param.token, param.EncodingAESKey, param.appid).
                                 EncryptMsg(result.ToString(), _Response.CreateTime.ToString(), param.nonce, ref data);
                result = new StringBuilder(data);
                if (encrypt != 0)
                {
                    LogHelper.WriteLog("【微信响应图片消息】加密失败", LogMessageType.Error);
                }
            }
            return(result.ToString());
        }
Beispiel #3
0
        /// <summary>
        /// 创建响应类型消息
        /// </summary>
        /// <param name="requestMessage">请求消息</param>
        /// <param name="param">请求参数</param>
        /// <param name="msgType">响应消息类型</param>
        /// <returns></returns>
        //public static string CreateResponseModel(BaseResponseMessage responseMessage, MsgTypeResponseEnum msgType)
        //{
        //    try
        //    {
        //        WxBaseResponse response = null;
        //        #region 响应类型
        //        switch (msgType)
        //        {
        //            //文本消息
        //            case MsgTypeResponseEnum.TEXT:
        //                response = new WxTextResponse();
        //                break;
        //            //图片消息
        //            case MsgTypeResponseEnum.IMAGE:
        //                response = new WxImageResponse();
        //                break;
        //            //视频消息
        //            case MsgTypeResponseEnum.VIDEO:
        //                response = new WxVideoResponse();
        //                break;
        //            //音乐消息
        //            case MsgTypeResponseEnum.MUSIC:
        //                response = new WxMusicResponse();
        //                break;
        //            //图文消息
        //            case MsgTypeResponseEnum.NEWS:
        //                response = new WxNewsResponse();
        //                break;
        //            //语音消息
        //            case MsgTypeResponseEnum.VOICE:
        //                response = new WxVoiceResponse();
        //                break;
        //            //默认回复文本消息
        //            default:
        //                response = new WxTextResponse();
        //                break;
        //        }

        //        #endregion
        //        return response;
        //    }
        //    catch (Exception ex)
        //    {
        //        LogHelper.WriteLog("【微信】响应消息出错", ex);
        //        return string.Empty;
        //    }
        //}
        #endregion

        #region 创建响应类型消息
        /// <summary>
        /// 创建响应类型消息
        /// </summary>
        /// <param name="requestMessage">请求消息</param>
        /// <param name="param">请求参数</param>
        /// <param name="msgType">响应消息类型</param>
        /// <returns></returns>
        public static string CreateResponseType(BaseResponseMessage responseMessage, EnterParamEntity param, MsgTypeResponseEnum msgType)
        {
            try
            {
                WxBaseResponse response = null;
                #region 响应类型
                switch (msgType)
                {
                //文本消息
                case MsgTypeResponseEnum.TEXT:
                    response = new WxTextResponse();
                    break;

                //图片消息
                case MsgTypeResponseEnum.IMAGE:
                    response = new WxImageResponse();
                    break;

                //视频消息
                case MsgTypeResponseEnum.VIDEO:
                    response = new WxVideoResponse();
                    break;

                //音乐消息
                case MsgTypeResponseEnum.MUSIC:
                    response = new WxMusicResponse();
                    break;

                //图文消息
                case MsgTypeResponseEnum.NEWS:
                    response = new WxNewsResponse();
                    break;

                //语音消息
                case MsgTypeResponseEnum.VOICE:
                    response = new WxVoiceResponse();
                    break;

                //默认回复文本消息
                default:
                    response = new WxTextResponse();
                    break;
                }

                #endregion
                return(response.Response(responseMessage, param));
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("【微信】响应消息出错", ex);
                return(string.Empty);
            }
        }
Beispiel #4
0
        /// <summary>
        /// 微信公众号 服务器配置
        /// </summary>
        /// <returns></returns>
        public void Index()
        {
            var param = new EnterParamEntity
            {
                echoString    = Request.QueryString["echoStr"],
                signature     = Request.QueryString["signature"],
                timestamp     = Request.QueryString["timestamp"],
                nonce         = Request.QueryString["nonce"],
                encrypt_type  = Request.QueryString["encrypt_type"],
                msg_signature = Request.QueryString["msg_signature"],
                InputStream   = Request.InputStream
            };

            //请求方式
            string method = Request.HttpMethod;

            //验签
            if (new CheckSignature().Check(param.token, param.signature, param.timestamp, param.nonce))
            {
                if (method.ToUpperInvariant() != "POST")
                {
                    #region 成为开发者第一步
                    //token 与公众号后台token保持一致

                    if (string.IsNullOrEmpty(token))
                    {
                        Response.Write("Token 错误");
                    }

                    //回传 echoString
                    Response.Write(param.echoString);
                    #endregion
                }
                else
                {
                    #region 废弃
                    ////Response.Write("success");
                    ////var model = WxRequest.Request(param);
                    ////var responseResult = MessageFactory.CreateResponseMessage(model, param);
                    ////LogHelper.WriteLog("【微信】响应消息:" + responseResult, LogMessageType.Info);
                    ////Response.Write(responseResult);
                    #endregion

                    string result = new WxService().ProcessRequest(param);
                    Response.Write(result);
                }
            }//验签结束
        }
Beispiel #5
0
        /// <summary>
        /// 微信响应方法
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public override string Response(BaseResponseMessage response, EnterParamEntity param)
        {
            var           _Response = response as MusicResponseMessage;
            StringBuilder result    = new StringBuilder();

            result.AppendFormat("<xml>");
            result.AppendFormat("<ToUserName><![CDATA[{0}]]></ToUserName>", _Response.ToUserName);
            result.AppendFormat("<FromUserName><![CDATA[{0}]]></FromUserName>", _Response.FromUserName);
            result.AppendFormat("<CreateTime>{0}></CreateTime>", _Response.CreateTime);
            result.AppendFormat("<MsgType><![CDATA[{0}]]></MsgType>", _Response.MsgType);
            result.AppendFormat("<Music>");
            if (!string.IsNullOrWhiteSpace(_Response.Title))
            {
                result.AppendFormat("<Title><![CDATA[{0}]]></Title>", _Response.Title);
            }
            if (!string.IsNullOrWhiteSpace(_Response.Description))
            {
                result.AppendFormat("<Description><![CDATA[{0}]]></Description>", _Response.Description);
            }
            if (!string.IsNullOrWhiteSpace(_Response.MusicURL))
            {
                result.AppendFormat("<MusicUrl><![CDATA[{0}]]></MusicUrl>", _Response.MusicURL);
            }
            if (!string.IsNullOrWhiteSpace(_Response.HQMusicUrl))
            {
                result.AppendFormat("<HQMusicUrl><![CDATA[{0}]]></HQMusicUrl>", _Response.HQMusicUrl);
            }
            if (!string.IsNullOrWhiteSpace(_Response.Description))
            {
                result.AppendFormat("<ThumbMediaId><![CDATA[{0}]]></ThumbMediaId>", _Response.ThumbMediaId);
            }
            result.AppendFormat("</Music>");
            result.AppendFormat("</xml>");
            if (param.IsAes)
            {
                string data    = string.Empty;
                int    encrypt = new WXBizMsgCrypt(param.token, param.EncodingAESKey, param.appid).
                                 EncryptMsg(result.ToString(), _Response.CreateTime.ToString(), param.nonce, ref data);
                result = new StringBuilder(data);
                if (encrypt != 0)
                {
                    LogHelper.WriteLog("【微信响应文本消息】加密失败", LogMessageType.Error);
                }
            }
            return(result.ToString());
        }
Beispiel #6
0
 /// <summary>
 /// 响应消息
 /// </summary>
 /// <param name="response">请求消息</param>
 /// <param name="param">请求参数</param>
 /// <returns></returns>
 public string Execute(BaseRequestMessage request, EnterParamEntity param)
 {
     string result = string.Empty;
     var requestType = (MsgTypeRequestEnum)Enum.Parse(typeof(MsgTypeRequestEnum), request.MsgType.ToUpperInvariant());
     BaseResponseMessage responseModel = null;
     switch (requestType)
     {
         //文本消息
         case MsgTypeRequestEnum.TEXT:
             responseDelegate = CreateResponseMessageFromText;
             result = responseDelegate(request, param);
             break;
         #region MyRegion
         //图片消息
         case MsgTypeRequestEnum.IMAGE:
             responseModel = new TextResponseMessage
                 {
                     FromUserName = request.ToUserName,
                     ToUserName = request.FromUserName,
                     MsgType = MsgTypeResponseEnum.TEXT.ToString().ToLower(),
                     Content = "您好,您发送了一张图片!"
                 };
             result = MessageFactory.CreateResponseType(responseModel, param, MsgTypeResponseEnum.TEXT);
             break;
         //视频消息
         case MsgTypeRequestEnum.VIDEO:
             return "";
         //链接消息
         case MsgTypeRequestEnum.LINK:
             return "";
         //地理消息
         case MsgTypeRequestEnum.LOCATION:
             return "";
         //短视频消息
         case MsgTypeRequestEnum.SHORTVIDEO:
             return "";
         //音频消息
         case MsgTypeRequestEnum.VOICE:
             return "";
         default: return "";
         #endregion
     }
     return result;
 }
Beispiel #7
0
 /// <summary>
 /// 请求消息
 /// </summary>
 /// <param name="param"></param>
 /// <param name="bug"></param>
 /// <returns></returns>
 public static string Request(EnterParamEntity param, bool bug = true)
 {
     using (StreamReader sr = new StreamReader(param.InputStream, Encoding.UTF8))
     {
         string data = string.Empty;
         try
         {
             string requestXml = sr.ReadToEnd();
             LogHelper.WriteLog("【微信POST】" + requestXml, LogMessageType.Info);
             //安全模式下
             if (!string.IsNullOrWhiteSpace(param.encrypt_type) && param.encrypt_type.ToUpperInvariant() == "AES")
             {
                 //加密模式
                 param.IsAes = true;
                 //解密消息
                 var result = new WXBizMsgCrypt(param.token, param.EncodingAESKey, param.appid).
                              DecryptMsg(param.msg_signature, param.timestamp, param.nonce, requestXml, ref data);
                 if (result != 0)
                 {
                     LogHelper.WriteLog("【微信POST】,消息解密失败!", LogMessageType.Error);
                 }
             }
             else
             {
                 //明文模式
                 param.IsAes = false;
                 data        = requestXml;
             }
             if (bug)
             {
                 LogHelper.WriteLog("【微信POST】,消息体:" + data, LogMessageType.Info);
             }
             return(data);
         }
         catch (Exception ex)
         {
             LogHelper.WriteLog("处理【微信消息】返回错误", ex);
             return(string.Empty);
         }
     }
 }
Beispiel #8
0
        /// <summary>
        /// 微信响应方法
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public override string Response(BaseResponseMessage response, EnterParamEntity param)
        {
            var           _Response = response as NewsResponseMessage;
            StringBuilder result    = new StringBuilder();

            result.AppendFormat("<xml>");
            result.AppendFormat("<ToUserName><![CDATA[{0}]]></ToUserName>", _Response.ToUserName);
            result.AppendFormat("<FromUserName><![CDATA[{0}]]></FromUserName>", _Response.FromUserName);
            result.AppendFormat("<CreateTime>{0}></CreateTime>", _Response.CreateTime);
            result.AppendFormat("<MsgType><![CDATA[{0}]]></MsgType>", _Response.MsgType);
            result.AppendFormat("<ArticleCount>{0}</ArticleCount>", _Response.ArticleCount);
            result.AppendFormat("<Articles>");
            foreach (var item in _Response.Articles)
            {
                result.AppendFormat("<item>");
                result.AppendFormat("<Title><![CDATA[{0}]]></Title>", item.Title);
                result.AppendFormat("<Description><![CDATA[{0}]]></Description>", item.Description);
                result.AppendFormat("<PicUrl><![CDATA[{0}]]></PicUrl>", item.PicUrl);
                result.AppendFormat("<Url><![CDATA[{0}]]></Url>", item.Url);
                result.AppendFormat("</item>");
            }
            result.AppendFormat("</Articles>");
            result.AppendFormat("</xml>");
            LogHelper.WriteLog("【微信Response】响应消息明文:" + result.ToString(), LogMessageType.Info);
            if (param.IsAes)
            {
                string data    = string.Empty;
                int    encrypt = new WXBizMsgCrypt(param.token, param.EncodingAESKey, param.appid).
                                 EncryptMsg(result.ToString(), _Response.CreateTime.ToString(), param.nonce, ref data);
                result = new StringBuilder(data);
                if (encrypt != 0)
                {
                    LogHelper.WriteLog("【微信响应文本消息】加密失败", LogMessageType.Error);
                }
            }
            return(result.ToString());
        }
Beispiel #9
0
 /// <summary>
 /// 响应消息
 /// </summary>
 /// <param name="response">响应消息实体</param>
 /// <param name="param">微信请求参数</param>
 /// <returns>响应消息</returns>
 public abstract string Response(BaseResponseMessage response, EnterParamEntity param);
Beispiel #10
0
        /// <summary>
        ///  处理来自文本类消息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public string CreateResponseMessageFromText(BaseRequestMessage request, EnterParamEntity param)
        {
            string result = string.Empty;
            BaseResponseMessage responseModel = null;

            var requestModel = request as TextRequestMessage;
            if (requestModel.Content.Equals("您好"))
            {
                responseModel = new TextResponseMessage
                  {
                      FromUserName = request.ToUserName,
                      ToUserName = request.FromUserName,
                      MsgType = MsgTypeResponseEnum.TEXT.ToString().ToLower(),
                      Content = GetContent("")
                  };
                result = MessageFactory.CreateResponseType(responseModel, param, MsgTypeResponseEnum.TEXT);
            }
            else if (requestModel.Content.Equals("图文"))
            {
                responseModel = new NewsResponseMessage
                {
                    FromUserName = request.ToUserName,
                    ToUserName = request.FromUserName,
                    MsgType = MsgTypeResponseEnum.NEWS.ToString().ToLower(),
                    ArticleCount = "1",
                    Articles = new List<Article>
                    {
                      new Article{
                       Title ="测试图文标题1",
                        Description="测试图文描述",
                         PicUrl="http://hn5ery.natappfree.cc/Content/images/pic1.png",
                          Url="http://m.flm365.com/member/Index"
                      },
                      new Article{
                       Title ="测试图文标题2",
                        Description="测试图文描述2",
                         PicUrl="http://hn5ery.natappfree.cc/Content/images/img1.jpg",
                          Url="http://m.flm365.com/Member/Store"
                      },
                       new Article{
                       Title ="测试图文标题3",
                        Description="测试图文描述3",
                         PicUrl="http://hn5ery.natappfree.cc/Content/images/img2.jpg",
                          Url="http://m.flm365.com/Member/LoanList"
                      },
                    }
                };
                result = MessageFactory.CreateResponseType(responseModel, param, MsgTypeResponseEnum.NEWS);
            }
            else
            {
                responseModel = new TextResponseMessage
                {
                    FromUserName = request.ToUserName,
                    ToUserName = request.FromUserName,
                    MsgType = MsgTypeResponseEnum.TEXT.ToString().ToLower(),
                    Content = GetContent(requestModel.Content)
                };
                result = MessageFactory.CreateResponseType(responseModel, param, MsgTypeResponseEnum.TEXT);
            }


            return result;
        }