Exemplo n.º 1
0
        public Lebi_User GetUserByopenid(string openid, int DT_id = 0)
        {
            string res = Get("https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + Token + "&openid=" + openid + "&lang=zh_CN");
            JavaScriptSerializer jss = new JavaScriptSerializer();

            Model.weixin.userinfo model = jss.Deserialize <Model.weixin.userinfo>(res);
            Lebi_User             user  = B_Lebi_User.GetModel("UserName='******'");

            if (user == null)
            {
                user = new Lebi_User();
                user.bind_weixin_id       = openid;
                user.bind_weixin_nickname = model.nickname;
                //user.bind_weixin_token = tokeninfo.access_token;
                try
                {
                    user.Face = DownLoadImage(model.headimgurl);
                }
                catch
                {
                    user.Face = model.headimgurl;
                }
                user.NickName          = model.nickname;
                user.UserName          = "******" + openid;
                user.Password          = EX_User.MD5(openid);
                user.Language          = Language.CurrentLanguage().Code;
                user.Sex               = model.sex == "2" ? "女" : "男";
                user.UserLevel_id      = B_Lebi_UserLevel.GetList("Grade>0", "Grade asc").FirstOrDefault().id;
                user.IsPlatformAccount = 1;
                if (CurrentSite != null)
                {
                    user.Site_id = CurrentSite.id;
                }
                user.DT_id = DT_id;
                //B_Lebi_User.Add(user);
                //user.id = B_Lebi_User.GetMaxId();
                //userid = user.id;
                return(EX_User.UserReg(user));
            }
            else
            {
                user.bind_weixin_id       = openid;
                user.bind_weixin_nickname = model.nickname;
                //user.bind_weixin_token = tokeninfo.access_token; ;
                //if (user.Face == "")
                //{
                try
                {
                    user.Face = DownLoadImage(model.headimgurl);
                }
                catch
                {
                    user.Face = model.headimgurl;
                }
                //}
                if (user.NickName == "")
                {
                    user.NickName = model.nickname;
                }
                //user.Sex = model.gender == "f" ? "女" : "男";
                user.DT_id = DT_id;
                B_Lebi_User.Update(user);
                EX_User.LoginOK(user, 0);
                return(user);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 微信登录
        /// https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
        /// </summary>
        /// <param name="back"></param>
        /// <returns></returns>
        public string Login(string back, int IsLogin = 1)
        {
            string code = RequestTool.RequestString("code");

            if (code != "")
            {
                try
                {
                    string uri    = reurnurl + "?backurl=" + ENBackuri(back);
                    int    userid = 0;
                    uri = System.Web.HttpUtility.UrlEncode(uri);
                    string res = Post("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + appkey + "&code=" + code + "&grant_type=authorization_code");
                    JavaScriptSerializer jss = new JavaScriptSerializer();

                    Model.weixin.tokeninfo tokeninfo = jss.Deserialize <Model.weixin.tokeninfo>(res);
                    string uid = tokeninfo.openid;
                    //SystemLog.Add(uid);
                    //获取用户资料
                    //https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
                    res = Post("https://api.weixin.qq.com/sns/userinfo?access_token=" + tokeninfo.access_token + "&openid=" + uid + "&lang=zh_CN");

                    Model.weixin.userinfo model = jss.Deserialize <Model.weixin.userinfo>(res);
                    string where = "bind_weixin_id='" + uid + "' and bind_weixin_id!=''";
                    //if (DT_id > 0)
                    //{
                    //    where += " and DT_id =" + DT_id + "";
                    //}
                    Lebi_User user        = B_Lebi_User.GetModel(where);
                    Lebi_User CurrentUser = null;
                    string    qrcodeid_   = RegexTool.GetRegValue((back + "&"), @"qrcodeid=(\d*)&");
                    int       qrid        = 0;
                    int.TryParse(qrcodeid_, out qrid);
                    if (qrid > 0)
                    {
                        //扫描二维码授权绑定当前账号
                        Lebi_weixin_qrcode qcode = B_Lebi_weixin_qrcode.GetModel(qrid);
                        CurrentUser = B_Lebi_User.GetModel(qcode.User_id);
                        if (IsLogin == 0)
                        {
                            if (user != null && CurrentUser != null)
                            {
                                return("已绑定其它帐号");
                            }
                        }
                    }

                    if (user == null)
                    {
                        Lebi_UserLevel defaultlevel = B_Lebi_UserLevel.GetModel("Grade>0 order by Grade asc");
                        if (defaultlevel == null)
                        {
                            defaultlevel = new Lebi_UserLevel();
                        }
                        if (defaultlevel.RegisterType == 0) //关闭注册
                        {
                            return("会员注册已关闭");
                        }
                        if (CurrentUser == null)
                        {
                            user = new Lebi_User();
                            user.bind_weixin_id       = uid;
                            user.bind_weixin_nickname = model.nickname;
                            user.bind_weixin_token    = tokeninfo.access_token;
                            try
                            {
                                user.Face = DownLoadImage(model.headimgurl);
                            }
                            catch
                            {
                                user.Face = model.headimgurl;
                            }
                            user.NickName          = model.nickname;
                            user.UserName          = "******" + uid;
                            user.Password          = EX_User.MD5(uid);
                            user.Language          = Language.CurrentLanguage().Code;
                            user.Sex               = model.sex == "2" ? "女" : "男";
                            user.UserLevel_id      = B_Lebi_UserLevel.GetList("Grade>0", "Grade asc").FirstOrDefault().id;
                            user.IsPlatformAccount = 1;
                            //B_Lebi_User.Add(user);
                            //user.id = B_Lebi_User.GetMaxId();
                            //userid = user.id;
                            user.DT_id = DT_id;
                            EX_User.UserReg(user);
                        }
                        else
                        {
                            CurrentUser.bind_weixin_id       = uid;
                            CurrentUser.bind_weixin_nickname = model.nickname;
                            CurrentUser.bind_weixin_token    = tokeninfo.access_token;

                            try
                            {
                                CurrentUser.Face = DownLoadImage(model.headimgurl);
                            }
                            catch
                            {
                                CurrentUser.Face = model.headimgurl;
                            }
                            //CurrentUser.Face = model.headimgurl;//头像 50*50

                            if (CurrentUser.NickName == "")
                            {
                                CurrentUser.NickName = model.nickname;
                            }
                            CurrentUser.DT_id = DT_id;
                            //B_Lebi_User.Update(CurrentUser);
                            EX_User.LoginOK(CurrentUser, 0);
                            userid = CurrentUser.id;
                        }
                    }
                    else
                    {
                        user.bind_weixin_id       = uid;
                        user.bind_weixin_nickname = model.nickname;
                        user.bind_weixin_token    = tokeninfo.access_token;;

                        try
                        {
                            user.Face = DownLoadImage(model.headimgurl);
                        }
                        catch (Exception ex)
                        {
                            user.Face = "";
                            LB.Tools.TxtLog.Add(ex.Message);
                        }

                        if (user.NickName == "")
                        {
                            user.NickName = model.nickname;
                        }
                        user.DT_id = DT_id;
                        //user.Sex = model.gender == "f" ? "女" : "男";
                        //B_Lebi_User.Update(user);
                        EX_User.LoginOK(user, 0);
                        userid = user.id;
                    }


                    if (qrid > 0)
                    {
                        //扫描二维码登录
                        Lebi_weixin_qrcode qcode = B_Lebi_weixin_qrcode.GetModel(qrid);
                        qcode.User_id = userid;
                        B_Lebi_weixin_qrcode.Update(qcode);
                        //Log.Add(back);
                    }
                    return("OK");
                }
                catch (Exception ex)
                {
                    LB.Tools.TxtLog.Add(ex.Message);
                    return("授权失败" + ex.Message);
                }
            }
            return("授权失败");
        }