Esempio n. 1
0
        public async Task <Response <Hotel> > GetHotelById(int hotelId)
        {
            //int hotelId;

            //if(!int.TryParse(obj.hotelId.toString(),out hotelId))
            if (hotelId <= 0)
            {
                return(new Response <Hotel>()
                {
                    Status = StatusEnum.ValidateModelError, Massage = "参数错误"
                });
            }

            Hotel hotel = await Task.Run(() => { return(Hander.Get(h => h.Id == hotelId && !h.IsDel.Value)); });

            return(new Response <Hotel>()
            {
                Status = StatusEnum.Success, Data = hotel, Massage = "获取成功"
            });
        }
Esempio n. 2
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
            });
        }