修改登录校验和初始化

This commit is contained in:
zhangchenhao
2025-05-16 17:43:30 +08:00
parent a286500c05
commit a4bd40b08e
6 changed files with 25 additions and 24 deletions

View File

@@ -24,7 +24,7 @@ func Sign(c *gin.Context) {
}
form.Username = strings.TrimSpace(form.Username)
form.Code = strings.TrimSpace(form.Code)
// 从数据库拿用户
s, err := public.NewSqlite("data/data.db", "")
if err != nil {
@@ -41,10 +41,10 @@ func Sign(c *gin.Context) {
public.FailMsg(c, err.Error())
return
}
session := sessions.Default(c)
now := time.Now()
loginErrCount := session.Get("__loginErrCount")
loginErrEnd := session.Get("__loginErrEnd")
ErrCount := 0
@@ -57,9 +57,9 @@ func Sign(c *gin.Context) {
if __loginErrEnd, ok := loginErrEnd.(time.Time); ok {
ErrEnd = __loginErrEnd
}
// fmt.Println(ErrCount, ErrEnd)
// 判断登录错误次数
switch {
case ErrCount >= 5:
@@ -91,7 +91,7 @@ func Sign(c *gin.Context) {
}
}
}
// 判断用户是否存在
if len(res) == 0 {
session.Set("__loginErrCount", ErrCount+1)
@@ -117,7 +117,7 @@ func Sign(c *gin.Context) {
keyMd5 := md5.Sum([]byte(passwd))
passwdMd5 := hex.EncodeToString(keyMd5[:])
// fmt.Println(passwdMd5)
if res[0]["password"] != passwdMd5 {
session.Set("__loginErrCount", ErrCount+1)
session.Set("__loginErrEnd", now)
@@ -128,12 +128,12 @@ func Sign(c *gin.Context) {
public.FailMsg(c, "密码错误")
return
}
// session := sessions.Default(c)
session.Set("__loginErrCount", 0)
session.Delete("__loginErrEnd")
session.Set("login", true)
session.Set("__login_key", public.GetSettingIgnoreError("login_key"))
session.Set("__login_key", public.LoginKey)
_ = session.Save()
// c.JSON(http.StatusOK, public.ResOK(0, nil, "登录成功"))
// 设置cookie
@@ -145,7 +145,7 @@ func Sign(c *gin.Context) {
func GetCode(c *gin.Context) {
_, bs64, code, _ := public.GenerateCode()
session := sessions.Default(c)
session.Set("_verifyCode", code)
_ = session.Save()
public.SuccessData(c, bs64, 0)