Commit 75117b65 authored by 黄智's avatar 黄智

续期和系统首选项调整

parent c8678c48
...@@ -14,41 +14,6 @@ import ( ...@@ -14,41 +14,6 @@ import (
"time" "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) { func UserLoginV2(c *gin.Context) {
userReq := request.UserReq{} userReq := request.UserReq{}
err := c.BindJSON(&userReq) err := c.BindJSON(&userReq)
......
package service package service
import ( import (
"crypto/md5"
"encoding/hex"
"errors" "errors"
"fmt" "fmt"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" "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 ...@@ -58,143 +56,6 @@ func (u *UserSvc) GetUserInfo(phone string) (userInfo entity.SystemUserInfo, err
return userInfo, nil 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) { func (u *UserSvc) GetCurUser(token string) (m entity.SystemUserInfo, err error) {
rcon, err := client.GetRedisClient() rcon, err := client.GetRedisClient()
if err != nil { if err != nil {
...@@ -219,6 +80,14 @@ func (u *UserSvc) GetCurUser(token string) (m entity.SystemUserInfo, err error) ...@@ -219,6 +80,14 @@ func (u *UserSvc) GetCurUser(token string) (m entity.SystemUserInfo, err error)
return m, resp.FAIL.WithError(err) return m, resp.FAIL.WithError(err)
} }
m.Password = "" m.Password = ""
op := SystemOptionsSvc{}
config, err := op.GetSystemOptions()
if config.SessionValidity > 0 {
expireTime := time.Duration(config.SessionValidity)
_, _ = rcon.Expire(token, expireTime*time.Minute)
}
return return
} }
......
...@@ -2,6 +2,8 @@ package service ...@@ -2,6 +2,8 @@ package service
import ( import (
"errors" "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/entity"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client"
...@@ -19,16 +21,41 @@ type SystemOptionsSvc struct { ...@@ -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() db, err := client.GetDbClient()
if err != nil { if err != nil {
return nil, resp.DbConnectError.WithError(err) return nil, resp.DbConnectError.WithError(err)
} }
var systemOpts entity.SystemPreferenceConfig
if _, err := db.Table("system_preference_config").Get(&systemOpts); err != nil { if _, err := db.Table("system_preference_config").Get(&systemOpts); err != nil {
conf.Logger.Error("获取系统首选项配置失败", zap.Error(err)) conf.Logger.Error("获取系统首选项配置失败", zap.Error(err))
return nil, resp.DbSelectError.WithError(err) return nil, resp.DbSelectError.WithError(err)
} }
bytes, _ := json.Marshal(systemOpts)
_ = rConn.Set("SYSTEMOPTIONS", bytes, 1*time.Minute)
return &systemOpts, nil return &systemOpts, nil
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment