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.

40 lines
1.3 KiB

  1. package system
  2. import (
  3. "gin-vue-admin/global"
  4. "gin-vue-admin/model/common/response"
  5. systemRes "gin-vue-admin/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)
  27. if id, b64s, err := cp.Generate(); err != nil {
  28. global.GVA_LOG.Error("验证码获取失败!", zap.Any("err", err))
  29. response.FailWithMessage("验证码获取失败", c)
  30. } else {
  31. response.OkWithDetailed(systemRes.SysCaptchaResponse{
  32. CaptchaId: id,
  33. PicPath: b64s,
  34. }, "验证码获取成功", c)
  35. }
  36. }