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

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