package CaptchaController //https://gitee.com/lemon527/base64Captcha //http://127.0.0.1:8006/dsSzxy/static/getcapt.html import ( "fmt" "github.com/gin-gonic/gin" "github.com/mojocn/base64Captcha" "net/http" ) // Routers 模块的路由配置 func Routers(r *gin.RouterGroup) { rr := r.Group("/captchaRelate") 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"` Base64Blob string `json:"base_64_blob"` } // GetOne 生成图形化验证码 func getCaptcha(c *gin.Context) { id, b64s := CaptMake() captchaResult := CaptchaResult{ Id: id, Base64Blob: b64s, } c.JSON(http.StatusOK, gin.H{"success": true, "captcha": captchaResult}) return } // Verify 验证captcha是否正确 func verifyCaptcha(c *gin.Context) { id := c.PostForm("id") capt := c.PostForm("capt") if id == "" || capt == "" { c.JSON(http.StatusOK, gin.H{"success": false, "msg": "参数错误!"}) } if CaptVerify(id, capt) == true { c.JSON(http.StatusOK, gin.H{"success": true}) } else { c.JSON(http.StatusOK, gin.H{"success": false}) } return } // CaptMake 生成验证码 func CaptMake() (string, string) { // 生成默认数字 driver := &base64Captcha.DriverDigit{ Height: 100, Width: 200, Length: 5, MaxSkew: 1, DotCount: 1, } // 生成base64图片 c := base64Captcha.NewCaptcha(driver, store) // 获取 id, b64s, err := c.Generate() if err != nil { fmt.Println("Register GetCaptchaPhoto get base64Captcha has err:", err) return "", "" } return id, b64s } // CaptVerify 验证captcha是否正确 func CaptVerify(id string, capt string) bool { fmt.Println("id:" + id) fmt.Println("capt:" + capt) return store.Verify(id, capt, false) }