sys_captcha.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. var store = base64Captcha.DefaultMemStore
  11. type BaseApi struct {
  12. }
  13. // @Tags Base
  14. // @Summary 生成验证码
  15. // @Security ApiKeyAuth
  16. // @accept application/json
  17. // @Produce application/json
  18. // @Success 200 {string} string "{"success":true,"data":{},"msg":"验证码获取成功"}"
  19. // @Router /base/captcha [post]
  20. func (b *BaseApi) Captcha(c *gin.Context) {
  21. // 字符,公式,验证码配置
  22. // 生成默认数字的driver
  23. driver := base64Captcha.NewDriverDigit(global.GVA_CONFIG.Captcha.ImgHeight, global.GVA_CONFIG.Captcha.ImgWidth, global.GVA_CONFIG.Captcha.KeyLong, 0.7, 80)
  24. cp := base64Captcha.NewCaptcha(driver, store)
  25. if id, b64s, err := cp.Generate(); err != nil {
  26. global.GVA_LOG.Error("验证码获取失败!", zap.Any("err", err))
  27. response.FailWithMessage("验证码获取失败", c)
  28. } else {
  29. response.OkWithDetailed(systemRes.SysCaptchaResponse{
  30. CaptchaId: id,
  31. PicPath: b64s,
  32. }, "验证码获取成功", c)
  33. }
  34. }