PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/Senparc.Weixin.MP.Sample/Senparc.Weixin.MP.Sample/Controllers/WeixinController_OldPost.cs

https://github.com/mjhuangzk/WeiXinMPSDK
C# | 136 lines | 117 code | 6 blank | 13 comment | 4 complexity | 0de22d7e46c66d5b0565bfe9d1e19609 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.IO;
  4using System.Linq;
  5using System.Text;
  6using System.Web;
  7using System.Web.Mvc;
  8using System.Xml.Linq;
  9using Senparc.Weixin.MP.MessageHandlers;
 10using Senparc.Weixin.MP.Sample.CommonService;
 11
 12namespace Senparc.Weixin.MP.Sample.Controllers
 13{
 14    using Senparc.Weixin.MP.Entities;
 15    using Senparc.Weixin.MP.Helpers;
 16    //using Senparc.Weixin.MP.Sample.Service;
 17    //using Senparc.Weixin.MP.Sample.CustomerMessageHandler;
 18
 19    public partial class WeixinController : Controller
 20    {
 21        /// <summary>
 22        /// 用户发送消息后,微信平台自动Post一个请求到这里,并等待响应XML
 23        /// PS:此方法为常规switch判断方法,从v0.3.3版本起,此Demo不再更新
 24        /// </summary>
 25        [HttpPost]
 26        [ActionName("OldIndex")]
 27        public ActionResult OldPost(string signature, string timestamp, string nonce, string echostr)
 28        {
 29            LocationService locationService=new LocationService();
 30            EventService eventService= new EventService();
 31
 32            if (!CheckSignature.Check(signature, timestamp, nonce, Token))
 33            {
 34                return Content("参数错误!");
 35            }
 36            XDocument requestDoc = null;
 37            try
 38            {
 39                requestDoc = XDocument.Load(Request.InputStream);
 40
 41                var requestMessage = RequestMessageFactory.GetRequestEntity(requestDoc);
 42                //如果不需要记录requestDoc,只需要:
 43                //var requestMessage = RequestMessageFactory.GetRequestEntity(Request.InputStream);
 44
 45                requestDoc.Save(Server.MapPath("~/App_Data/" + DateTime.Now.Ticks + "_Request_" + requestMessage.FromUserName + ".txt"));//测试时可开启,帮助跟踪数据
 46                ResponseMessageBase responseMessage = null;
 47                switch (requestMessage.MsgType)
 48                {
 49                    case RequestMsgType.Text://文字
 50                        {
 51                            //TODO:交给Service处理具体信息,参考/Service/EventSercice.cs 及 /Service/LocationSercice.cs
 52                            var strongRequestMessage = requestMessage as RequestMessageText;
 53                            var strongresponseMessage =
 54                                ResponseMessageBase.CreateFromRequestMessage(requestMessage, ResponseMsgType.Text) as
 55                                ResponseMessageText;
 56                            strongresponseMessage.Content =
 57                                string.Format(
 58                                    "您刚才发送了文字信息:{0}\r\n您还可以发送【位置】【图片】【语音】等类型的信息,查看不同格式的回复。\r\nSDK官方地址:http://weixin.senparc.com",
 59                                    strongRequestMessage.Content);
 60                            responseMessage = strongresponseMessage;
 61                            break;
 62                        }
 63                    case RequestMsgType.Location://位置
 64                        {
 65                            responseMessage = locationService.GetResponseMessage(requestMessage as RequestMessageLocation);
 66                            break;
 67                        }
 68                    case RequestMsgType.Image://图片
 69                        {
 70                            //TODO:交给Service处理具体信息
 71                            var strongRequestMessage = requestMessage as RequestMessageImage;
 72                            var strongresponseMessage =
 73                                ResponseMessageBase.CreateFromRequestMessage(requestMessage, ResponseMsgType.News) as
 74                                ResponseMessageNews;
 75                            strongresponseMessage.Articles.Add(new Article()
 76                                                                   {
 77                                                                       Title = "您刚才发送了图片信息",
 78                                                                       Description = "您发送的图片将会显示在边上",
 79                                                                       PicUrl = strongRequestMessage.PicUrl,
 80                                                                       Url = "http://weixin.senparc.com"
 81                                                                   });
 82                            strongresponseMessage.Articles.Add(new Article()
 83                                                                   {
 84                                                                       Title = "第二条",
 85                                                                       Description = "第二条带连接的内容",
 86                                                                       PicUrl = strongRequestMessage.PicUrl,
 87                                                                       Url = "http://weixin.senparc.com"
 88                                                                   });
 89                            responseMessage = strongresponseMessage;
 90                            break;
 91                        }
 92                    case RequestMsgType.Voice://语音
 93                        {
 94                            //TODO:交给Service处理具体信息
 95                            var strongRequestMessage = requestMessage as RequestMessageVoice;
 96                            var strongresponseMessage =
 97                               ResponseMessageBase.CreateFromRequestMessage(requestMessage, ResponseMsgType.Music) as
 98                               ResponseMessageMusic;
 99                            strongresponseMessage.Music.MusicUrl = "http://weixin.senparc.com/Content/music1.mp3";
100                            responseMessage = strongresponseMessage;
101                            break;
102                        }
103                    case RequestMsgType.Event://事件
104                        {
105                            responseMessage = eventService.GetResponseMessage(requestMessage as RequestMessageEventBase);
106                            break;
107                        }
108                    default:
109                        throw new ArgumentOutOfRangeException();
110                }
111                var responseDoc = MP.Helpers.EntityHelper.ConvertEntityToXml(responseMessage);
112                responseDoc.Save(Server.MapPath("~/App_Data/" + DateTime.Now.Ticks + "_Response_" + responseMessage.ToUserName + ".txt"));//测试时可开启,帮助跟踪数据
113
114                return Content(responseDoc.ToString());
115                //如果不需要记录responseDoc,只需要:
116                //return Content(responseMessage.ConvertEntityToXmlString());
117            }
118            catch (Exception ex)
119            {
120                using (
121                    TextWriter tw = new StreamWriter(Server.MapPath("~/App_Data/Error_" + DateTime.Now.Ticks + ".txt")))
122                {
123                    tw.WriteLine(ex.Message);
124                    tw.WriteLine(ex.InnerException.Message);
125                    if (requestDoc != null)
126                    {
127                        tw.WriteLine(requestDoc.ToString());
128                    }
129                    tw.Flush();
130                    tw.Close();
131                }
132                return Content("");
133            }
134        }
135    }
136}