static readonly bool isWxLogger = true; //微信日志开关

        protected VWeiXinOAuthTemplatedWebControl()
        {
            SiteSettings masterSettings = SettingsManager.GetMasterSettings(true);

            string currentRequestUrl = HttpContext.Current.Request.Url.ToString();

            currentRequestUrl = System.Text.RegularExpressions.Regex.Replace(currentRequestUrl, "[\f\n\r\t\v]", "");

            #region
            WeiXinOAuthAttribute oAuth2Attr = Attribute.GetCustomAttribute(this.GetType(), typeof(WeiXinOAuthAttribute)) as WeiXinOAuthAttribute;

            WxLogger("*****************请求进入会员中心*****************");

            WxLogger("********Request.Url****" + currentRequestUrl);
            WxLogger("********ReferralId****" + this.Page.Request.QueryString["ReferralId"]);

            //WxLogger("Cookies:" + Globals.GetCurrentMemberUserId());

            MemberInfo currentMember = null;

            currentMember = MemberProcessor.GetCurrentMember();
            WxLogger("调式Vshop-Member-COOKIE:" + Globals.GetCurrentMemberUserId().ToString());
            WxLogger("调式currentMember:" + ((currentMember == null)?"empty":"111"));



            if (currentMember != null)    // || (this.Page.Session["userid"] == null || this.Page.Session["userid"].ToString() != currentMember.UserId.ToString())
            {
                //爽爽挝啡需求:店铺增加粉丝数
                string storeId = HiCache.Get(string.Format("DataCache-sub-StoreId-{0}", currentMember.OpenId)) as string;
                if (!string.IsNullOrEmpty(storeId))
                {
                    ManagerHelper.addStoreFansCount(storeId.ToInt(), currentMember.UserId, 0);
                    HiCache.Remove(string.Format("DataCache-sub-StoreId-{0}", currentMember.OpenId));
                }

                /*爽爽挝啡的代理商系统跳转判断:如果aspnet_member表的topRegionId为0或者不为1,则表示未激活,跳转到非法页面*/
                if (CustomConfigHelper.Instance.IsBuyerNeedsToBeActive)
                {
                    if (currentMember.TopRegionId == 0 || currentMember.TopRegionId != 1)
                    {
                        GotoResourceNotFound("您尚未被授权登录后台采购系统");
                    }
                }
                WxLogger(string.Format("        状态信息:**用户“{0}”已登录,中止请求微信**", currentMember.UserName));

                if (null != oAuth2Attr)
                {
                    switch (oAuth2Attr.WeiXinOAuthPage)
                    {
                    case WeiXinOAuthPage.VLogin:
                    case WeiXinOAuthPage.VRegister:
                    case WeiXinOAuthPage.VUserLogin:
                    {
                        string tUrl = (string.IsNullOrEmpty(this.Page.Request.QueryString["ReferralId"]))
                                        ? "~/vshop/MemberCenter.aspx" : "~/vshop/MemberCenter.aspx?ReferralId=" + this.Page.Request.QueryString["ReferralId"];
                        Page.Response.Redirect(tUrl, true);
                        break;
                    }

                    case WeiXinOAuthPage.VMemberCenter:
                    {
                        break;
                    }

                    default:
                    {
                        break;
                    }
                    }
                }
                return;
            }

            //读取配置信息.
            if (masterSettings.IsValidationService)     //是否启用微信登录
            {
                string code = this.Page.Request.QueryString["code"];

                if (!string.IsNullOrEmpty(code))
                {
                    WxLogger("      状态信息:**从微信网关授权回来**");

                    WxLogger("      code:" + code);

                    #region 取到了code,说明用户同意了授权登录

                    string responseResult = this.GetResponseResult("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + masterSettings.WeixinAppId + "&secret=" + masterSettings.WeixinAppSecret + "&code=" + code + "&grant_type=authorization_code");

                    WxLogger("      获取令牌:" + responseResult);

                    if (responseResult.Contains("access_token"))
                    {
                        JObject obj2 = JsonConvert.DeserializeObject(responseResult) as JObject;

                        string openId = obj2["openid"].ToString();    //微信用户OPENID
                        if (1 == 1)
                        {
                            string wxUserInfoStr = this.GetResponseResult("https://api.weixin.qq.com/sns/userinfo?access_token=" + obj2["access_token"].ToString() + "&openid=" + obj2["openid"].ToString() + "&lang=zh_CN");

                            WxLogger("      用户信息:" + wxUserInfoStr);

                            if (wxUserInfoStr.Contains("nickname"))
                            {
                                JObject wxUserInfo = JsonConvert.DeserializeObject(wxUserInfoStr) as JObject;

                                if (this.SkipWinxinOpenId(Globals.UrlDecode(wxUserInfo["nickname"].ToString()), wxUserInfo["openid"].ToString(), wxUserInfo["headimgurl"].ToString(), Page.Request["state"], 0))
                                {
                                    WxLogger("      状态信息:**微信绑定登录成功**" + currentRequestUrl);

                                    this.Page.Response.Redirect(currentRequestUrl);
                                }
                                else
                                {
                                    WxLogger("      状态信息:**微信绑定登录失败**");

                                    this.Page.Response.Redirect(Globals.ApplicationPath + "/Vshop/index.aspx");
                                }
                            }
                            else
                            {
                                WxLogger("      状态信息:**微信登录失败**");

                                this.Page.Response.Redirect(Globals.ApplicationPath + "/Vshop/index.aspx");
                            }
                        }
                        else
                        {
                        }
                    }
                    else
                    {
                        WxLogger("      状态信息:**获取信息失败**");

                        this.Page.Response.Redirect(Globals.ApplicationPath + "/Vshop/index.aspx");
                    }

                    #endregion
                }
                else if (!string.IsNullOrEmpty(this.Page.Request.QueryString["state"]))
                {
                    this.Page.Response.Redirect(Globals.ApplicationPath + "/Vshop/index.aspx");
                }
                else
                {
                    #region 跳转到微信登录

                    WxLogger("      状态信息:**到微信网关授权**");

                    string state = "";

                    if (System.Web.HttpContext.Current.Request.Cookies["Vshop-ReferralId"] != null)
                    {
                        state = System.Web.HttpContext.Current.Request.Cookies.Get("Vshop-ReferralId").Value;
                    }

                    WxLogger("      state:" + state);

                    string url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + masterSettings.WeixinAppId + "&redirect_uri=" + Globals.UrlEncode(currentRequestUrl) + "&response_type=code&scope=snsapi_userinfo&state=" + (string.IsNullOrWhiteSpace(state) ? "STATE" : state) + "#wechat_redirect";

                    /*url = (string.IsNullOrEmpty(this.Page.Request.QueryString["ReferralId"]))
                    *               ? url :url+ "&ReferralId=" + this.Page.Request.QueryString["ReferralId"];*/
                    /*url = (string.IsNullOrEmpty(this.Page.Request.QueryString["ReferralId"]) || url.ToLower().IndexOf("referralid") > -1)
                     *              ? url : url + "&ReferralId=" + this.Page.Request.QueryString["ReferralId"];*/
                    WxLogger("      状态信息:**测试**" + url);

                    //这里是微信入口
                    this.Page.Response.Redirect(url, true);

                    #endregion
                }
            }
            else
            {
                //if (!string.IsNullOrEmpty(masterSettings.WeixinLoginUrl))
                //{
                //    WxLogger("      状态信息:**跳转到通用登陆接口" + masterSettings.WeixinLoginUrl + "**");
                //    this.Page.Response.Redirect(masterSettings.WeixinLoginUrl);
                //}
                //else
                //{

                WxLogger("      状态信息:**跳转到通用登陆接口" + masterSettings.WeixinLoginUrl + "**");

                #region 加上尾巴

                int ReferralUserId = (null == currentMember ? 0 : currentMember.ReferralUserId);    // GetReferralUserId();

                //跳转过来的URL
                Uri urlReferrer = HttpContext.Current.Request.UrlReferrer;

                string returnUrl = currentRequestUrl;

                if (ReferralUserId > 0 && returnUrl.Contains("?"))
                {
                    returnUrl += "&ReferralUserId=" + ReferralUserId.ToString();
                }
                //else
                //{
                //    returnUrl += "?ReferralUserId=" + ReferralUserId.ToString();
                //}

                #endregion

                // this.Page.Response.Redirect("Login.aspx?returnUrl=" + Globals.UrlEncode(returnUrl));


                if (null != oAuth2Attr)
                {
                    switch (oAuth2Attr.WeiXinOAuthPage)
                    {
                    case WeiXinOAuthPage.VLogin:
                    case WeiXinOAuthPage.VMemberCenter:
                    {
                        this.Page.Response.Redirect(Globals.ApplicationPath + "/Vshop/UserLogin.aspx?returnUrl=" + Globals.UrlEncode(returnUrl));
                        break;
                    }

                    default:
                    {
                        break;
                    }
                    }
                }
            }
            #endregion



            // }
        }
        static readonly bool isWxLogger = false; //微信日志开关

        protected VWeiXinOAuthTemplatedWebControl()
        {
            WeiXinOAuthAttribute oAuth2Attr = Attribute.GetCustomAttribute(this.GetType(), typeof(WeiXinOAuthAttribute)) as WeiXinOAuthAttribute;

            WxLogger("*****************请求进入会员中心*****************");

            //WxLogger("Cookies:" + Globals.GetCurrentMemberUserId());

            MemberInfo currentMember = null;

            currentMember = MemberProcessor.GetCurrentMember();

            if (currentMember != null)// || (this.Page.Session["userid"] == null || this.Page.Session["userid"].ToString() != currentMember.UserId.ToString())
            {
                WxLogger(string.Format("        状态信息:**用户“{0}”已登录,中止请求微信**", currentMember.UserName));

                if (null != oAuth2Attr)
                {
                    switch (oAuth2Attr.WeiXinOAuthPage)
                    {
                    case WeiXinOAuthPage.VLogin:
                    case WeiXinOAuthPage.VRegister:
                    case WeiXinOAuthPage.VUserLogin:
                    {
                        Page.Response.Redirect("~/vshop/MemberCenter.aspx", true);
                        break;
                    }

                    case WeiXinOAuthPage.VMemberCenter:
                    {
                        break;
                    }

                    default:
                    {
                        break;
                    }
                    }
                }

                return;
            }

            //读取配置信息
            SiteSettings masterSettings = SettingsManager.GetMasterSettings(true);

            if (masterSettings.IsValidationService) //是否启用微信登录
            {
                string code = this.Page.Request.QueryString["code"];

                if (!string.IsNullOrEmpty(code))
                {
                    WxLogger("      状态信息:**从微信网关授权回来**");

                    WxLogger("      code:" + code);

                    #region 取到了code,说明用户同意了授权登录

                    string responseResult = this.GetResponseResult("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + masterSettings.WeixinAppId + "&secret=" + masterSettings.WeixinAppSecret + "&code=" + code + "&grant_type=authorization_code");

                    WxLogger("      获取令牌:" + responseResult);

                    if (responseResult.Contains("access_token"))
                    {
                        JObject obj2 = JsonConvert.DeserializeObject(responseResult) as JObject;

                        string openId = obj2["openid"].ToString();//微信用户OPENID

                        WxLogger("      openid:" + openId);

                        bool bOpenIdExists = this.CheckUserByOpenId(openId);// 判断openid是否存在

                        WxLogger("      openid:" + (bOpenIdExists ? "已绑定用户" : "未绑定用户 "));

                        if (!bOpenIdExists)
                        {
                            string wxUserInfoStr = this.GetResponseResult("https://api.weixin.qq.com/sns/userinfo?access_token=" + obj2["access_token"].ToString() + "&openid=" + obj2["openid"].ToString() + "&lang=zh_CN");

                            WxLogger("      用户信息:" + wxUserInfoStr);

                            if (wxUserInfoStr.Contains("nickname"))
                            {
                                JObject wxUserInfo = JsonConvert.DeserializeObject(wxUserInfoStr) as JObject;

                                if (this.SkipWinxinOpenId(Globals.UrlDecode(wxUserInfo["nickname"].ToString()), wxUserInfo["openid"].ToString(), wxUserInfo["headimgurl"].ToString(), Page.Request["state"]))
                                {
                                    WxLogger("      状态信息:**微信绑定登录成功**");

                                    //跳志到原来页面
                                    // this.Page.Response.Redirect("~/vshop/MemberCenter.aspx", true);
                                    this.Page.Response.Redirect(HttpContext.Current.Request.Url.ToString());
                                }
                                else
                                {
                                    WxLogger("      状态信息:**微信绑定登录失败**");

                                    this.Page.Response.Redirect(Globals.ApplicationPath + "/Vshop/Default.aspx");
                                }
                            }
                            else
                            {
                                WxLogger("      状态信息:**微信登录失败**");

                                this.Page.Response.Redirect(Globals.ApplicationPath + "/Vshop/Default.aspx");
                            }
                        }
                    }
                    else
                    {
                        WxLogger("      状态信息:**获取信息失败**");

                        this.Page.Response.Redirect(Globals.ApplicationPath + "/Vshop/Default.aspx");
                    }

                    #endregion
                }
                else if (!string.IsNullOrEmpty(this.Page.Request.QueryString["state"]))
                {
                    this.Page.Response.Redirect(Globals.ApplicationPath + "/Vshop/Default.aspx");
                }
                else
                {
                    #region 跳转到微信登录

                    WxLogger("      状态信息:**到微信网关授权**");

                    string state = "";

                    if (System.Web.HttpContext.Current.Request.Cookies["Vshop-ReferralId"] != null)
                    {
                        state = System.Web.HttpContext.Current.Request.Cookies.Get("Vshop-ReferralId").Value;
                    }

                    WxLogger("      state:" + state);

                    string url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + masterSettings.WeixinAppId + "&redirect_uri=" + Globals.UrlEncode(HttpContext.Current.Request.Url.ToString()) + "&response_type=code&scope=snsapi_userinfo&state=" + (string.IsNullOrWhiteSpace(state) ? "STATE" : state) + "#wechat_redirect";

                    //这里是微信入口
                    this.Page.Response.Redirect(url, true);

                    #endregion
                }
            }
            else
            {
                //if (!string.IsNullOrEmpty(masterSettings.WeixinLoginUrl))
                //{
                //    WxLogger("      状态信息:**跳转到通用登陆接口" + masterSettings.WeixinLoginUrl + "**");
                //    this.Page.Response.Redirect(masterSettings.WeixinLoginUrl);
                //}
                //else
                //{

                WxLogger("      状态信息:**跳转到通用登陆接口" + masterSettings.WeixinLoginUrl + "**");

                #region 加上尾巴

                int ReferralUserId = (null == currentMember ? 0 : currentMember.ReferralUserId);// GetReferralUserId();

                //跳转过来的URL
                Uri urlReferrer = HttpContext.Current.Request.UrlReferrer;

                string returnUrl = HttpContext.Current.Request.Url.ToString();

                if (ReferralUserId > 0 && returnUrl.Contains("?"))
                {
                    returnUrl += "&ReferralUserId=" + ReferralUserId.ToString();
                }
                //else
                //{
                //    returnUrl += "?ReferralUserId=" + ReferralUserId.ToString();
                //}

                #endregion

                // this.Page.Response.Redirect("Login.aspx?returnUrl=" + Globals.UrlEncode(returnUrl));

                if (null != oAuth2Attr)
                {
                    switch (oAuth2Attr.WeiXinOAuthPage)
                    {
                    case WeiXinOAuthPage.VLogin:
                    case WeiXinOAuthPage.VMemberCenter:
                    {
                        this.Page.Response.Redirect(Globals.ApplicationPath + "/Vshop/UserLogin.aspx?returnUrl=" + Globals.UrlEncode(returnUrl));
                        break;
                    }

                    default:
                    {
                        break;
                    }
                    }
                }
            }



            // }
        }