Ejemplo n.º 1
0
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="loginInfo">用户登录信息</param>
        /// <param name="token">用户token</param>
        /// <returns></returns>
        public WxLoginResponseInfo Login(LoginInfo loginInfo, out string token)
        {
            WxLoginResponseInfo result = null;

            try
            {
                ////检查手机号是否注册过
                //if (!userDbDal.IsCustomerNameExists(loginInfo.CustomerName, out userId))
                //{
                //    ThrowResponseContextException(ErrCode.AccountNotExist);
                //}

                //验证登录
                string weiXinSessionKeyUrl           = ConfigurationManager.AppSettings["WeiXinSessionKeyUrl"];
                string weiXinAppId                   = ConfigurationManager.AppSettings["WeiXinAppId"];
                string weiXinSecret                  = ConfigurationManager.AppSettings["WeiXinSecret"];
                Dictionary <string, string> paramDic = new Dictionary <string, string>();
                paramDic.Add("appid", weiXinAppId);
                paramDic.Add("secret", weiXinSecret);
                paramDic.Add("js_code", loginInfo.code);
                paramDic.Add("grant_type", "authorization_code");
                var response = HttpHelper.Get(weiXinSessionKeyUrl, paramDic);
                if (string.IsNullOrEmpty(response) || response.Contains("errcode"))
                {
                    ThrowResponseContextException(ErrCode.TokenPastDue);
                }
                WxLoginInfo        info     = JsonHelper.JsonToObject <WxLoginInfo>(response);
                TokenOpearteResult tokenRes = TokenSrv.GetWXToken(info);
                if (!tokenRes.isok)
                {
                    token = string.Empty;
                    ThrowResponseContextException(ErrCode.TokenPastDue);
                }
                else
                {
                    token  = tokenRes.token;
                    result = new WxLoginResponseInfo
                    {
                        thrdsession = tokenRes.token
                    };

                    Task.Run(() => AddWxUserInfo(loginInfo, info.openid));
                }
            }
            catch (Exception ex)
            {
                LogHelper.Exception(ex);
                throw;
            }
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取新的用户token
        /// </summary>
        /// <param name="val">用户信息</param>
        /// <returns></returns>
        public TokenOpearteResult GetWXToken(WxLoginInfo val)
        {
            TimeSpan Expire = TimeSpan.FromDays(3);
            //string customerName = val.CustomerName;
            //var oldtoken = RedisEntity.HashGet(LoginCustomerNameListKey, customerName);
            ////被挤掉的用户token
            //if (!string.IsNullOrEmpty(oldtoken))
            //{
            //    var customerId = RedisEntity.HashGet(LoginCustomerListKey, oldtoken);
            //    CustomerRedisDal cusRedisDal = new CustomerRedisDal();
            //    CustomerDetail oldLoginInfo = cusRedisDal.GetCustomerDetail(long.Parse(customerId));
            //    //不是同一台设备
            //    if (oldLoginInfo != null)
            //    {
            //        //添加到被挤掉的用户
            //        string crowKey = CrowdedTokenKey + ":" + oldtoken;
            //        RedisEntity.ItemSet<string>(crowKey, customerName);
            //        RedisEntity.ItemSetExpire(crowKey, DateTime.Now.AddDays(1));
            //    }
            //    RedisEntity.HashRemove(LoginCustomerNameListKey, customerName);
            //    RedisEntity.HashRemove(LoginCustomerListKey, oldtoken);
            //    //记录删除的token列表
            //    LogHelper.Debug("移除旧的token:" + oldtoken + "|" + customerName);
            //}
            string token = Guid.NewGuid().ToString();

            RedisEntity.HashSet(WXLoginListKey, token, val.openid + ";" + val.session_key);
            RedisEntity.HashSetExpire(token, Expire);
            TokenOpearteResult result = new TokenOpearteResult
            {
                isok  = true,
                token = token
            };

            return(result);
        }
Ejemplo n.º 3
0
        public async Task <Response <Login> > WxLogin([FromBody] dynamic request)
        {
            string wxcode = request.wxcode;

            Login  resultData = new Login();
            string appid      = Configuration.GetValue <string>("AppSetting:WxAppid");
            string secret     = Configuration.GetValue <string>("AppSetting:WxSecret");
            string uri        = $"https://api.weixin.qq.com/sns/jscode2session?appid={appid}&secret={secret}&js_code={wxcode}&grant_type=authorization_code";
            string response   = await Task.Run(() => { return(HttpHelper.HttpJsonGetRequest(uri)); });

            if (!string.IsNullOrEmpty(response))
            {
                WxLoginInfo wxInfo = JsonConvert.DeserializeObject <WxLoginInfo>(response);

                if (wxInfo != null && !string.IsNullOrEmpty(wxInfo.openid))
                {
                    resultData.OpenId  = wxInfo.openid;
                    resultData.UnionId = wxInfo.unionid;

                    var claims = new Claim[] {
                        new Claim(ClaimTypes.Name, wxInfo.openid),
                        //new Claim(JwtRegisteredClaimNames.NameId, wxInfo.openid),
                        //new Claim(JwtRegisteredClaimNames.UniqueName,string.IsNullOrEmpty(wxInfo.unionid)?"":wxInfo.unionid)
                    };

                    var secretKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration.GetValue <string>("AppSetting:JwtSigningKey")));
                    var token     = new JwtSecurityToken(
                        issuer: Configuration.GetValue <string>("AppSetting:JwtIssuer"),
                        audience: Configuration.GetValue <string>("AppSetting:JwtAudience"),
                        claims: claims,
                        notBefore: DateTime.Now,
                        expires: DateTime.Now.AddDays(1),
                        signingCredentials: new SigningCredentials(secretKey, SecurityAlgorithms.HmacSha256)
                        );

                    resultData.JwtToken = new JwtSecurityTokenHandler().WriteToken(token);

                    //获取对应的宾馆
                    //var hotel = Hander.GetHotelByOpenId(wxInfo.openid);


                    var manager = await Task.Run(() =>
                    {
                        return(Hander.Get(m => m.WxOpenId == wxInfo.openid && m.IsDel.HasValue && !m.IsDel.Value));
                    });

                    if (manager != null)
                    {
                        resultData.Role = manager.Role;
                        var hotel = await Task.Run(() => { return(HotelHander.Get(h => h.Id == manager.HotelId)); });

                        resultData.Hotel = hotel;
                    }
                    else
                    {
                        resultData.Role = -1;
                    }
                }
                else
                {
                    throw new Exception("微信登录接口返回异常!" + response);
                }
            }
            else
            {
                throw new Exception("微信登录接口返回为空");
            }
            return(new Response <Login>()
            {
                Status = StatusEnum.Success, Massage = "登录成功", Data = resultData
            });
        }
Ejemplo n.º 4
0
        public static TokenOpearteResult GetWXToken(WxLoginInfo val)
        {
            TokenRedisDal tokenDal = new TokenRedisDal();

            return(tokenDal.GetWXToken(val));
        }