captcha.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package service
  2. import (
  3. "github.com/gogf/gf/frame/g"
  4. "github.com/gogf/gf/text/gstr"
  5. "github.com/mojocn/base64Captcha"
  6. )
  7. type captcha struct{}
  8. var Captcha = new(captcha)
  9. //获取字母数字混合验证码
  10. func (s *captcha) GetVerifyImgString() (idKeyC string, base64stringC string) {
  11. driver := &base64Captcha.DriverString{
  12. Height: 80,
  13. Width: 240,
  14. NoiseCount: 50,
  15. ShowLineOptions: 20,
  16. Length: 4,
  17. Source: "abcdefghjkmnpqrstuvwxyz23456789",
  18. Fonts: []string{"chromohv.ttf"},
  19. }
  20. driver = driver.ConvertFonts()
  21. store := base64Captcha.DefaultMemStore
  22. c := base64Captcha.NewCaptcha(driver, store)
  23. idKeyC, base64stringC, err := c.Generate()
  24. if err != nil {
  25. g.Log().Error(err)
  26. }
  27. return
  28. }
  29. //验证输入的验证码是否正确
  30. func (s *captcha) VerifyString(id, answer string) bool {
  31. driver := new(base64Captcha.DriverString)
  32. store := base64Captcha.DefaultMemStore
  33. c := base64Captcha.NewCaptcha(driver, store)
  34. answer = gstr.ToLower(answer)
  35. return c.Verify(id, answer, true)
  36. }