Beispiel #1
0
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="openId"></param>
        public static WxUserEntity GetWxUser(string accessToken, string openId)
        {
            string url  = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken + "&openid=" + openId;
            string type = "utf-8";

            GetWXUsersHelper userHelper   = new GetWXUsersHelper();
            string           urlRes       = userHelper.GetUrltoHtml(url, type);
            WxUserEntity     wxUserEntity = JsonConvert.DeserializeObject <WxUserEntity>(urlRes);

            return(wxUserEntity);
        }
Beispiel #2
0
        /// <summary>
        /// 创建微信用户
        /// </summary>
        /// <param name="wxUserEntity"></param>
        /// <returns></returns>
        private int CreateWxUser(WeChat.WxUserEntity wxUserEntity)
        {
            UserEntity userEntity = new UserEntity()
            {
                account    = "",
                address    = "",
                birthday   = Helper.ConvertHelper.DEFAULT_DATE,
                createDate = DateTime.Now,
                email      = "",
                forbidden  = false,
                gender     = Convert.ToInt32(wxUserEntity.sex),
                grade      = 1,
                integral   = 1000,
                modifyDate = DateTime.Now,
                name       = wxUserEntity.nickname,
                password   = "",
                phone      = "",
                portrait   = wxUserEntity.headimgurl,
                remark     = "",
                signature  = "",
                state      = -1,
                type       = -1
            };

            UserEntity user = userBLL.ActionDal.ActionDBAccess.Insertable(userEntity).ExecuteReturnEntity();

            Entity.WxUserEntity wxUser = new Entity.WxUserEntity()
            {
                avatarUrl  = wxUserEntity.headimgurl,
                city       = wxUserEntity.city,
                country    = wxUserEntity.country,
                createDate = DateTime.Now,
                gender     = wxUserEntity.sex,
                modifyDate = DateTime.Now,
                nickName   = wxUserEntity.nickname,
                openId     = wxUserEntity.openid,
                province   = wxUserEntity.province,
                unionId    = wxUserEntity.unionId ?? "",
                userId     = user.userId
            };
            int rows = userBLL.ActionDal.ActionDBAccess.Insertable(wxUser).ExecuteCommand();

            return(rows);
        }
Beispiel #3
0
        public JsonResult WxLogin([FromForm] string code)
        {
            DataResult dr = new DataResult();

            try
            {
                if (string.IsNullOrWhiteSpace(code))
                {
                    dr.code = "201";
                    dr.msg  = "参数错误";
                    return(Json(dr));
                }

                //UserEntity userEntity = userBLL.GetById(10007);
                //userEntity.account = code;
                //userBLL.ActionDal.ActionDBAccess.Updateable(userEntity).ExecuteCommand();

                AccessTokenEntity accessTokenEntity = WeChat.LoginHelper.GetAccessToken(code);

                if (!string.IsNullOrWhiteSpace(accessTokenEntity.errcode))
                {
                    dr.code = "201";
                    dr.msg  = "获取AccessToken失败";
                    return(Json(dr));
                }

                AccessTokenEntity accessToken = WeChat.LoginHelper.GetRefreshToken(accessTokenEntity.refresh_token);

                if (!string.IsNullOrWhiteSpace(accessToken.errcode))
                {
                    dr.code = "201";
                    dr.msg  = "获取RefreshToken失败";
                    return(Json(dr));
                }

                AccessTokenEntity tokenEntity = WeChat.LoginHelper.SetExpiresIn(accessToken);

                if (string.IsNullOrWhiteSpace(tokenEntity.access_token) || string.IsNullOrWhiteSpace(tokenEntity.openid))
                {
                    dr.code = "201";
                    dr.msg  = "续期失败";
                    return(Json(dr));
                }

                WeChat.WxUserEntity wxUserEntity = WeChat.LoginHelper.GetWxUser(tokenEntity.access_token, tokenEntity.openid);

                if (string.IsNullOrWhiteSpace(wxUserEntity.openid) || !string.IsNullOrWhiteSpace(wxUserEntity.errcode))
                {
                    dr.code = "201";
                    dr.msg  = "获取用户信息失败";
                    return(Json(dr));
                }

                WxUserBLL wxUserBLL = new WxUserBLL();

                Entity.WxUserEntity wxUser = wxUserBLL.GetByOpenId(wxUserEntity.openid);

                if (wxUser == null)
                {
                    int rows = CreateWxUser(wxUserEntity);
                    if (rows > 0)
                    {
                        dr.code = "201";
                        dr.msg  = "创建用户失败";
                        return(Json(dr));
                    }
                }

                Entity.WxUserEntity wx = wxUserBLL.GetByOpenId(wxUserEntity.openid);

                UserEntity user = userBLL.GetById(wx.userId);

                UserTokenBLL    userTokenBLL    = new UserTokenBLL();
                UserTokenEntity userTokenEntity = userTokenBLL.GetByUserId(user.userId);
                UserTokenEntity userToken       = new UserTokenEntity();

                if (userTokenEntity == null)
                {
                    userToken = userTokenBLL.Create(user.userId);
                }
                else
                {
                    userToken = userTokenBLL.Update(userTokenEntity);
                }

                LoginResult loginResult = new LoginResult();
                loginResult.token      = userToken.token;
                loginResult.userEntity = user;

                dr.code = "200";
                dr.data = loginResult;
            }
            catch (Exception ex)
            {
                dr.code = "999";
                dr.msg  = ex.Message;
            }

            return(Json(dr));
        }