예제 #1
0
        public string appsecret = ""; //公众微信平台下可以找到

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //获取从wxProcess.aspx传递过来的跳转地址reurl
                string              reurl   = HttpContext.Current.Session["session_reurl"].ToString();
                int                 wid     = int.Parse(HttpContext.Current.Session["session_wid"].ToString());
                BLL.wx_userweixin   wx      = new BLL.wx_userweixin();
                Model.wx_userweixin wxModel = wx.GetModel(wid);
                appid     = wxModel.AppId;
                appsecret = wxModel.AppSecret;
                string code = "";
                if (Request.QueryString["code"] != null && Request.QueryString["code"] != "")
                {
                    //获取微信回传的code
                    code = Request.QueryString["code"].ToString();
                    OAuth_Token Model           = Get_token(code);                      //获取token
                    OAuthUser   OAuthUser_Model = Get_UserInfo(Model.access_token, Model.openid);
                    if (OAuthUser_Model.openid != null && OAuthUser_Model.openid != "") //已获取得openid及其他信息
                    {
                        //绑定数据
                        wx_ucard_users       userBLL   = new wx_ucard_users();
                        Model.wx_ucard_users userModel = new XCWeiXin.Model.wx_ucard_users();
                        bool Isuser = userBLL.getUserExists(OAuthUser_Model.openid);
                        if (Isuser)
                        {
                            userModel = userBLL.GetModel(OAuthUser_Model.openid);
                        }
                        userModel.wid       = wid;
                        userModel.openid    = OAuthUser_Model.openid;
                        userModel.wxName    = OAuthUser_Model.nickname;
                        userModel.sex       = OAuthUser_Model.sex;
                        userModel.language  = OAuthUser_Model.language;
                        userModel.province  = OAuthUser_Model.province;
                        userModel.city      = OAuthUser_Model.city;
                        userModel.country   = OAuthUser_Model.country;
                        userModel.headimgul = OAuthUser_Model.headimgurl;
                        userModel.subscribe = OAuthUser_Model.subscribe;
                        //   userModel.subscribe_time =DateTime.Parse(OAuthUser_Model.subscribe_time.Trim());
                        userModel.groupid = OAuthUser_Model.groupid;


                        if (!Isuser)//没有记录,添加
                        {
                            HttpCookie cookie = new HttpCookie("xcopenid");
                            cookie.Value   = OAuthUser_Model.openid;
                            cookie.Expires = DateTime.Now.AddDays(3650);
                            HttpContext.Current.Response.Cookies.Add(cookie);

                            userBLL.Add(userModel);
                        }
                        else//有记录更新
                        {
                            HttpCookie cookie = new HttpCookie("xcopenid");
                            cookie.Value   = OAuthUser_Model.openid;
                            cookie.Expires = DateTime.Now.AddDays(3650);
                            HttpContext.Current.Response.Cookies.Add(cookie);

                            ;                            userBLL.Update(userModel);
                        }
                        Response.Redirect("" + reurl + "&openid=" + OAuthUser_Model.openid + "");
                        //   Response.Redirect(reurl);
                    }
                    else  //未获得openid,回到wxProcess.aspx,访问弹出微信授权页面,提示用户授权
                    {
                        Response.Redirect("wxProcess.aspx?auth=1");
                    }
                }
            }
        }
예제 #2
0
        public int init()
        {
            BLL.wx_userweixin   wx      = new BLL.wx_userweixin();
            Model.wx_userweixin wxModel = wx.GetModel(wid);
            appid     = wxModel.AppId;
            appsecret = wxModel.AppSecret;
            string code = "";

            if (Request.QueryString["code"] != null && Request.QueryString["code"] != "")
            {
                //获取微信回传的code
                code = Request.QueryString["code"].ToString();
                OAuth_Token Model           = Get_token(code);                      //获取token
                OAuthUser   OAuthUser_Model = Get_UserInfo(Model.access_token, Model.openid);
                if (OAuthUser_Model.openid != null && OAuthUser_Model.openid != "") //已获取得openid及其他信息
                {
                    //绑定数据
                    wx_ucard_users       userBLL   = new wx_ucard_users();
                    Model.wx_ucard_users userModel = new XCWeiXin.Model.wx_ucard_users();
                    bool Isuser = userBLL.getUserExists(OAuthUser_Model.openid);
                    if (Isuser)
                    {
                        userModel = userBLL.GetModel(OAuthUser_Model.openid);
                    }
                    userModel.wid       = wid;
                    userModel.openid    = OAuthUser_Model.openid;
                    userModel.wxName    = OAuthUser_Model.nickname;
                    userModel.sex       = OAuthUser_Model.sex;
                    userModel.language  = OAuthUser_Model.language;
                    userModel.province  = OAuthUser_Model.province;
                    userModel.city      = OAuthUser_Model.city;
                    userModel.country   = OAuthUser_Model.country;
                    userModel.headimgul = OAuthUser_Model.headimgurl;
                    userModel.subscribe = OAuthUser_Model.subscribe;
                    //   userModel.subscribe_time =DateTime.Parse(OAuthUser_Model.subscribe_time.Trim());
                    userModel.groupid = OAuthUser_Model.groupid;

                    openid = OAuthUser_Model.openid;
                    if (!Isuser)//没有记录,添加
                    {
                        HttpCookie cookie = new HttpCookie("xcopenid" + wid + "");
                        cookie.Value   = OAuthUser_Model.openid;
                        cookie.Expires = DateTime.Now.AddDays(3650);
                        HttpContext.Current.Response.Cookies.Add(cookie);

                        userBLL.Add(userModel);
                        return(0);
                    }
                    else//有记录更新
                    {
                        HttpCookie cookie = new HttpCookie("xcopenid" + wid + "");
                        cookie.Value   = OAuthUser_Model.openid;
                        cookie.Expires = DateTime.Now.AddDays(3650);
                        HttpContext.Current.Response.Cookies.Add(cookie);

                        userBLL.Update(userModel);
                        return(0);
                    }

                    //  Response.Redirect("" + reurl + "&openid=" + OAuthUser_Model.openid + "");
                    //   Response.Redirect(reurl);
                }
                else      //未获得openid,回到wxProcess.aspx,访问弹出微信授权页面,提示用户授权
                {
                    return(1);
                }
            }
            else       //取code失败
            {
                return(2);
            }
        }