Example #1
0
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                string filePath = context.Request.FilePath;
                if (filePath.EndsWith("EventMsgNotify.wxopenmsg"))//拦截处理消息
                {
                    context.Response.ContentType = "text/plain";
                    string signature   = context.Request["msg_signature"]; //签名串
                    string timeStamp   = context.Request["timestamp"];     //时间戳
                    string nonce       = context.Request["nonce"];         //随机串
                    string encryptType = context.Request["encrypt_type"];
                    string xml         = "";                               //解密后的xml字符串
                    if (bllWeixinOpen.DecryptMsg(signature, timeStamp, nonce, RequestXML(context).InnerXml, out xml))
                    {
                        string appId = filePath.Split('/')[2];

                        #region 微信的测试机器人
                        if (appId == "wx570bc396a51b8ff8")
                        {
                            var dic = bllWeixinOpen.XmlToDictionary(xml);
                            switch (dic["MsgType"].ToString().ToLower())
                            {
                                #region 事件消息
                            case "event":
                                var acctoken = GetToken();
                                bllWeixin.SendKeFuMessageText(GetToken(), dic["FromUserName"], dic["Event"].ToString() + "from_callback");

                                break;
                                #endregion

                                #region 文本消息
                            case "text":
                                if (dic["Content"].ToString().StartsWith("QUERY_AUTH_CODE"))
                                {
                                    var code = dic["Content"].Replace("QUERY_AUTH_CODE:", "");
                                    //客服消息
                                    var authInfo    = bllWeixinOpen.GetQueryAuth(code);
                                    var accessToken = authInfo.authorization_info.authorizer_access_token;
                                    bllWeixin.SendKeFuMessageText(accessToken, dic["FromUserName"], code + "_from_api");
                                    SaveToken(accessToken);
                                }
                                else
                                {
                                    bllWeixin.SendKeFuMessageText(GetToken(), dic["FromUserName"], "TESTCOMPONENT_MSG_TYPE_TEXT_callback");
                                }
                                break;

                                #endregion
                            default:
                                break;
                            }
                            context.Response.Write(string.Empty);
                            //context.Response.Flush();
                            //context.Response.Close();
                            return;
                        }
                        #endregion

                        #region 一般公众号
                        WebsiteInfo websiteInfo = bllWeixin.Get <WebsiteInfo>(string.Format(" AuthorizerAppId='{0}'", appId));
                        if (websiteInfo == null)
                        {
                            return;
                        }

                        bllWeixin.SetWebSiteOwner(websiteInfo.WebsiteOwner);

                        string result = bllWeixin.ActionResultOpen(xml, websiteInfo.WebsiteOwner);

                        if (result.Contains("<Content><![CDATA[]]></Content>") && result.ToLower().Contains("<msgtype>text</msgtype>"))
                        {
                            result = result.Replace("<Content><![CDATA[]]></Content>", "").Replace("<MsgType>Text</MsgType>", "<MsgType><![CDATA[transfer_customer_service]]></MsgType>");
                            //context.Response.Write(result);
                            //return;
                        }


                        string resp = bllWeixinOpen.EncryptMsg(result, timeStamp, nonce);
                        context.Response.Write(resp);
                        return;

                        #endregion
                    }
                    else
                    {
                        context.Response.Write("fail");
                        return;
                    }
                }
                return;
            }
            catch (Exception ex)
            {
                context.Response.Write(ex.ToString());
                return;
            }
        }