Пример #1
0
        /// <summary>
        /// 详细页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult Detail(int id)
        {
            UserBLL    userBLL    = new UserBLL();
            UserEntity userEntity = userBLL.GetById(id);

            WxUserBLL wxUserBLL = new WxUserBLL();

            userEntity.wxUserEntity = wxUserBLL.GetByUserId(userEntity.userId);

            return(View(userEntity));
        }
Пример #2
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));
        }