Ejemplo n.º 1
0
        public HttpResponseMessage WetChatVerify(HttpRequestMessage content)      //HttpRequestMessage 和HttpResponseMessage,分别用于封装Requset和Response
        {
            string echostr = (from kvp in content.GetQueryNameValuePairs()
                              where kvp.Key == "echostr"
                              select kvp.Value).FirstOrDefault();

            string signature = (from kvp in content.GetQueryNameValuePairs()
                                where kvp.Key == "signature"
                                select kvp.Value).FirstOrDefault();

            string timestamp = (from kvp in content.GetQueryNameValuePairs()
                                where kvp.Key == "timestamp"
                                select kvp.Value).FirstOrDefault();

            string nonce = (from kvp in content.GetQueryNameValuePairs()
                            where kvp.Key == "nonce"
                            select kvp.Value).FirstOrDefault();

            log.Info("echostr:" + echostr + " signature:" + signature + " nonce:" + nonce);
            string xmlContent = content.Content.ReadAsStringAsync().Result;
            string response   = string.Empty;

            log.Info("xml3:" + xmlContent);

            if (!string.IsNullOrEmpty(xmlContent))
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xmlContent);
                WeChatMessage msg = WeChatHelper.GetWxMessage(xmlContent);

                if (msg.MsgType.Trim() == "text")//用户发送一些文字信息
                {
                    string text = WXMsgUtil.GetFromXML(doc, "Content") + "";

                    response = WXMsgUtil.GetTulingMsg(text != ""?text:"你是谁?") + "";
                    log.Info("text:" + text + " tuling:" + response);
                }
                if (msg.MsgType.Trim() == "event")//点击菜单或者新增/取消关注
                {
                    switch (msg.EventName.Trim().ToLower())
                    {
                    case "click":          //点击菜单
                        response = "haha";
                        break;

                    case "subscribe":        //用户新增关注(可以返回一些欢迎信息之类)                                  
                        response = "wawa";
                        break;

                    case "unsubscribe":       //用户取消关注(一般不需要去返回什么信息)
                    default:
                        break;
                    }
                }

                HttpResponseMessage xmlResult = new HttpResponseMessage();
                xmlResult.Content = new StringContent(WXMsgUtil.CreateTextMsg(doc, response != "" ? response : "返回不能为空值!"));
                return(xmlResult);
            }
            string returnStr = "";

            if (string.IsNullOrEmpty(echostr) | string.IsNullOrEmpty(signature) | string.IsNullOrEmpty(timestamp) | string.IsNullOrEmpty(nonce))
            {
                returnStr = "error";
            }

            //if (CheckSignature(signature, timestamp, nonce))
            //{
            //    log.Info("验证成功,返回:" + echostr);
            returnStr = echostr;
            //}

            HttpResponseMessage result = new HttpResponseMessage();

            result.Content = new StringContent(returnStr != ""?returnStr:"返回不能为空值!");
            return(result);
        }
Ejemplo n.º 2
0
        public void responseMsg(HttpContext context, XmlDocument xmlDoc)
        {
            string result  = "";
            string msgType = WXMsgUtil.GetFromXML(xmlDoc, "MsgType");

            switch (msgType)
            {
            case "event":
                switch (WXMsgUtil.GetFromXML(xmlDoc, "Event"))
                {
                case "subscribe":         //订阅
                    break;

                case "unsubscribe":         //取消订阅
                    break;

                case "CLICK":
                    DataTable dtMenuMsg = MenuMsgDal.GetMenuMsg(WXMsgUtil.GetFromXML(xmlDoc, "EventKey"));
                    if (dtMenuMsg.Rows.Count > 0)
                    {
                        List <Dictionary <string, string> > dictList = new List <Dictionary <string, string> >();
                        foreach (DataRow dr in dtMenuMsg.Rows)
                        {
                            Dictionary <string, string> dict = new Dictionary <string, string>();
                            dict["Title"]       = dr["Title"].ToString();
                            dict["Description"] = dr["Description"].ToString();
                            dict["PicUrl"]      = dr["PicUrl"].ToString();
                            dict["Url"]         = dr["Url"].ToString();
                            dictList.Add(dict);
                        }
                        result = WXMsgUtil.CreateNewsMsg(xmlDoc, dictList);
                    }
                    else
                    {
                        result = WXMsgUtil.CreateTextMsg(xmlDoc, "无此消息哦");
                    }
                    break;

                default:
                    break;
                }
                break;

            case "text":
                string text = WXMsgUtil.GetFromXML(xmlDoc, "Content");
                //if (text == "合肥" || text == "合肥天气" || text == "合肥天气预报"
                //    || text.ToLower() == "hf" || text.ToLower() == "hefei")
                //{
                //    result = WXMsgUtil.CreateNewsMsg(xmlDoc, WeatherUtil.GetWeatherInfo());
                //}
                //else
                {
                    result = WXMsgUtil.CreateNewsMsg(xmlDoc, Weather.GetForecastInfo(text));
                    //result = WXMsgUtil.CreateTextMsg(xmlDoc, WXMsgUtil.GetTulingMsg(text));
                }
                break;

            default:
                break;
            }

            if (!string.IsNullOrWhiteSpace(sAppID)) //没有AppID则不加密(订阅号没有AppID)
            {
                //加密
                WXBizMsgCrypt wxcpt       = new WXBizMsgCrypt(sToken, sEncodingAESKey, sAppID);
                string        sEncryptMsg = ""; //xml格式的密文
                string        timestamp   = context.Request["timestamp"];
                string        nonce       = context.Request["nonce"];
                int           ret         = wxcpt.EncryptMsg(result, timestamp, nonce, ref sEncryptMsg);
                if (ret != 0)
                {
                    //FileLogger.WriteErrorLog(context, "加密失败,错误码:" + ret);
                    Log.WriteDebug("加密失败,错误码:" + ret);
                    return;
                }

                context.Response.Write(sEncryptMsg);
                context.Response.Flush();
            }
            else
            {
                context.Response.Write(result);
                context.Response.Flush();
            }
        }