Beispiel #1
0
        /// <summary>
        /// 整理文字消息格式
        /// </summary>
        /// <param name="requestXML"></param>
        /// <param name="mesg"></param>
        /// <returns></returns>
        private string checkXML(RequestXML requestXML, string mesg)
        {
            string now = "";

            now = "<xml><ToUserName><![CDATA[" + requestXML.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + requestXML.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) +
                  "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" +
                  mesg +
                  "]]></Content><FuncFlag>0</FuncFlag></xml>";
            return(now);
        }
Beispiel #2
0
        public void ProcessRequest(HttpContext context)
        {
            if (HttpContext.Current.Request.HttpMethod.ToLower() == "post")
            {
                var    db  = new WeChatPay.Models.ModelContext();
                string ret = "";
                Stream s   = System.Web.HttpContext.Current.Request.InputStream;
                byte[] b   = new byte[s.Length];
                s.Read(b, 0, (int)s.Length);
                var postStr = Encoding.UTF8.GetString(b);

                WriteTxt(postStr);
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(postStr);
                XmlElement rootElement = doc.DocumentElement;

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

                RequestXML requestXML = new RequestXML();
                requestXML.ToUserName   = rootElement.SelectSingleNode("ToUserName").InnerText;
                requestXML.FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText;
                requestXML.CreateTime   = rootElement.SelectSingleNode("CreateTime").InnerText;
                requestXML.MsgType      = MsgType.InnerText;


                if (requestXML.MsgType == "event")
                {
                    requestXML.Event = rootElement.SelectSingleNode("Event").InnerText;
                    var node = rootElement.SelectSingleNode("EventKey");
                    requestXML.EventKey = node == null ? "" : node.InnerText;
                    if (requestXML.Event.ToLower() == "subscribe" || requestXML.Event.ToLower() == "scan")
                    {
                        //查找user 没找到就插入
                        User user = db.UserSet.FirstOrDefault(u => u.OpenId == requestXML.FromUserName);
                        if (user == null)
                        {
                            user = new WeChatPay.Models.User
                            {
                                OpenId = requestXML.FromUserName,
                            };
                            db.UserSet.Add(user);
                        }
                        #region 博博会绑定标签
                        //根据事件号寻找标签
                        //var card = db.Cards.Include("Code").FirstOrDefault(c => "qrscene_" + c.Code.EventNumber ==
                        //                  (requestXML.Event == "subscribe" ? requestXML.EventKey : "qrscene_" + requestXML.EventKey));
                        //if (card != null)
                        //{
                        //    card.IsNeedSkip = false;
                        //    var userList = db.Users.Where(u => u.CardId == card.CardId);
                        //    foreach (var item in userList)
                        //    {
                        //        item.CardId = null;
                        //    }

                        //    //修改已经存在的user信息
                        //    user.CardId = card.CardId;
                        //}
                        //else
                        //{
                        //    if (string.IsNullOrEmpty(requestXML.EventKey))
                        //    {
                        //        user.Remarks = "Without any event key!";
                        //    }
                        //    else
                        //    {
                        //        user.Remarks = "With undefined event key " + requestXML.EventKey + "!";
                        //    }
                        //}

                        //if (user.WatchTime == null)
                        //{
                        //    user.WatchTime = new int?(0);
                        //}
                        //user.SubscribeTime = DateTime.Now;
                        //if (!string.IsNullOrEmpty(user.OpenId))
                        //{
                        //    Helper.UserHelper.GetUserInfo(user);
                        //}
                        #endregion

                        db.SaveChanges();

                        if (!string.IsNullOrEmpty(requestXML.EventKey))
                        {
                            requestXML.EventKey = requestXML.EventKey.Replace("qrscene_", "");
                            ret = Helper.WechatHelper.GetEventString(requestXML.FromUserName, requestXML.ToUserName, requestXML.EventKey);
                        }
                        else
                        {
                            ret = checkXML(requestXML, "欢迎使用爱点车联。");
                        }

                        //else
                        //{
                        //    ret = checkXML(requestXML, "事件编号:" + requestXML.EventKey);
                        //}
                    }
                }
                HttpContext.Current.Response.Write(ret);
                HttpContext.Current.Response.End();
            }
            else
            {
                string echoString = HttpContext.Current.Request.QueryString["echostr"];
                string signature  = HttpContext.Current.Request.QueryString["signature"];
                string timestamp  = HttpContext.Current.Request.QueryString["timestamp"];
                string nonce      = HttpContext.Current.Request.QueryString["nonce"];

                if (!string.IsNullOrEmpty(echoString))
                {
                    HttpContext.Current.Response.Write(echoString);
                    HttpContext.Current.Response.End();
                }
            }
        }