You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.5 KiB

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