コード例 #1
0
        //public HttpResponseBase Response { get; }
        // //public WeChatApiDispatch()
        // {
        // }
        /// <summary>
        /// 微信信息xml格式
        ///<xml>
        ///<ToUserName><![CDATA[toUser]]></ToUserName>
        ///<FromUserName><![CDATA[fromUser]]></FromUserName>
        ///<CreateTime>1348831860</CreateTime>
        ///<MsgType><![CDATA[image]]></MsgType>
        ///<PicUrl><![CDATA[this is a url]]></PicUrl>
        ///<MediaId><![CDATA[media_id]]></MediaId>
        ///<MsgId>1234567890123456</MsgId>
        ///</xml>
        /// </summary>
        /// <param name="postStr"></param>
        /// <returns></returns>
        public string Execute(string postStr)
        {
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(postStr);

            XmlElement rootElement = doc.DocumentElement;

            XmlNode MsgType = rootElement.SelectSingleNode("MsgType");

            //RequestXML requestXML = new RequestXML();
            string ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText;

            string FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText;

            string CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText;

            string msgType = MsgType.InnerText;

            string resxml = string.Empty;

            string replyMsg = string.Empty;

            string defaultMsg = string.Empty;

            UserDal u3 = new UserDal();
            //u3.Temp("全局XML数据:"+postStr);
                switch (msgType.ToLower())
                {
                    case "event":
                        #region 事件消息
                        string eventType = rootElement.SelectSingleNode("Event").InnerText;
                        if (!string.IsNullOrEmpty(eventType))
                        {
                            UserBll ubll = new UserBll();
                            if (eventType == "subscribe")
                            {
                                //ubll.AddOpenId(FromUserName);
                                replyMsg = @"世界那么大,我想去看看。钱包那么小,哪都去不了。既然如此,那就干脆来份水果,对自己好一点吧~ 新鲜水果,0元起送,30分钟极速送达,记得给老板一个赞哦~   果然快,果然!快!好东西一定要跟身边的小伙伴分享哦 。
            一大拨活动即将到达战场··· 敬请关注。";
                            }
                            else if (eventType == "unsubscribe")
                            {
                                //ubll.RemoveOpenId(FromUserName);//取消关注删除用户
                            }
                        }
                        if (eventType.ToUpper() == "LOCATION")
                        {
                            //<Latitude>23.137466</Latitude>
                            //<Longitude>113.352425</Longitude>
                            //<Precision>119.385040</Precision>
                            double Latitude =0;
                            double Longitude =0;
                            UserBll ubll = new UserBll();
                            try
                            {
                                Latitude = rootElement.SelectSingleNode("Latitude").InnerText.ToDouble();
                                Longitude = rootElement.SelectSingleNode("Longitude").InnerText.ToDouble();
                                if (!ubll.FindOpenId(FromUserName))
                                {
                                    ubll.AddOpenId(FromUserName);
                                }
                                if (ubll.SaveLocation(Longitude, Latitude, FromUserName))
                                {

                                }

                            }
                            catch (Exception e)
                            {

                            }
                        }

                        #endregion
                        break;
                    case "view":
                        break;
                    case "text":
                        string content = rootElement.SelectSingleNode("Content").InnerText;
                        replyMsg = "对不起,您先稍等一下,小果果马上就来了,么么哒";

                        //文字消息
                        break;
                    default:
                        replyMsg = "default" + msgType;
                        break;
                }

            #region 其他类型消息

            //                //无匹配消息
            //                resxml = @"<xml>
            //                                <ToUserName><![CDATA[" + FromUserName + @"]]></ToUserName>
            //                                <FromUserName><![CDATA[" + ToUserName + @"]]></FromUserName>
            //                                <CreateTime>" + DateTime.Now.DateTimeToInt() + @"</CreateTime>
            //                                <MsgType><![CDATA[text]]></MsgType>
            //                                <Content><![CDATA[" + defaultMsg + @"]></Content>
            //                                </xml>";
            //                //图文消息
            //                resxml = @"<xml>
            //                                    <ToUserName><![CDATA[" + FromUserName + @"]]></ToUserName>
            //                                    <FromUserName><![CDATA[" + ToUserName + @"]]></FromUserName>
            //                                    <CreateTime>" + DateTime.Now.DateTimeToInt() + @"</CreateTime>
            //                                    <MsgType><![CDATA[news]]></MsgType>
            //                                    <ArticleCount>1</ArticleCount>
            //                                    <Articles>
            //                                            <item>
            //                                                <Title><![CDATA[" + title + @"]]></Title>
            //                                                <Description><![CDATA[" + msg + @"]]></Description>
            //                                                <PicUrl><![CDATA[" + imgUrl + @"]]></PicUrl>
            //                                                <Url><![CDATA[" + url + @"]]></Url>
            //                                            </item>
            //                                    </Articles>
            //                                    </xml>";

            //                //关注后消息
            //                resxml = @"<xml>
            //                            <ToUserName><![CDATA[" + FromUserName + @"]]></ToUserName>
            //                            <FromUserName><![CDATA[" + ToUserName + @"]]></FromUserName>
            //                            <CreateTime>" + DateTime.Now.DateTimeToInt() + @"</CreateTime>
            //                            <MsgType><![CDATA[text]]></MsgType>
            //                            <Content><![CDATA[欢迎关注哦]]></Content>
            //                            </xml>";
            #endregion
            msgType = "222";
            resxml = @"<xml>
                                <ToUserName>
                                        <![CDATA[" + FromUserName + @"]]>
                                </ToUserName>
                                <FromUserName>
                                        <![CDATA[" + ToUserName + @"]]>
                                </FromUserName>
                                <CreateTime>
                                        " + DateTime.Now.DateTimeToInt() + @"
                                </CreateTime>
                                <MsgType>
                                        <![CDATA[text]]>
                                </MsgType>
                                <Content>
                                        <![CDATA[" + replyMsg + @"]]>
                                </Content></xml>";
            return resxml;

            //ResponseText response = new ResponseText(info);
            //response.Content = "抱歉,此功能暂未开通。";
            //result = response.ToXml();
        }