Ejemplo n.º 1
0
        public async Task <IActionResult> login([FromBody] LoginRequest loginRequest)
        {
            var token = await accountService.Login(loginRequest);

            return(Ok(token));
        }
        public async Task <ActionResult> LoginAsync([FromBody] LoginRequest loginRequest)
        {
            var result = await _authenticationService.LoginAsync(loginRequest, ModelState);

            return(result);
        }
Ejemplo n.º 3
0
        public ResponseResult <string> Login([FromBody] LoginRequest model)
        {
            try
            {
                if (model.Token == null)
                {
                    //直接调用微信接口登录
                    //检查Code
                    if (string.IsNullOrEmpty(model.Code))
                    {
                        return(new ResponseResult <string>()
                        {
                            ErrCode = 1002,
                            ErrMsg = "Code为空",
                            Data = null
                        });
                    }

                    dynamic result = _weChatServiceHandler.GetOpenId(model.Code);
                    if (result is WeChatOpenId)
                    {
                        var openId      = result as WeChatOpenId;
                        var redisOpenId = _redisHandler.SaveOpenId(openId);
                        return(new ResponseResult <string>()
                        {
                            ErrCode = 0,
                            ErrMsg = "Success",
                            Data = redisOpenId.Token
                        });
                    }
                    else
                    {
                        var error = result as Error;
                        logger.Error(error.errmsg);
                        return(new ResponseResult <string>()
                        {
                            ErrCode = 1001,
                            ErrMsg = "获取OpenId失败",
                            Data = null
                        });
                    }
                }
                else
                {
                    //检查缓存中OpenId是否过期
                    var redisOpenId = _redisHandler.GetSavedOpenId(model.Token);
                    if (redisOpenId == null)
                    {
                        //OpenId已过期,重新调用微信接口登录
                        //检查Code
                        if (string.IsNullOrEmpty(model.Code))
                        {
                            return(new ResponseResult <string>()
                            {
                                ErrCode = 1002,
                                ErrMsg = "Code为空",
                                Data = null
                            });
                        }

                        dynamic result = _weChatServiceHandler.GetOpenId(model.Code);
                        if (result is WeChatOpenId)
                        {
                            var openId           = result as WeChatOpenId;
                            var redisOpenIdSaved = _redisHandler.SaveOpenId(openId);
                            return(new ResponseResult <string>()
                            {
                                ErrCode = 0,
                                ErrMsg = "Success",
                                Data = redisOpenIdSaved.Token
                            });
                        }
                        else
                        {
                            var error = result as Error;
                            logger.Error(error.errmsg);
                            return(new ResponseResult <string>()
                            {
                                ErrCode = 1001,
                                ErrMsg = "获取OpenId失败",
                                Data = null
                            });
                        }
                    }
                    else
                    {
                        //OpenId未过期
                        //返回用户Id
                        return(new ResponseResult <string>()
                        {
                            ErrCode = 0,
                            ErrMsg = "Success",
                            Data = redisOpenId.Token
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return(new ResponseResult <string>()
                {
                    ErrCode = 1003,
                    ErrMsg = ex.Message,
                    Data = null
                });
            }
        }
Ejemplo n.º 4
0
 public async Task <LoginResponse> Login([FromBody] LoginRequest request) =>
 await Task.Run(() => _personService.Login(request));