sys_captcha.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package v1
  2. import (
  3. "gin-vue-admin/global"
  4. "gin-vue-admin/model/response"
  5. "github.com/gin-gonic/gin"
  6. "github.com/mojocn/base64Captcha"
  7. "go.uber.org/zap"
  8. )
  9. // 当开启多服务器部署时,替换下面的配置,使用redis共享存储验证码
  10. // var store = captcha.NewDefaultRedisStore()
  11. var store = base64Captcha.DefaultMemStore
  12. // @Tags Base
  13. // @Summary 生成验证码
  14. // @Security ApiKeyAuth
  15. // @accept application/json
  16. // @Produce application/json
  17. // @Success 200 {string} string "{"success":true,"data":{},"msg":"验证码获取成功"}"
  18. // @Router /base/captcha [post]
  19. func Captcha(c *gin.Context) {
  20. // 字符,公式,验证码配置
  21. // 生成默认数字的driver
  22. driver := base64Captcha.NewDriverDigit(global.GVA_CONFIG.Captcha.ImgHeight, global.GVA_CONFIG.Captcha.ImgWidth, global.GVA_CONFIG.Captcha.KeyLong, 0.7, 80)
  23. cp := base64Captcha.NewCaptcha(driver, store)
  24. if id, b64s, err := cp.Generate(); err != nil {
  25. global.GVA_LOG.Error("验证码获取失败!", zap.Any("err", err))
  26. response.FailWithMessage("验证码获取失败", c)
  27. } else {
  28. response.OkWithDetailed(response.SysCaptchaResponse{
  29. CaptchaId: id,
  30. PicPath: b64s,
  31. }, "验证码获取成功", c)
  32. }
  33. }