sys_captcha.go 905 B

12345678910111213141516171819202122232425262728293031323334
  1. package v1
  2. import (
  3. "fmt"
  4. "gin-vue-admin/global/response"
  5. resp "gin-vue-admin/model/response"
  6. "github.com/gin-gonic/gin"
  7. "github.com/mojocn/base64Captcha"
  8. )
  9. var store = base64Captcha.DefaultMemStore
  10. // @Tags base
  11. // @Summary 生成验证码
  12. // @Security ApiKeyAuth
  13. // @accept application/json
  14. // @Produce application/json
  15. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  16. // @Router /base/captcha [post]
  17. func Captcha(c *gin.Context) {
  18. //字符,公式,验证码配置
  19. // 生成默认数字的driver
  20. driver := base64Captcha.NewDriverDigit(80, 240, 5, 0.7, 80)
  21. cp := base64Captcha.NewCaptcha(driver, store)
  22. id, b64s, err := cp.Generate()
  23. if err != nil {
  24. response.FailWithMessage(fmt.Sprintf("获取数据失败,%v", err), c)
  25. } else {
  26. response.OkDetailed(resp.SysCaptchaResponse{
  27. CaptchaId: id,
  28. PicPath: b64s,
  29. }, "验证码获取成功", c)
  30. }
  31. }