예제 #1
0
        public static WebchatJsUserinfo GetUserInfo(string userAgent, string CODE)
        {
            WechatConfig      wechatconfig = AccessTokenService.GetWechatConfig();
            WebchatJsUserinfo userinfo     = new WebchatJsUserinfo();
            string            url          = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + wechatconfig.Appid + "&secret=" + wechatconfig.AppSecret + "&code=" + CODE + "&grant_type=authorization_code";

            HttpWebResponse response = HttpWebResponseUtility.CreateGetHttpResponse(url, null, userAgent, null);

            Stream       stream = response.GetResponseStream();
            StreamReader sr     = new StreamReader(stream);
            string       result = sr.ReadToEnd();

            WechatJsToken token = JsonConvert.DeserializeObject <WechatJsToken>(result);


            string url2 = "https://api.weixin.qq.com/sns/userinfo?access_token=" + token.access_token + "&openid=" + token.openid + "&lang=zh_CN";

            HttpWebResponse res     = HttpWebResponseUtility.CreateGetHttpResponse(url2, null, userAgent, null);
            Stream          stream2 = res.GetResponseStream();
            StreamReader    sr2     = new StreamReader(stream2);
            string          result2 = sr2.ReadToEnd();

            userinfo = JsonConvert.DeserializeObject <WebchatJsUserinfo>(result2);

            return(userinfo);
        }
예제 #2
0
        public static string  RefrenshToken(string appid, string appsecret)
        {
            string userAgent = System.Web.HttpContext.Current.Request.UserAgent;
            string url       = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appid, appsecret);

            HttpWebResponse res    = HttpWebResponseUtility.CreateGetHttpResponse(url, null, userAgent, null);
            Stream          stream = res.GetResponseStream();
            StreamReader    sr     = new StreamReader(stream);
            string          result = sr.ReadToEnd();

            WechatAccessToken token = new WechatAccessToken();

            token = JsonConvert.DeserializeObject <WechatAccessToken>(result);
            string access_token = token.access_token;

            if (string.IsNullOrEmpty(token.access_token))
            {
                WechatError err = new WechatError();
                err          = JsonConvert.DeserializeObject <WechatError>(result);
                access_token = "appid或者appsecret错误,无法获取access_token 微信错误代码:" + err.errcode + "微信错误信息:" + err.errmsg;
                LogHelper.Error(access_token);
            }
            else
            {
                AccessTokenService.WriteToken(token.access_token, System.DateTime.Now.AddMinutes(115).ToString("yyyy-MM-dd HH:mm:ss"));
            }
            return(access_token);
        }
예제 #3
0
        public static string GetAccessToken()
        {
            string access_token = "";

            WechatConfig wechatConfig = AccessTokenService.GetWechatConfig();

            if (DateTime.Now < DateTime.Parse(wechatConfig.AccessTokenExpiredTime))
            {
                access_token = wechatConfig.AccessToken;
            }
            else
            {
                access_token = AccessTokenService.RefrenshToken(wechatConfig.Appid, wechatConfig.AppSecret);
            }
            return(access_token);
        }
예제 #4
0
        //处理微信信息
        public static void  Excute(string postStr)
        {
            Configuration      config     = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
            AppSettingsSection appsection = config.GetSection("appSettings") as AppSettingsSection;

            string WechatId = appsection.Settings["WechatId"].Value.ToString();

            LogHelper.Info("构建xml");

            XmlDocument xmldoc = new XmlDocument();

            LogHelper.Info("加载字符串");
            xmldoc.Load(new System.IO.MemoryStream(System.Text.Encoding.GetEncoding("GB2312").GetBytes(postStr)));
            LogHelper.Info("查找字节");
            XmlNode MsgType      = xmldoc.SelectSingleNode("/xml/MsgType");
            XmlNode FromUserName = xmldoc.SelectSingleNode("/xml/FromUserName");

            LogHelper.Info(MsgType.InnerText);

            if (MsgType != null)
            {
                if (MsgType.InnerText == "event")
                {
                    XmlNode EventContent = xmldoc.SelectSingleNode("/xml/Event");
                    switch (EventContent.InnerText)
                    {
                    case "subscribe":
                        WechatMessageServices.ResponseTextMessage(FromUserName.InnerText, WechatId, "您终于来了,感谢您的关注!\n\n 您是自闭症孩子家长吗?请您点击下方菜单了解本系统。\n\n 如有疑问,您可以拨打13945016428联系瑞夕老师进行咨询。");
                        break;

                    case "unsubscribe":
                        WechatMessageServices.ResponseTextMessage(FromUserName.InnerText, WechatId, "我哪里做的不好了,你居然敢离开我。");
                        LogHelper.Info("我哪里做的不好了,你居然敢离开我。");
                        break;

                    case "CLICK":

                        string token       = AccessTokenService.GetAccessToken();
                        string PreMsg      = "感谢您的认可,正在为您生成专属邀请卡。";
                        string cardMediaId = "slv-URo_tcvYdETXRXCkYC9LsbMh2atzB72d0NznT0o";
                        string LastMsg     = "你可以把这张图片发送给需要的人,让更多星星的孩子能够健康成长。";
                        MyWechatServices.invitationCard(token, FromUserName.InnerText, PreMsg, cardMediaId, LastMsg);
                        break;

                    case "LOCATION":
                        WechatMessageServices.ResponseSuccessMessage(FromUserName.InnerText, WechatId);
                        break;

                    default:
                        //这是非常好用的一个地方,打开公众号,我就会和你打招呼。
                        WechatMessageServices.ResponseSuccessMessage(FromUserName.InnerText, WechatId);
                        break;
                    }
                }
                else
                {
                    switch (MsgType.InnerText)
                    {
                    case "image":
                        WechatMessageServices.ResponseTextMessage(FromUserName.InnerText, WechatId, "好好玩的图片啊,你想告诉我什么呢?");
                        break;

                    case "text":

                        XmlNode TextContent = xmldoc.SelectSingleNode("/xml/Content");
                        string  keywords    = TextContent.InnerText;
                        if (string.IsNullOrEmpty(keywords))
                        {
                            WechatMessageServices.ResponseSuccessMessage(FromUserName.InnerText, WechatId);
                            WechatMessageServices.ResponseTextMessage(FromUserName.InnerText, WechatId, "您好,这是自闭症儿童家庭训练和评估系统,请点击下方菜单开始使用。\n\n 如有疑问,您可以拨打13945016428联系瑞夕老师进行咨询。");
                        }
                        else
                        {
                            switch (keywords)
                            {
                            case "自闭症资料":
                                WechatMessageServices.ResponseTextMessage(FromUserName.InnerText, WechatId, "您好,请先分享图片到朋友圈,帮助更多的自闭症家庭。\n\n然后请使用百度网盘下载该资料。\n\n 链接地址:https://pan.baidu.com/s/1pLdwA3D \n 密码:zeju \n\n 同时建议您点击下方开始训练按钮,加入自闭症家庭训练计划,让孩子提升更快。\n\n <a href=\"http://zbz.zuyanquxian.cn/wechat/login\">点击获取更多资料</a>");

                                break;

                            default:
                                WechatMessageServices.ResponseTextMessage(FromUserName.InnerText, WechatId, "您好,这是自闭症儿童家庭训练和评估系统,请点击下方菜单开始使用。\n\n 如有疑问,您可以拨打13945016428联系瑞夕老师进行咨询。");

                                break;
                            }
                        }

                        break;

                    default:
                        WechatMessageServices.ResponseTextMessage(FromUserName.InnerText, WechatId, "祝星星宝贝健康快乐成长。");
                        break;
                        //这几个一般用不到,我就没做判断。
                        //case "voice":
                        //    WechatMessageServices.ResponseTextMessage(FromUserName.InnerText, WechatId, "这是对语音的回复");
                        //    break;
                        //case "video":
                        //    WechatMessageServices.ResponseTextMessage(FromUserName.InnerText, WechatId, "这是对视频的回复");
                        //    break;
                        //case "shortvideo":
                        //    WechatMessageServices.ResponseTextMessage(FromUserName.InnerText, WechatId, "这是对小视频的回复");
                        //    break;
                        //case "location":
                        //    WechatMessageServices.ResponseTextMessage(FromUserName.InnerText, WechatId, "这是对地理位置的回复");
                        //    break;
                        //case "link":
                        //    WechatMessageServices.ResponseTextMessage(FromUserName.InnerText, WechatId, "这是对链接的回复");
                        //    break;
                    }
                }
            }
            else
            {
                WechatMessageServices.ResponseTextMessage(FromUserName.InnerText, WechatId, "success");
            }
        }