验证码存redis

This commit is contained in:
suyl
2021-07-13 18:36:23 +08:00
parent 826975c1b3
commit 7e00b58050
2 changed files with 20 additions and 10 deletions

View File

@@ -7,7 +7,6 @@ import (
"git.rosy.net.cn/jx-print/model"
"git.rosy.net.cn/jx-print/utils"
"github.com/dchest/captcha"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"net/http"
"strings"
@@ -93,10 +92,9 @@ func callFunc(c *gin.Context, worker func() (retVal interface{}, errCode string,
}
func captchaVerify(c *gin.Context, code string) bool {
session := sessions.Default(c)
if captchaId := session.Get(c.ClientIP() + model.SessionKey); captchaId != nil {
session.Delete(c.ClientIP() + model.SessionKey)
_ = session.Save()
//session := sessions.Default(c)
if captchaId := utils.GetKet(c.ClientIP() + model.SessionKey); captchaId != nil {
utils.DelKey(c.ClientIP() + model.SessionKey)
if captcha.VerifyString(captchaId.(string), code) {
return true
} else {
@@ -105,6 +103,17 @@ func captchaVerify(c *gin.Context, code string) bool {
} else {
return false
}
//if captchaId := session.Get(c.ClientIP() + model.SessionKey); captchaId != nil {
// session.Delete(c.ClientIP() + model.SessionKey)
// _ = session.Save()
// if captcha.VerifyString(captchaId.(string), code) {
// return true
// } else {
// return false
// }
//} else {
// return false
//}
}
func buildErrJson(c *gin.Context, err error) {

View File

@@ -8,7 +8,6 @@ import (
"git.rosy.net.cn/jx-print/globals"
"git.rosy.net.cn/jx-print/model"
"github.com/dchest/captcha"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"math/rand"
"net/http"
@@ -65,10 +64,12 @@ func Captcha(c *gin.Context, length ...int) {
h = length[2]
}
captchaId := captcha.NewLen(l)
session := sessions.Default(c)
session.Set(c.ClientIP()+model.SessionKey, captchaId)
_ = session.Save()
_ = Serve(c.Writer, c.Request, captchaId, ".png", "zh", false, w, h)
//session := sessions.Default(c)
//session.Set(c.ClientIP()+model.SessionKey, captchaId)
//_ = session.Save()
if err := SetKey(c.ClientIP()+model.SessionKey, captchaId, time.Minute*5); err == nil {
_ = Serve(c.Writer, c.Request, captchaId, ".png", "zh", false, w, h)
}
}
func Serve(w http.ResponseWriter, r *http.Request, id, ext, lang string, download bool, width, height int) error {