Наследование: Senparc.Weixin.MP.Entities.ResponseMessageBase, IResponseMessageBase
Пример #1
0
        /// <summary>
        /// 从返回结果XML转换成IResponseMessageBase实体类
        /// </summary>
        /// <param name="xml">返回给服务器的Response Xml</param>
        /// <returns></returns>
        public static IResponseMessageBase CreateFromResponseXml(string xml)
        {
            try
            {
                var doc = XDocument.Parse(xml);
                ResponseMessageBase responseMessage = null;
                var msgType = (ResponseMsgType)Enum.Parse(typeof(ResponseMsgType), doc.Root.Element("MsgType").Value, true);
                switch (msgType)
                {
                case ResponseMsgType.Text:
                    responseMessage = new ResponseMessageText();
                    break;

                case ResponseMsgType.News:
                    responseMessage = new ResponseMessageNews();
                    break;

                case ResponseMsgType.Music:
                    responseMessage = new ResponseMessageMusic();
                    break;
                }

                responseMessage.FillEntityWithXml(doc);
                return(responseMessage);
            }
            catch (Exception ex)
            {
                throw new WeixinException("ResponseMessageBase.CreateFromResponseXml<T>过程发生异常!", ex);
            }
        }
 //<?xml version="1.0" encoding="utf-8"?>
 //<xml>
 //  <ToUserName><![CDATA[olPjZjsXuQPJoV0HlruZkNzKc91E]]></ToUserName>
 //  <FromUserName><![CDATA[gh_a96a4a619366]]></FromUserName>
 //  <CreateTime>63497820384</CreateTime>
 //  <MsgType>text</MsgType>
 //  <Content><![CDATA[您刚才发送了文字信息:中文
 //您还可以发送【位置】【图片】【语音】信息,查看不同格式的回复。
 //SDK官方地址:http://weixin.senparc.com]]></Content>
 //  <FuncFlag>0</FuncFlag>
 //</xml>
 /// <summary>
 /// 获取XDocument转换后的IResponseMessageBase实例(通常在反向读取日志的时候用到)。
 /// 如果MsgType不存在,抛出UnknownRequestMsgTypeException异常
 /// </summary>
 /// <returns></returns>
 public static IResponseMessageBase GetResponseEntity(XDocument doc)
 {
     ResponseMessageBase responseMessage = null;
     ResponseMsgType msgType;
     try
     {
         msgType = MsgTypeHelper.GetResponseMsgType(doc);
         switch (msgType)
         {
             case ResponseMsgType.Text:
                 responseMessage=new ResponseMessageText();
                 break;
             case ResponseMsgType.News:
                 responseMessage=new ResponseMessageNews();
                 break;
             case ResponseMsgType.Music:
                 responseMessage=new ResponseMessageMusic();
                 break;
             default:
                 throw new UnknownRequestMsgTypeException(string.Format("MsgType:{0} 在ResponseMessageFactory中没有对应的处理程序!", msgType), new ArgumentOutOfRangeException());
         }
         EntityHelper.FillEntityWithXml(responseMessage, doc);
     }
     catch (ArgumentException ex)
     {
         throw new WeixinException(string.Format("ResponseMessage转换出错!可能是MsgType不存在!,XML:{0}", doc.ToString()), ex);
     }
     return responseMessage;
 }
Пример #3
0
        private static ResponseMessageBase CreateFromRequestMessage(IRequestMessageBase requestMessage, ResponseMsgType msgType)
        {
            ResponseMessageBase responseMessage = null;

            try
            {
                switch (msgType)
                {
                case ResponseMsgType.Text:
                    responseMessage = new ResponseMessageText();
                    break;

                case ResponseMsgType.News:
                    responseMessage = new ResponseMessageNews();
                    break;

                case ResponseMsgType.Music:
                    responseMessage = new ResponseMessageMusic();
                    break;

                case ResponseMsgType.Image:
                    responseMessage = new ResponseMessageImage();
                    break;

                case ResponseMsgType.Voice:
                    responseMessage = new ResponseMessageVoice();
                    break;

                case ResponseMsgType.Video:
                    responseMessage = new ResponseMessageVideo();
                    break;

                case ResponseMsgType.Transfer_Customer_Service:
                    responseMessage = new ResponseMessageTransfer_Customer_Service();
                    break;

                case ResponseMsgType.NoResponse:
                    responseMessage = new ResponseMessageNoResponse();
                    break;

                default:
                    throw new UnknownRequestMsgTypeException(string.Format("ResponseMsgType没有为 {0} 提供对应处理程序。", msgType), new ArgumentOutOfRangeException());
                }

                responseMessage.ToUserName   = requestMessage.FromUserName;
                responseMessage.FromUserName = requestMessage.ToUserName;
                responseMessage.CreateTime   = DateTime.Now; //使用当前最新时间
            }
            catch (Exception ex)
            {
                throw new WeixinException("CreateFromRequestMessage过程发生异常", ex);
            }

            return(responseMessage);
        }
Пример #4
0
        /// <summary>
        /// 从返回结果XML转换成IResponseMessageBase实体类
        /// </summary>
        /// <param name="xml">返回给服务器的Response Xml</param>
        /// <returns></returns>
        public static IResponseMessageBase CreateFromResponseXml(string xml)
        {
            try
            {
                if (string.IsNullOrEmpty(xml))
                {
                    return(null);
                }

                var doc = XDocument.Parse(xml);
                ResponseMessageBase responseMessage = null;
                var msgType = (ResponseMsgType)Enum.Parse(typeof(ResponseMsgType), doc.Root.Element("MsgType").Value, true);
                switch (msgType)
                {
                case ResponseMsgType.Text:
                    responseMessage = new ResponseMessageText();
                    break;

                case ResponseMsgType.Image:
                    responseMessage = new ResponseMessageImage();
                    break;

                case ResponseMsgType.Voice:
                    responseMessage = new ResponseMessageVoice();
                    break;

                case ResponseMsgType.Video:
                    responseMessage = new ResponseMessageVideo();
                    break;

                case ResponseMsgType.Music:
                    responseMessage = new ResponseMessageMusic();
                    break;

                case ResponseMsgType.News:
                    responseMessage = new ResponseMessageNews();
                    break;

                case ResponseMsgType.Transfer_Customer_Service:
                    responseMessage = new ResponseMessageTransfer_Customer_Service();
                    break;
                }

                responseMessage.FillEntityWithXml(doc);
                return(responseMessage);
            }
            catch (Exception ex)
            {
                throw new WeixinException("ResponseMessageBase.CreateFromResponseXml<T>过程发生异常!" + ex.Message, ex);
            }
        }
Пример #5
0
        public static ResponseMessageBase CreateFromRequestMessage(IRequestMessageBase requestMessage, ResponseMsgType msgType)
        {
            ResponseMessageBase responseMessage = null;

            try
            {
                switch (msgType)
                {
                case ResponseMsgType.Text:
                    responseMessage = new ResponseMessageText()
                    {
                        ToUserName   = requestMessage.FromUserName,
                        FromUserName = requestMessage.ToUserName,
                        CreateTime   = DateTime.Now,                       //使用当前最新时间
                        //MsgType = msgType
                    };
                    break;

                case ResponseMsgType.News:
                    responseMessage = new ResponseMessageNews()
                    {
                        ToUserName   = requestMessage.FromUserName,
                        FromUserName = requestMessage.ToUserName,
                        CreateTime   = DateTime.Now,                         //使用当前最新时间
                        //MsgType = msgType
                    };
                    break;

                case ResponseMsgType.Music:
                    responseMessage = new ResponseMessageMusic()
                    {
                        ToUserName   = requestMessage.FromUserName,
                        FromUserName = requestMessage.ToUserName,
                        CreateTime   = DateTime.Now,                        //使用当前最新事件
                        //MsgType = msgType
                    };
                    break;

                default:
                    throw new UnknownRequestMsgTypeException(string.Format("ResponseMsgType没有为 {0} 提供对应处理程序。", msgType), new ArgumentOutOfRangeException());
                }
            }
            catch (Exception ex)
            {
                throw new WeixinException("CreateFromRequestMessage过程发生异常", ex);
            }

            return(responseMessage);
        }
        public static ResponseMessageBase CreateFromRequestMessage(IRequestMessageBase requestMessage, ResponseMsgType msgType)
        {
            ResponseMessageBase responseMessage = null;
            try
            {
                switch (msgType)
                {
                    case ResponseMsgType.Text:
                        responseMessage = new ResponseMessageText()
                                             {
                                                 ToUserName = requestMessage.FromUserName,
                                                 FromUserName = requestMessage.ToUserName,
                                                 CreateTime = DateTime.Now,//使用当前最新事件
                                                 MsgType = msgType
                                             };
                        break;
                    case ResponseMsgType.News:
                        responseMessage = new ResponseMessageNews()
                                               {
                                                   ToUserName = requestMessage.FromUserName,
                                                   FromUserName = requestMessage.ToUserName,
                                                   CreateTime = DateTime.Now,//使用当前最新事件
                                                   MsgType = msgType
                                               };
                        break; break;
                    case ResponseMsgType.Music:
                        responseMessage = new ResponseMessageMusic()
                                              {
                                                  ToUserName = requestMessage.FromUserName,
                                                  FromUserName = requestMessage.ToUserName,
                                                  CreateTime = DateTime.Now,//使用当前最新事件
                                                  MsgType = msgType
                                              };
                        break;
                    default:
                        throw new UnknownRequestMsgTypeException(string.Format("ResponseMsgType没有为 {0} 提供对应处理程序。", msgType), new ArgumentOutOfRangeException());
                }
            }
            catch (Exception ex)
            {
                throw new WeixinException("CreateFromRequestMessage过程发生异常", ex);
            }

            return responseMessage;
        }
        public void GetResponseEntityTest()
        {
            {
                //Text
                ResponseMessageText exceptResult = new ResponseMessageText()
                                              {
                                                  ToUserName = "******",
                                                  FromUserName = "******",
                                                  CreateTime = DateTimeHelper.GetDateTimeFromXml(63497820384),
                                                  //MsgType = ResponseMsgType.Text,
                                                  Content = "文字信息",
                                                  //FuncFlag = false
                                              };
                var result = ResponseMessageFactory.GetResponseEntity(xmlText) as ResponseMessageText;
                Assert.AreEqual(exceptResult.ToUserName, result.ToUserName);
                Assert.AreEqual(exceptResult.CreateTime, result.CreateTime);
                Assert.AreEqual(exceptResult.Content, result.Content);
            }

            {
                //Image
                ResponseMessageNews exceptResult = new ResponseMessageNews()
                                                     {
                                                         //Articles = new List<Article>(),
                                                         CreateTime = DateTimeHelper.GetDateTimeFromXml(63497821905),
                                                         FromUserName = "******",
                                                         ToUserName = "******",
                                                         //FuncFlag = false,
                                                         //MsgType = ResponseMsgType.News
                                                     };
                var result = ResponseMessageFactory.GetResponseEntity(xmlNews) as ResponseMessageNews;
                Assert.AreEqual(exceptResult.ToUserName, result.ToUserName);
                Assert.AreEqual(exceptResult.CreateTime, result.CreateTime);
                Assert.AreEqual(2, result.ArticleCount);
                Assert.AreEqual(result.Articles.Count, result.ArticleCount);
            }

            //TODO:测试语音和视频类型

            {
                //Music
                ResponseMessageMusic exceptResult = new ResponseMessageMusic()
                                                        {
                                                            Music = new Music()
                                                                        {
                                                                            Title = "标题",
                                                                            Description = "说明",
                                                                            MusicUrl = "http://weixin.senparc.com/Content/music1.mp3",
                                                                            HQMusicUrl = ""
                                                                        },
                                                            CreateTime = DateTimeHelper.GetDateTimeFromXml(63497823450),
                                                            FromUserName = "******",
                                                            ToUserName = "******",
                                                            //FuncFlag = false,
                                                            //MsgType = ResponseMsgType.Music
                                                        };
                var result = ResponseMessageFactory.GetResponseEntity(xmlMusic) as ResponseMessageMusic;
                Assert.AreEqual(exceptResult.ToUserName, result.ToUserName);
                Assert.AreEqual(exceptResult.CreateTime, result.CreateTime);
                //Assert.AreEqual(exceptResult.Music, result.Music);
            }
        }
Пример #8
0
        /// <summary>
        /// 测试用例
        /// </summary>
        /// <returns></returns>
        public ActionResult Text()
        {
            var userProfile =
                _userProfileRepository.Find(
                    Specification<EasyWeixin.Model.UserProfile>.Eval(
                        o => o.UserId == WebSecurity.GetUserId(User.Identity.Name)));
            var buttons = userProfile.Buttons;
            foreach (var button in buttons)
            {
                if (button.SubButtons == null)
                {
                    var responseMessages = button.ResponseMessages.ToList();
                    //返回类型Text (ButtonType == 0)
                    if (responseMessages.Where(o => o.ButtonType == 0).ToList().Count > 0)
                    {
                        var responseMessage = new ResponseMessageText();
                        responseMessage.Content = responseMessages.ElementAt(0).Content;
                    }

                    //返回类型Music (ButtonType == 1)
                    else if (responseMessages.Where(o => o.ButtonType == 1).ToList().Count > 0)
                    {
                        var responseMessage = new ResponseMessageMusic();
                        responseMessage.Music.Title = responseMessages.ElementAt(0).ResponseMusic.MusicName;
                        responseMessage.Music.HQMusicUrl = responseMessages.ElementAt(0).ResponseMusic.HQMusicUrl;
                        responseMessage.Music.MusicUrl = responseMessages.ElementAt(0).ResponseMusic.MusicUrl;
                        responseMessage.Music.Description = "";
                        //   return responseMessage;
                    }
                    //返回类型News (ButtonType == 4)
                    else if (responseMessages.Where(o => o.ButtonType == 4).ToList().Count > 0)
                    {
                        var responseMessage = new ResponseMessageNews();
                        foreach (var item in responseMessages)
                        {
                            responseMessage.Articles.Add(new Article()
                            {
                                //Description = HtmlStringHelper.ClearHTMLString(item.ResponseImageText.Content),
                                //Title = item.ResponseImageText.ImageTextName,
                                //PicUrl = GetImageUrl(item.ResponseImageText.Content),
                                //Url = item.ResponseImageText.Url
                            });
                        }
                    }
                }
                else
                {
                    //var essage = UserProfile.ResponseMessages.Where(o => o.ResponseType == 2).ToList();
                    //return GetEventsResponseMessages(essage, requestMessage);
                    foreach (var Subitem in button.SubButtons.ToList())
                    {
                        var ResponseMessages = Subitem.ResponseMessages.ToList();
                        //返回类型Text (ButtonType == 0)
                        if (ResponseMessages.Where(o => o.ButtonType == 0).ToList().Count > 0)
                        {
                            var responseMessage = new ResponseMessageText();
                            responseMessage.Content = ResponseMessages.ElementAt(0).Content;
                        }

                        //返回类型Music (ButtonType == 1)
                        else if (ResponseMessages.Where(o => o.ButtonType == 1).ToList().Count > 0)
                        {
                            var responseMessage = new ResponseMessageMusic();
                            responseMessage.Music.Title = ResponseMessages.ElementAt(0).ResponseMusic.MusicName;
                            responseMessage.Music.HQMusicUrl = ResponseMessages.ElementAt(0).ResponseMusic.HQMusicUrl;
                            responseMessage.Music.MusicUrl = ResponseMessages.ElementAt(0).ResponseMusic.MusicUrl;
                            responseMessage.Music.Description = "";
                            //   return responseMessage;
                        }
                        //返回类型News (ButtonType == 4)
                        else if (ResponseMessages.Where(o => o.ButtonType == 4).ToList().Count > 0)
                        {
                            var responseMessage = new ResponseMessageNews();
                            foreach (var item in ResponseMessages)
                            {
                                responseMessage.Articles.Add(new Article()
                                {
                                    //Description = HtmlStringHelper.ClearHTMLString(item.ResponseImageText.Content),
                                    //Title = item.ResponseImageText.ImageTextName,
                                    //PicUrl = GetImageUrl(item.ResponseImageText.Content),
                                    //Url = item.ResponseImageText.Url
                                });
                            }
                        }
                    }
                }
            }
            return View();
        }
Пример #9
0
 public ActionResult text()
 {
     var datas = _responseKeyRepository.FindAll().FirstOrDefault(o => o.Key == "250");
     if (datas != null && datas.ResponseKeyRules != null &&
         datas.ResponseKeyRules.ResponseMessages.ToList().Count > 0)
     {
         //返回类型Text
         if (datas.ResponseKeyRules.ResponseMessages.Where(o => o.ButtonType == 0).ToList().Count > 0)
         {
             var responseMessage = new ResponseMessageText();
             responseMessage.Content = datas.ResponseKeyRules.ResponseMessages.ElementAt(0).Content;
             return View();
         }
         else if (datas.ResponseKeyRules.ResponseMessages.Where(o => o.ButtonType == 1).ToList().Count > 0)
         {
             var responseMessage = new ResponseMessageMusic();
             responseMessage.Music.Title =
                 datas.ResponseKeyRules.ResponseMessages.ElementAt(0).ResponseMusic.MusicName;
             responseMessage.Music.HQMusicUrl =
                 datas.ResponseKeyRules.ResponseMessages.ElementAt(0).ResponseMusic.HQMusicUrl;
             responseMessage.Music.MusicUrl =
                 datas.ResponseKeyRules.ResponseMessages.ElementAt(0).ResponseMusic.MusicUrl;
             responseMessage.Music.Description = "";
             return View();
         }
         else if (datas.ResponseKeyRules.ResponseMessages.Where(o => o.ButtonType == 4).ToList().Count > 0)
         {
             var responseMessage = new ResponseMessageNews();
             foreach (var item in datas.ResponseKeyRules.ResponseMessages)
             {
                 responseMessage.Articles.Add(new Article()
                 {
                     Description = item.ResponseImageText.Content,
                     Title = item.ResponseImageText.ImageTextName,
                     PicUrl = "",
                     Url = "www.baidu.com"
                 });
             }
             return View();
         }
         else
         {
             var errorMessage = new ResponseMessageText();
             errorMessage.Content = "小妹纸不懂官人的意思,到我网址<a href=\"http://home.ipow.cn\">home.ipow.cn</a>看看吧1";
             return View();
         }
     }
     else
     {
         var errorMessage = new ResponseMessageText();
         errorMessage.Content = "小妹纸不懂官人的意思,到我网址<a href=\"http://home.ipow.cn\">home.ipow.cn</a>看看吧2";
         return View();
     }
 }