示例#1
0
        /// <summary>
        /// 将公众平台POST过来的数据转化成实体对象
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static ReceiveMessageBase ConvertMsgToObject(string token)
        {
            if (!CheckSignature(token))
            {
                return null;
            }
            string msgBody = string.Empty;
            Stream s = System.Web.HttpContext.Current.Request.InputStream;
            byte[] b = new byte[s.Length];
            s.Read(b, 0, (int)s.Length);
            msgBody = Encoding.UTF8.GetString(b);

            if (string.IsNullOrEmpty(msgBody))
            {
                return null;
            }

            XmlDocument doc = null;

            MsgType msgType = MsgType.UnKnown;
            EventType eventType = EventType.UnKnown;
            ReceiveMessageBase msg = new ReceiveMessageBase();
            msg.MsgType = msgType;
            msg.MessageBody = msgBody;
            XmlNode node = null;
            XmlNode tmpNode = null;

            try
            {

                doc = new XmlDocument();
                doc.LoadXml(msgBody);//读取XML字符串
                XmlElement rootElement = doc.DocumentElement;
                XmlNode msgTypeNode = rootElement.SelectSingleNode("MsgType");//获取字符串中的消息类型

                node = rootElement.SelectSingleNode("FromUserName");
                if (node != null)
                {
                    msg.FromUserName = node.InnerText;
                }
                node = rootElement.SelectSingleNode("ToUserName");
                if (node != null)
                {
                    msg.ToUserName = node.InnerText;
                }
                node = rootElement.SelectSingleNode("CreateTime");
                if (node != null)
                {
                    msg.CreateTime = Convert.ToInt64(node.InnerText);
                }

                #region 获取具体的消息对象

                string strMsgType = msgTypeNode.InnerText;
                string msgId = string.Empty;
                string content = string.Empty;

                tmpNode = rootElement.SelectSingleNode("MsgId");
                if (tmpNode != null)
                {
                    msgId = tmpNode.InnerText.Trim();
                }

                switch (strMsgType)
                {
                    case "text":
                        msgType = MsgType.Text;

                        tmpNode = rootElement.SelectSingleNode("Content");
                        if (tmpNode != null)
                        {
                            content = tmpNode.InnerText.Trim();
                        }
                        TextReceiveMessage txtMsg = new TextReceiveMessage()
                        {
                            CreateTime = msg.CreateTime,
                            FromUserName = msg.FromUserName,
                            MessageBody = msg.MessageBody,
                            MsgType = msgType,
                            ToUserName = msg.ToUserName,
                            MsgId = Convert.ToInt64(msgId),
                            Content = content
                        };

                        return txtMsg;
                    case "image":
                        msgType = MsgType.Image;

                        ImageReceiveMessage imgMsg = new ImageReceiveMessage()
                        {
                            CreateTime = msg.CreateTime,
                            FromUserName = msg.FromUserName,
                            MessageBody = msg.MessageBody,
                            MsgId = Convert.ToInt64(msgId),
                            MsgType = msgType,
                            ToUserName = msg.ToUserName,
                            MediaId = rootElement.SelectSingleNode("MediaId").InnerText,
                            PicUrl = rootElement.SelectSingleNode("PicUrl").InnerText
                        };

                        return imgMsg;
                    case "voice":
                        msgType = MsgType.Voice;
                        XmlNode node1 = rootElement.SelectSingleNode("Recognition");
                        if (node1 != null)
                        {
                            msgType = MsgType.VoiceResult;
                        }

                        VoiceReceiveMessage voiceMsg = new VoiceReceiveMessage()
                        {
                            CreateTime = msg.CreateTime,
                            FromUserName = msg.FromUserName,
                            ToUserName = msg.ToUserName,
                            MessageBody = msg.MessageBody,
                            MsgId = Convert.ToInt64(msgId),
                            MsgType = msgType,
                            Recognition = node1 == null ? string.Empty : node1.InnerText.Trim(),
                            Format = rootElement.SelectSingleNode("Format").InnerText,
                            MediaId = rootElement.SelectSingleNode("MediaId").InnerText
                        };

                        return voiceMsg;

                    case "video":
                        msgType = MsgType.Video;

                        VideoReceiveMessage videoMsg = new VideoReceiveMessage()
                        {
                            CreateTime = msg.CreateTime,
                            FromUserName = msg.FromUserName,
                            MediaId = rootElement.SelectSingleNode("MediaId").InnerText,
                            MessageBody = msg.MessageBody,
                            MsgId = Convert.ToInt64(msgId),
                            MsgType = msgType,
                            ToUserName = msg.ToUserName,
                            ThumbMediaId = rootElement.SelectSingleNode("ThumbMediaId").InnerText
                        };

                        return videoMsg;
                    case "location":
                        msgType = MsgType.Location;

                        LocationReceiveMessage locationMsg = new LocationReceiveMessage()
                        {
                            CreateTime = msg.CreateTime,
                            FromUserName = msg.FromUserName,
                            MessageBody = msg.MessageBody,
                            MsgId = Convert.ToInt64(msgId),
                            MsgType = msgType,
                            ToUserName = msg.ToUserName,
                            Label = rootElement.SelectSingleNode("Label").InnerText,
                            Location_X = rootElement.SelectSingleNode("Location_X").InnerText,
                            Location_Y = rootElement.SelectSingleNode("Location_Y ").InnerText,
                            Scale = rootElement.SelectSingleNode("Scale").InnerText
                        };

                        return locationMsg;
                    case "link":
                        msgType = MsgType.Link;

                        LinkReceiveMessage linkMsg = new LinkReceiveMessage()
                        {
                            CreateTime = msg.CreateTime,
                            Description = rootElement.SelectSingleNode("Description").InnerText,
                            FromUserName = msg.FromUserName,
                            MessageBody = msg.MessageBody,
                            MsgId = Convert.ToInt64(msgId),
                            MsgType = msgType,
                            Title = rootElement.SelectSingleNode("Title").InnerText,
                            ToUserName = msg.ToUserName,
                            Url = rootElement.SelectSingleNode("Url ").InnerText
                        };

                        return linkMsg;
                    case "event":
                        msgType = MsgType.Event;
                        eventType = EventType.UnKnown;
                        msg.MsgType = msgType;

                        XmlNode eventNode = rootElement.SelectSingleNode("Event");
                        if (eventNode != null)
                        {

                             switch (eventNode.InnerText)
                            {
                                case "subscribe":
                                    eventType = EventType.Subscribe;
                                    tmpNode = rootElement.SelectSingleNode("EventKey");
                                    var tmp = rootElement.SelectSingleNode("Ticket");
                                    if (tmpNode != null && !string.IsNullOrEmpty(tmpNode.InnerText))  //&& tmpNode.InnerText.StartsWith("qrscene_")
                                    {
                                        //扫描二维码关注事件
                                        ScanSubscribeEventMessage scanSubEvt = new ScanSubscribeEventMessage()
                                        {
                                            CreateTime = msg.CreateTime,
                                            EventKey = rootElement.SelectSingleNode("EventKey").InnerText,
                                            EventType = EventType.Subscribe,
                                            FromUserName = msg.FromUserName,
                                            MessageBody = msg.MessageBody,
                                            MsgType = msgType,
                                            ToUserName = msg.ToUserName,
                                            Ticket = rootElement.SelectSingleNode("Ticket").InnerText
                                        };
                                        return scanSubEvt;
                                    }
                                    else
                                    {
                                        //普通关注事件
                                        SubscribeEventMessage subEvt = new SubscribeEventMessage()
                                        {
                                            CreateTime = msg.CreateTime,
                                            EventType = EventType.Subscribe,
                                            FromUserName = msg.FromUserName,
                                            MessageBody = msg.MessageBody,
                                            MsgType = msgType,
                                            ToUserName = msg.ToUserName
                                        };
                                        return subEvt;
                                    }
                                case "unsubscribe":
                                    eventType = EventType.UnSubscribe;

                                    UnSubscribeEventMessage unSubEvt = new UnSubscribeEventMessage()
                                    {
                                        CreateTime = msg.CreateTime,
                                        EventType = eventType,
                                        FromUserName = msg.FromUserName,
                                        MessageBody = msg.MessageBody,
                                        MsgType = msgType,
                                        ToUserName = msg.ToUserName
                                    };

                                    return unSubEvt;
                                case "scan":
                                    eventType = EventType.Scan;

                                    ScanEventMessage scanEvt = new ScanEventMessage()
                                    {
                                        CreateTime = msg.CreateTime,
                                        EventKey = rootElement.SelectSingleNode("EventKey").InnerText,
                                        EventType = eventType,
                                        FromUserName = msg.FromUserName,
                                        MessageBody = msg.MessageBody,
                                        MsgType = msgType,
                                        Ticket = rootElement.SelectSingleNode("Ticket").InnerText,
                                        ToUserName = msg.ToUserName
                                    };

                                    return scanEvt;
                                case "LOCATION":
                                    eventType = EventType.Location;

                                    UploadLocationEventMessage locationEvt = new UploadLocationEventMessage()
                                    {
                                        CreateTime = msg.CreateTime,
                                        EventType = eventType,
                                        FromUserName = msg.FromUserName,
                                        Latitude = rootElement.SelectSingleNode("Latitude").InnerText,
                                        Longitude = rootElement.SelectSingleNode("Longitude").InnerText,
                                        MessageBody = msg.MessageBody,
                                        MsgType = msgType,
                                        Precision = rootElement.SelectSingleNode("Precision").InnerText,
                                        ToUserName = msg.ToUserName
                                    };

                                    return locationEvt;
                                case "CLICK":
                                    eventType = EventType.Click;

                                    MenuEventMessage menuEvt = new MenuEventMessage()
                                    {
                                        CreateTime = msg.CreateTime,
                                        EventKey = rootElement.SelectSingleNode("EventKey").InnerText,
                                        EventType = eventType,
                                        FromUserName = msg.FromUserName,
                                        MessageBody = msg.MessageBody,
                                        MsgType = msgType,
                                        ToUserName = msg.ToUserName
                                    };

                                    return menuEvt;
                                default:
                                    EventMessage evtMsg = new EventMessage()
                                    {
                                        CreateTime = msg.CreateTime,
                                        EventType = EventType.UnKnown,
                                        FromUserName = msg.FromUserName,
                                        MessageBody = msg.MessageBody,
                                        MsgType = MsgType.Event,
                                        ToUserName = msg.ToUserName
                                    };
                                    return evtMsg;
                            }
                        }

                        break;
                }
                msg.MsgType = msgType;
                #endregion
            }
            finally
            {
                if (doc != null)
                {
                    doc = null;
                }
            }

            msg.MsgType = msgType;
            return msg;
        }
示例#2
0
文件: WinXin.cs 项目: uwitec/O2O
 /// <summary>
 /// 处理上报地理位置事件
 /// </summary>
 /// <param name="msg"></param>
 /// <param name="args"></param>
 /// <returns></returns>
 public bool ProcessUploadLocationEvent(UploadLocationEventMessage msg, params object[] args)
 {
     //这里回应1条文本消息,当然您也可以回应其他消息
     MessageHandler.SendTextReplyMessage(msg.ToUserName, msg.FromUserName, "您的地里位置为:" + msg.Latitude + "-" + msg.Longitude);
     return true;
 }