captcha.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package captcha
  2. import (
  3. "image/color"
  4. "github.com/google/uuid"
  5. "github.com/mojocn/base64Captcha"
  6. )
  7. // SetStore 设置store
  8. func SetStore(s base64Captcha.Store) {
  9. base64Captcha.DefaultMemStore = s
  10. }
  11. //configJsonBody json request body.
  12. type configJsonBody struct {
  13. Id string
  14. CaptchaType string
  15. VerifyValue string
  16. DriverAudio *base64Captcha.DriverAudio
  17. DriverString *base64Captcha.DriverString
  18. DriverChinese *base64Captcha.DriverChinese
  19. DriverMath *base64Captcha.DriverMath
  20. DriverDigit *base64Captcha.DriverDigit
  21. }
  22. func DriverStringFunc() (id, b64s string, err error) {
  23. e := configJsonBody{}
  24. e.Id = uuid.New().String()
  25. e.DriverString = base64Captcha.NewDriverString(46, 140, 2, 2, 4, "234567890abcdefghjkmnpqrstuvwxyz", &color.RGBA{240, 240, 246, 246}, []string{"wqy-microhei.ttc"})
  26. driver := e.DriverString.ConvertFonts()
  27. cap := base64Captcha.NewCaptcha(driver, base64Captcha.DefaultMemStore)
  28. return cap.Generate()
  29. }
  30. func DriverDigitFunc() (id, b64s string, err error) {
  31. e := configJsonBody{}
  32. e.Id = uuid.New().String()
  33. e.DriverDigit = base64Captcha.NewDriverDigit(80, 240, 4, 0.7, 80)
  34. driver := e.DriverDigit
  35. cap := base64Captcha.NewCaptcha(driver, base64Captcha.DefaultMemStore)
  36. return cap.Generate()
  37. }
  38. // Verify 校验验证码
  39. func Verify(id, code string, clear bool) bool {
  40. return base64Captcha.DefaultMemStore.Verify(id, code, clear)
  41. }