diff --git a/src/controller/login.go b/src/controller/login.go index a9a8909c7019dab6c35dc00a1e76341c4009e8a1..0aaed8abb71a909116110dd29b802847f2aa45de 100644 --- a/src/controller/login.go +++ b/src/controller/login.go @@ -14,41 +14,6 @@ import ( "time" ) -func UserLogin(c *gin.Context) { - userReq := request.UserReq{} - err := c.BindJSON(&userReq) - if err != nil { - SendJsonResponse(c, resp.InvalidParam.WithError(err), nil) - return - } - //参数检测 - if err := vd.Validate(userReq); err != nil { - SendJsonResponse(c, resp.InvalidParam.WithError(err), "") - return - } - // 验证 userName 是否含有危险字符 - if util.IfDangerCharacter(userReq.SystemAccount) { - SendJsonResponse(c, resp.InvalidParam.WithMsg("账号存在危险字符"), "") - return - } - //TODO 解密password - pwd, err := util.DecryptPwd(userReq.Password) - if err != nil { - SendJsonResponse(c, resp.FAIL.WithError(err), "") - return - } - userReq.Password = pwd - loginInf := service.UserSvc{SystemAccount: userReq.SystemAccount, PassWord: userReq.Password} - msg, uuidStr, lastLogin, err := loginInf.Login() - if err != nil { - SendJsonResponse(c, err, "") - return - } - c.SetCookie(conf.CookieName, uuidStr, 1*60*60*24, "/", "", false, false) - c.SetCookie(conf.CookieNameLastLogin, lastLogin, 1*60*60*24, "/", "", false, false) - SendJsonResponse(c, resp.OK, msg) -} - func UserLoginV2(c *gin.Context) { userReq := request.UserReq{} err := c.BindJSON(&userReq) diff --git a/src/service/login.go b/src/service/login.go index 1402303387036f5919d2eb63607b29703159d5c0..3b8dbcf3b664d8654cc4e7505e2fc66f77a413d7 100644 --- a/src/service/login.go +++ b/src/service/login.go @@ -1,8 +1,6 @@ package service import ( - "crypto/md5" - "encoding/hex" "errors" "fmt" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" @@ -58,143 +56,6 @@ func (u *UserSvc) GetUserInfo(phone string) (userInfo entity.SystemUserInfo, err return userInfo, nil } -func (u *UserSvc) Login() (msg string, uuidStr string, last_login string, err error) { - db, err := client.GetDbClient() - if err != nil { - return "", "", "", resp.DbConnectError.WithError(err) - } - // 查询用户 - var userInfo entity.SystemUserInfo - _, err = db.Table("system_user"). - Select("system_user.*"). - Where("system_account = ?", u.SystemAccount).Get(&userInfo) - if err != nil { - return - } - if userInfo.Id == 0 { - conf.Logger.Error("用户名或密码错误", zap.Error(err)) - return "", "", "", resp.FAIL.WithMsg("用户名或密码错误") - } - - if userInfo.State == 0 { - conf.Logger.Error("账号未启用", zap.Error(err)) - return "", "", "", resp.FAIL.WithMsg("账号未启用") - } - redisCli, err := client.GetRedisClient() - if err != nil { - return "", "", "", resp.RedisConnectError.WithError(err) - } - lockKey := fmt.Sprintf("so-operation-user-lock-%v", userInfo.Id) - haslock, err := redisCli.Get(lockKey) - - if err != nil && err != redis.Nil { - conf.Logger.Error("获取密码插入次数失败", zap.Error(err)) - return "", "", "", resp.DbSelectError.WithError(err) - } else if haslock == "" { - if err := redisCli.Set(lockKey, 0, conf.LockDuration); err != nil { - conf.Logger.Error("密码插入次数插入redis失败", zap.Error(err)) - return "", "", "", resp.DbInsertError.WithError(err) - } - } else if cast.ToInt(haslock) >= conf.LockErrorNumber { - if ttl, err := redisCli.Ttl(lockKey); err != nil { - msg = "错误次数达到上限,请稍后重试" - } else { - if ttl.Seconds() <= 0 { - if err := redisCli.Del(lockKey); err != nil { - msg = "删除错误次数错误,请稍后重试" - } - ttl = time.Second - } - ttl := int(ttl.Seconds()) - if ttl >= 3600 { - msg = fmt.Sprintf("错误次数达到上限,请%d小时后重试", ttl/3600) - } else if ttl >= 60 && ttl < 3600 { - msg = fmt.Sprintf("错误次数达到上限,请%d分钟后重试", ttl/60) - } else { - msg = fmt.Sprintf("错误次数达到上限,请%d秒后重试", ttl) - } - } - err = errors.New(msg) - if err != nil { - conf.Logger.Error(msg, zap.Error(err)) - return "", "", "", resp.RedisExecError.WithError(err) - } - } - - h := md5.New() - _, err = h.Write([]byte(strings.ToUpper(fmt.Sprintf("%d-%s", userInfo.Id, u.PassWord)))) - if err != nil { - conf.Logger.Error("加密错误", zap.Error(err)) - return "", "", "", resp.FAIL.WithError(err) - } - uppperMd5Pass := strings.ToUpper(hex.EncodeToString(h.Sum(nil))) - if uppperMd5Pass != userInfo.Password { - incr, err := redisCli.Incr(lockKey) - if err != nil { - conf.Logger.Error("写入错误次数失败", zap.Error(err)) - return "", "", "", resp.RedisExecError.WithError(err) - } - conf.Logger.Info("当前错误次数为", zap.Int64("incr", incr)) - //再次错误执行续期 - expire, err := redisCli.Expire(lockKey, conf.LockDuration) - if err != nil { - conf.Logger.Error("错误次数续期错误", zap.Error(err)) - return "", "", "", resp.RedisExecError.WithError(err) - } - conf.Logger.Info("续期结果为", zap.Bool("expire", expire)) - return "", "", "", resp.FAIL.WithMsg("用户名或密码错误") - } - //密码正确 删除 锁定文件 - if err := redisCli.Del(lockKey); err != nil { - conf.Logger.Error("删除锁定文件错误", zap.Error(err)) - return "", "", "", resp.RedisExecError.WithError(err) - } - uu := uuid.NewV4() - uuidStr = uu.String() - // 存入redis - b, err := json.Marshal(userInfo) - if err != nil { - return "", "", "", resp.FAIL.WithError(err) - } - err = redisCli.Set(uuidStr, string(b), time.Minute*60*24) - if err != nil { - conf.Logger.Error("登录失败", zap.Error(err)) - return "", "", "", resp.FAIL.WithError(err) - } - msg = "登录成功" - conf.Logger.Info("登录成功", zap.String("msg", msg)) - //登录时间计录在map - loginMap := make(map[string]string, 0) - cont, _ := redisCli.Get("LOGIN-TIME") - //if err != nil { - // conf.Logger.Error("获取登录时间失败", zap.Error(err)) - // return "", "", "", res.DataFailError.ErrorDetail(err) - //} - if cont != "" { - err = json.Unmarshal([]byte(cont), &loginMap) - if err != nil { - return "", "", "", resp.FAIL.WithError(err) - } - } - now := time.Now().Format(conf.LocalDateTimeFormat) - if _, ok := loginMap[u.SystemAccount]; ok { - last_login = loginMap[u.SystemAccount] - } else { - last_login = now - } - loginMap[u.SystemAccount] = now - a, err := json.Marshal(loginMap) - if err != nil { - return "", "", "", resp.FAIL.WithError(err) - } - err = redisCli.Set("LOGIN-TIME", string(a), -1) - if err != nil { - conf.Logger.Error("登录失败", zap.Error(err)) - return "", "", "", resp.FAIL.WithError(err) - } - return -} - func (u *UserSvc) GetCurUser(token string) (m entity.SystemUserInfo, err error) { rcon, err := client.GetRedisClient() if err != nil { @@ -219,6 +80,14 @@ func (u *UserSvc) GetCurUser(token string) (m entity.SystemUserInfo, err error) return m, resp.FAIL.WithError(err) } m.Password = "" + + op := SystemOptionsSvc{} + config, err := op.GetSystemOptions() + if config.SessionValidity > 0 { + expireTime := time.Duration(config.SessionValidity) + _, _ = rcon.Expire(token, expireTime*time.Minute) + } + return } diff --git a/src/service/system_preference.go b/src/service/system_preference.go index 884e216fecced11f9df64b82887b26847cd6c43b..6dcd6b1228c9b023918398b3dca298bd0e11c2f4 100644 --- a/src/service/system_preference.go +++ b/src/service/system_preference.go @@ -2,6 +2,8 @@ package service import ( "errors" + "github.com/go-redis/redis" + json "github.com/json-iterator/go" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client" @@ -19,16 +21,41 @@ type SystemOptionsSvc struct { } // 获取系统首选项 -func (so *SystemOptionsSvc) GetSystemOptions() (config *entity.SystemPreferenceConfig, err error) { +func (so *SystemOptionsSvc) GetSystemOptions() (*entity.SystemPreferenceConfig, error) { + var systemOpts entity.SystemPreferenceConfig + rConn, err := client.GetRedisClient() + if err != nil { + return nil, resp.RedisConnectError.WithError(err) + } + + str, err := rConn.Get("SYSTEMOPTIONS") + if err != nil && err != redis.Nil { + return nil, resp.FAIL.WithError(err) + } + + if str != "" { + err = json.Unmarshal([]byte(str), &systemOpts) + if err != nil { + return nil, resp.FAIL.WithError(err) + } + + if systemOpts.Id != 0 { + return &systemOpts, nil + } + } + db, err := client.GetDbClient() if err != nil { return nil, resp.DbConnectError.WithError(err) } - var systemOpts entity.SystemPreferenceConfig + if _, err := db.Table("system_preference_config").Get(&systemOpts); err != nil { conf.Logger.Error("获取系统首选项配置失败", zap.Error(err)) return nil, resp.DbSelectError.WithError(err) } + + bytes, _ := json.Marshal(systemOpts) + _ = rConn.Set("SYSTEMOPTIONS", bytes, 1*time.Minute) return &systemOpts, nil }