Exemple #1
0
        public SingleApiResponse Login(UserLoginRequest req)
        {
            var user = RepoBase.Instance.GetWhere <SS_USER>(x => x.U_NAME == req.userName).FirstOrDefault();

            if (user != null && user.U_ID > 0)
            {
                string pwd1 = MD5Encrypt.MD5(req.passWord + user.U_ENCRYPT);
                if (!user.U_DISABLED)
                {
                    if (user.U_PWD == pwd1)
                    {
                        //TokenModelJwt tokenModel = new TokenModelJwt { Uid = user.U_ID, Role = user.U_ID.ToString() };
                        var roles = RoleRepo.GetListByUid <SS_ROLE>(new UserInfoGetRequest()
                        {
                            U_ID = user.U_ID
                        }).Select(x => x.R_ID);
                        TokenModelJwt tokenModel = new TokenModelJwt {
                            Uid = user.U_ID, Role = string.Join(",", roles)
                        };
                        var jwtStr = JwtHelper.IssueJWT(tokenModel); //登录,获取到一定规则的 Token 令牌

                        #region 更新user
                        //登录次数
                        user.U_LOGINTIMES++;

                        //上次登录时间和ip
                        user.U_PREVLOGINIP   = user.U_LASTLOGINIP;
                        user.U_PREVLOGINTIME = user.U_UPDATETIME;

                        //本次登录时间和ip
                        user.U_LASTLOGINTIME = DateTime.Now;
                        user.U_UPDATETIME    = DateTime.Now;
                        user.U_LASTLOGINIP   = req.Ip;

                        RepoBase.Instance.Update(user);

                        #endregion

                        return(new SingleApiResponse()
                        {
                            Data = new LoginDto()
                            {
                                U_ID = user.U_ID, Token = $"Bearer {jwtStr}"
                            }
                        });
                    }
                    return(new SingleApiResponse()
                    {
                        ErrCode = 105, BizErrorMsg = "密码错误"
                    });
                }
                return(new SingleApiResponse()
                {
                    ErrCode = 104, BizErrorMsg = "登录系统,该用户状态为禁止登录"
                });
            }
            return(new SingleApiResponse()
            {
                ErrCode = 103, BizErrorMsg = "用户名不存在"
            });
        }