Beispiel #1
0
        /// <summary>
        /// 处理取消关注事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public bool ProcessUnSubscribeEvent(UnSubscribeEventMessage msg, params object[] args)
        {
            new Eyousoft_yhq.BLL.BWeiXin().GuanZhu(msg.FromUserName, "0");

            //这里回应1条文本消息,当然您也可以回应其他消息
            //MessageHandler.SendTextReplyMessage(msg.ToUserName, msg.FromUserName, "您触发了取消关注事件,欢迎您下次再光临哦");
            EyouSoft.Common.Page.HuiyuanPage.Logout();
            return(true);
        }
Beispiel #2
0
        ///

        /// 处理取消关注事件
        ///

        /// 事件消息
        /// 参数(用于具体业务传递参数用)
        /// 是否处理成功
        public bool ProcessUnSubscribeEvent(UnSubscribeEventMessage msg, params object[] args)
        {
            try
            {
                Post(null, new Dictionary <string, string>()
                {
                    { "visit", "unsubscribwexinuser" },
                    { "OpenID", msg.FromUserName },
                });
            }
            catch (Exception ex)
            {
                LogHelper.WriteError(LogModule, "记录访问日志失败", ex);
            }
            return(true);
        }
Beispiel #3
0
 /// <summary>
 /// Event事件类型请求之unsubscribe
 /// </summary>
 public virtual Task <IResponseMessage> OnEvent_UnsubscribeRequest(UnSubscribeEventMessage requestMessage)
 {
     return(Task.FromResult <IResponseMessage>(null));
 }
Beispiel #4
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.IsNullOrWhiteSpace(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");
                            if (tmpNode != null)          // && (!string.IsNullOrWhiteSpace(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);
        }
        /// <summary>
        /// 将公众平台POST过来的数据转化成实体对象
        /// </summary>
        /// <param name="token">用户在公众平台填写的token</param>
        /// <returns>消息处理的基类</returns>
        public static ReceiveMessageBase ConvertMsgToObject(string token)
        {
            if (!CheckSignature(token))
            {
                return(null);
            }
            //获取传入的HTTP消息
            var s = HttpContext.Current.Request.InputStream;
            var b = new byte[s.Length];

            s.Read(b, 0, (int)s.Length);
            var msgBody = Encoding.UTF8.GetString(b);

            if (string.IsNullOrWhiteSpace(msgBody))
            {
                return(null);
            }
            var msgType = MsgType.UnKnown;
            //获取消息主体和设置消息类型枚举
            var msg = new ReceiveMessageBase
            {
                MsgType     = msgType,
                MessageBody = msgBody
            };

            try
            {
                //读取XML字符串,返回消息为xml文档类型
                var doc = new XmlDocument();
                doc.LoadXml(msgBody);
                //获取xml根节点
                var rootElement = doc.DocumentElement;
                if (rootElement != null)
                {
                    //获取字符串中的消息类型,搜索xml消息节点信息
                    var msgTypeNode = rootElement.SelectSingleNode("MsgType");
                    var 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);
                    }
                    //根据获取传入的消息主体数据,判断消息类型,获取具体的消息对象
                    if (msgTypeNode != null)
                    {
                        var strMsgType = msgTypeNode.InnerText;
                        var msgId      = string.Empty;
                        var content    = string.Empty;
                        var 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();
                            }
                            var 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 = null;
                            var selectSingleNode       = rootElement.SelectSingleNode("MediaId");
                            if (selectSingleNode != null)
                            {
                                var singleNode = rootElement.SelectSingleNode("PicUrl");
                                if (singleNode != null)
                                {
                                    imgMsg = new ImageReceiveMessage()
                                    {
                                        CreateTime   = msg.CreateTime,
                                        FromUserName = msg.FromUserName,
                                        MessageBody  = msg.MessageBody,
                                        MsgId        = Convert.ToInt64(msgId),
                                        MsgType      = msgType,
                                        ToUserName   = msg.ToUserName,
                                        MediaId      = selectSingleNode.InnerText,
                                        PicUrl       = singleNode.InnerText
                                    }
                                }
                                ;
                            }
                            return(imgMsg);

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

                            return(voiceMsg);

                        case "video":
                            msgType = MsgType.Video;
                            VideoReceiveMessage videoMsg = null;
                            var singleNode1 = rootElement.SelectSingleNode("MediaId");
                            if (singleNode1 != null)
                            {
                                var singleNode = rootElement.SelectSingleNode("ThumbMediaId");
                                if (singleNode != null)
                                {
                                    videoMsg = new VideoReceiveMessage()
                                    {
                                        CreateTime   = msg.CreateTime,
                                        FromUserName = msg.FromUserName,
                                        MediaId      = singleNode1.InnerText,
                                        MessageBody  = msg.MessageBody,
                                        MsgId        = Convert.ToInt64(msgId),
                                        MsgType      = msgType,
                                        ToUserName   = msg.ToUserName,
                                        ThumbMediaId = singleNode.InnerText
                                    };
                                }
                            }
                            return(videoMsg);

                        case "location":
                            msgType = MsgType.Location;
                            LocationReceiveMessage locationMsg = null;
                            var xmlNode1 = rootElement.SelectSingleNode("Label");
                            if (xmlNode1 != null)
                            {
                                var selectSingleNode2 = rootElement.SelectSingleNode("Location_X");
                                if (selectSingleNode2 == null)
                                {
                                    return(null);
                                }
                                var singleNode = rootElement.SelectSingleNode("Location_Y ");
                                if (singleNode == null)
                                {
                                    return(null);
                                }
                                var selectSingleNode1 = rootElement.SelectSingleNode("Scale");
                                if (selectSingleNode1 != null)
                                {
                                    locationMsg = new LocationReceiveMessage()
                                    {
                                        CreateTime   = msg.CreateTime,
                                        FromUserName = msg.FromUserName,
                                        MessageBody  = msg.MessageBody,
                                        MsgId        = Convert.ToInt64(msgId),
                                        MsgType      = msgType,
                                        ToUserName   = msg.ToUserName,
                                        Label        = xmlNode1.InnerText,
                                        LocationX    = selectSingleNode2.InnerText,
                                        LocationY    = singleNode.InnerText,
                                        Scale        = selectSingleNode1.InnerText
                                    }
                                }
                                ;
                            }
                            return(locationMsg);

                        case "link":
                            msgType = MsgType.Link;
                            LinkReceiveMessage linkMsg = null;
                            var singleNode2            = rootElement.SelectSingleNode("Description");
                            if (singleNode2 != null)
                            {
                                var xmlNode2 = rootElement.SelectSingleNode("Title");
                                if (xmlNode2 == null)
                                {
                                    return((LinkReceiveMessage)null);
                                }
                                var singleNode = rootElement.SelectSingleNode("Url ");
                                if (singleNode != null)
                                {
                                    linkMsg = new LinkReceiveMessage()
                                    {
                                        CreateTime   = msg.CreateTime,
                                        Description  = singleNode2.InnerText,
                                        FromUserName = msg.FromUserName,
                                        MessageBody  = msg.MessageBody,
                                        MsgId        = Convert.ToInt64(msgId),
                                        MsgType      = msgType,
                                        Title        = xmlNode2.InnerText,
                                        ToUserName   = msg.ToUserName,
                                        Url          = singleNode.InnerText
                                    }
                                }
                                ;
                            }
                            return(linkMsg);

                        case "event":
                            msgType     = MsgType.Event;
                            msg.MsgType = msgType;
                            ScanSubscribeEventMessage scanSubEvt = null;
                            var eventNode = rootElement.SelectSingleNode("Event");
                            if (eventNode != null)
                            {
                                EventType eventType;
                                switch (eventNode.InnerText)
                                {
                                case "subscribe":
                                    tmpNode = rootElement.SelectSingleNode("EventKey");
                                    if (tmpNode != null)
                                    // && (!string.IsNullOrWhiteSpace(tmpNode.InnerText)))  //&& tmpNode.InnerText.StartsWith("qrscene_")
                                    {
                                        //扫描二维码关注事件
                                        var singleNode = rootElement.SelectSingleNode("EventKey");
                                        if (singleNode == null)
                                        {
                                            return(null);
                                        }
                                        var selectSingleNode1 = rootElement.SelectSingleNode("Ticket");
                                        if (selectSingleNode1 != null)
                                        {
                                            scanSubEvt = new ScanSubscribeEventMessage()
                                            {
                                                CreateTime   = msg.CreateTime,
                                                EventKey     = singleNode.InnerText,
                                                EventType    = EventType.Subscribe,
                                                FromUserName = msg.FromUserName,
                                                MessageBody  = msg.MessageBody,
                                                MsgType      = msgType,
                                                ToUserName   = msg.ToUserName,
                                                Ticket       = selectSingleNode1.InnerText
                                            }
                                        }
                                        ;
                                        return(scanSubEvt);
                                    }
                                    else
                                    {
                                        //普通关注事件
                                        var 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;
                                    var 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 = null;
                                    var selectSingleNode2    = rootElement.SelectSingleNode("EventKey");
                                    if (selectSingleNode2 != null)
                                    {
                                        var xmlNode2 = rootElement.SelectSingleNode("Ticket");
                                        if (xmlNode2 != null)
                                        {
                                            scanEvt = new ScanEventMessage()
                                            {
                                                CreateTime   = msg.CreateTime,
                                                EventKey     = selectSingleNode2.InnerText,
                                                EventType    = eventType,
                                                FromUserName = msg.FromUserName,
                                                MessageBody  = msg.MessageBody,
                                                MsgType      = msgType,
                                                Ticket       = xmlNode2.InnerText,
                                                ToUserName   = msg.ToUserName
                                            }
                                        }
                                        ;
                                    }
                                    return(scanEvt);

                                case "LOCATION":
                                    eventType = EventType.Location;
                                    UploadLocationEventMessage locationEvt = null;
                                    var node2 = rootElement.SelectSingleNode("Latitude");
                                    if (node2 != null)
                                    {
                                        var selectSingleNode3 = rootElement.SelectSingleNode("Longitude");
                                        if (selectSingleNode3 == null)
                                        {
                                            return(null);
                                        }
                                        var singleNode = rootElement.SelectSingleNode("Precision");
                                        if (singleNode != null)
                                        {
                                            locationEvt = new UploadLocationEventMessage()
                                            {
                                                CreateTime   = msg.CreateTime,
                                                EventType    = eventType,
                                                FromUserName = msg.FromUserName,
                                                Latitude     = node2.InnerText,
                                                Longitude    = selectSingleNode3.InnerText,
                                                MessageBody  = msg.MessageBody,
                                                MsgType      = msgType,
                                                Precision    = singleNode.InnerText,
                                                ToUserName   = msg.ToUserName
                                            }
                                        }
                                        ;
                                    }
                                    return(locationEvt);

                                case "CLICK":
                                    eventType = EventType.Click;
                                    MenuEventMessage menuEvt = null;
                                    var singleNode3          = rootElement.SelectSingleNode("EventKey");
                                    if (singleNode3 != null)
                                    {
                                        menuEvt = new MenuEventMessage()
                                        {
                                            CreateTime   = msg.CreateTime,
                                            EventKey     = singleNode3.InnerText,
                                            EventType    = eventType,
                                            FromUserName = msg.FromUserName,
                                            MessageBody  = msg.MessageBody,
                                            MsgType      = msgType,
                                            ToUserName   = msg.ToUserName
                                        }
                                    }
                                    ;
                                    return(menuEvt);

                                default:
                                    var 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;
            }
            finally
            {
                msg.MsgType = msgType;
            }
            return(msg);
        }
Beispiel #6
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;
        }
Beispiel #7
0
        /// <summary>
        /// 处理取消关注事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public bool ProcessUnSubscribeEvent(UnSubscribeEventMessage msg, params object[] args)
        {
            new Eyousoft_yhq.BLL.BWeiXin().GuanZhu(msg.FromUserName, "0");

            //这里回应1条文本消息,当然您也可以回应其他消息
            //MessageHandler.SendTextReplyMessage(msg.ToUserName, msg.FromUserName, "您触发了取消关注事件,欢迎您下次再光临哦");
            EyouSoft.Common.Page.HuiyuanPage.Logout();
            return true;
        }
        /// <summary>
        /// 将公众平台POST过来的数据转化成实体对象
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static ReceiveMessageBase ConvertMsgToObject(string token)
        {
            //if (!CheckSignature(token))
            //{
            //    return null;
            //}
            try
            {
                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);
                //Utility.LogHelper.WriteDebug("Foresight.Winxin_消息内容", msgBody);
                if (WebUtils.IsNullOrWhiteSpace(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.ToLower())
                            {
                            case "subscribe":
                            {
                                eventType = EventType.Subscribe;
                                var node_EventKey = rootElement.SelectSingleNode("EventKey");
                                var node_Ticket   = rootElement.SelectSingleNode("Ticket");
                                //普通关注事件
                                SubscribeEventMessage subEvt = new SubscribeEventMessage()
                                {
                                    CreateTime   = msg.CreateTime,
                                    EventType    = EventType.Subscribe,
                                    FromUserName = msg.FromUserName,
                                    MessageBody  = msg.MessageBody,
                                    MsgType      = msgType,
                                    ToUserName   = msg.ToUserName,
                                    EventKey     = (node_EventKey != null && !string.IsNullOrEmpty(node_EventKey.InnerText) && node_EventKey.InnerText.StartsWith("qrscene_")) ? node_EventKey.InnerText : null,
                                    Ticket       = (node_Ticket != null && !string.IsNullOrEmpty(node_Ticket.InnerText) && node_Ticket.InnerText.StartsWith("qrscene_")) ? node_Ticket.InnerText : null,
                                };
                                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;
                                var node_Ticket = rootElement.SelectSingleNode("Ticket");

                                ScanEventMessage scanEvt = new ScanEventMessage()
                                {
                                    CreateTime   = msg.CreateTime,
                                    EventKey     = rootElement.SelectSingleNode("EventKey").InnerText,
                                    EventType    = eventType,
                                    FromUserName = msg.FromUserName,
                                    MessageBody  = msg.MessageBody,
                                    MsgType      = msgType,
                                    Ticket       = (node_Ticket != null && !string.IsNullOrEmpty(node_Ticket.InnerText) && node_Ticket.InnerText.StartsWith("qrscene_")) ? node_Ticket.InnerText : null,
                                    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);
                            }

                            case "wificonnected":
                            {
                                eventType = EventType.WifiConnected;

                                WifiConnectedEventMessage menuEvt = new WifiConnectedEventMessage()
                                {
                                    CreateTime   = msg.CreateTime,
                                    EventType    = eventType,
                                    FromUserName = msg.FromUserName,
                                    MessageBody  = msg.MessageBody,
                                    MsgType      = msgType,
                                    ToUserName   = msg.ToUserName,
                                    ConnectTime  = Convert.ToInt32(rootElement.SelectSingleNode("ConnectTime").InnerText),
                                    ExpireTime   = Convert.ToInt32(rootElement.SelectSingleNode("ExpireTime").InnerText),
                                    VendorId     = rootElement.SelectSingleNode("VendorId").InnerText,
                                    ShopId       = rootElement.SelectSingleNode("ShopId").InnerText,
                                    DeviceNo     = rootElement.SelectSingleNode("DeviceNo").InnerText,
                                };

                                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);
            }
            catch (Exception ex)
            {
                Utility.LogHelper.WriteError("Foresight.Winxin", "转化消息出错", ex);
                return(null);
            }
        }
 /// <summary>
 /// Event事件类型请求之unsubscribe
 /// </summary>
 public virtual IResponseMessage OnEvent_UnsubscribeRequest(UnSubscribeEventMessage requestMessage)
 {
     return(null);
 }