diff --git a/dsSzxy/.idea/workspace.xml b/dsSzxy/.idea/workspace.xml index 140899be..63afc7e7 100644 --- a/dsSzxy/.idea/workspace.xml +++ b/dsSzxy/.idea/workspace.xml @@ -5,8 +5,10 @@ + + + + + + + + + + @@ -84,6 +94,15 @@ + + + + + + + + + diff --git a/dsSzxy/Business/CaptchaRelate/CaptchaController/CaptchaController.go b/dsSzxy/Business/CaptchaRelate/CaptchaController/CaptchaController.go index 0737249d..71406d74 100644 --- a/dsSzxy/Business/CaptchaRelate/CaptchaController/CaptchaController.go +++ b/dsSzxy/Business/CaptchaRelate/CaptchaController/CaptchaController.go @@ -13,13 +13,16 @@ import ( // Routers 模块的路由配置 func Routers(r *gin.RouterGroup) { rr := r.Group("/captchaRelate") - rr.GET("/GetOne", GetOne) - rr.POST("/Verify", Verify) + rr.GET("/getCaptcha", getCaptcha) + rr.POST("/verifyCaptcha", verifyCaptcha) } // 设置自带的store var store = base64Captcha.DefaultMemStore +//使用redis作为store +//var store = RedisStore{} + // CaptchaResult 存储验证码的结构 type CaptchaResult struct { Id string `json:"id"` @@ -27,7 +30,7 @@ type CaptchaResult struct { } // GetOne 生成图形化验证码 -func GetOne(c *gin.Context) { +func getCaptcha(c *gin.Context) { id, b64s, err := CaptMake() if err != nil { c.JSON(http.StatusOK, gin.H{"success": false, "info": "发生错误!"}) @@ -41,7 +44,7 @@ func GetOne(c *gin.Context) { } // Verify 验证captcha是否正确 -func Verify(c *gin.Context) { +func verifyCaptcha(c *gin.Context) { id := c.PostForm("id") capt := c.PostForm("capt") if id == "" || capt == "" { @@ -76,7 +79,6 @@ func CaptMake() (id, b64s string, err error) { }, Fonts: []string{"wqy-microhei.ttc"}, } - driverString = captchaConfig driver = driverString.ConvertFonts() captcha := base64Captcha.NewCaptcha(driver, store) diff --git a/dsSzxy/Business/CaptchaRelate/CaptchaController/RedisStore.go b/dsSzxy/Business/CaptchaRelate/CaptchaController/RedisStore.go new file mode 100644 index 00000000..98ad8f5e --- /dev/null +++ b/dsSzxy/Business/CaptchaRelate/CaptchaController/RedisStore.go @@ -0,0 +1,42 @@ +package CaptchaController + +import ( + "context" + "dsSzxy/Utils/RedisUtil" + "fmt" + "time" +) + +var ctx = context.Background() + +const CAPTCHA = "captcha:" + +type RedisStore struct { +} + +// Set set a capt +func (r RedisStore) Set(id string, value string) { + key := CAPTCHA + id + RedisUtil.SET(key, value, time.Minute*2) +} + +// Get get a capt +func (r RedisStore) Get(id string, clear bool) string { + key := CAPTCHA + id + + val, err := RedisUtil.GET(key) + if err != nil { + fmt.Println(err) + return "" + } + if clear { + RedisUtil.DEL(key) + } + return val +} + +// Verify verify a capt +func (r RedisStore) Verify(id, answer string, clear bool) bool { + v := RedisStore{}.Get(id, clear) + return v == answer +} diff --git a/dsSzxy/static/getcapt.html b/dsSzxy/static/getcapt.html index 4954dfee..ab23fb80 100644 --- a/dsSzxy/static/getcapt.html +++ b/dsSzxy/static/getcapt.html @@ -14,7 +14,7 @@ //得到图形验证码和id $.ajax({ type: "GET", - url: "/dsSzxy/captchaRelate/GetOne", + url: "/dsSzxy/captchaRelate/getCaptcha", data: {}, dataType: "JSON", success: function (result) { @@ -32,7 +32,7 @@ }; $.ajax({ type: "POST", - url: "/dsSzxy/captchaRelate/Verify", + url: "/dsSzxy/captchaRelate/verifyCaptcha", data: postdata, dataType: "JSON", success: function (result) {