|
@@ -4,16 +4,20 @@ import (
|
|
|
"context"
|
|
"context"
|
|
|
"github.com/gogf/gf/v2/text/gstr"
|
|
"github.com/gogf/gf/v2/text/gstr"
|
|
|
"github.com/mojocn/base64Captcha"
|
|
"github.com/mojocn/base64Captcha"
|
|
|
- "github.com/tiger1103/gfast/v3/library/liberr"
|
|
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
-type captcha struct{}
|
|
|
|
|
|
|
+type ICaptcha interface {
|
|
|
|
|
+ GetVerifyImgString(ctx context.Context) (idKeyC string, base64stringC string, err error)
|
|
|
|
|
+ VerifyString(id, answer string) bool
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
-var Captcha = new(captcha)
|
|
|
|
|
|
|
+type captchaImpl struct {
|
|
|
|
|
+ driver *base64Captcha.DriverString
|
|
|
|
|
+ store base64Captcha.Store
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
-// GetVerifyImgString 获取字母数字混合验证码
|
|
|
|
|
-func (s *captcha) GetVerifyImgString(ctx context.Context) (idKeyC string, base64stringC string) {
|
|
|
|
|
- driver := &base64Captcha.DriverString{
|
|
|
|
|
|
|
+var Captcha ICaptcha = &captchaImpl{
|
|
|
|
|
+ driver: &base64Captcha.DriverString{
|
|
|
Height: 80,
|
|
Height: 80,
|
|
|
Width: 240,
|
|
Width: 240,
|
|
|
NoiseCount: 50,
|
|
NoiseCount: 50,
|
|
@@ -21,20 +25,21 @@ func (s *captcha) GetVerifyImgString(ctx context.Context) (idKeyC string, base64
|
|
|
Length: 4,
|
|
Length: 4,
|
|
|
Source: "abcdefghjkmnpqrstuvwxyz23456789",
|
|
Source: "abcdefghjkmnpqrstuvwxyz23456789",
|
|
|
Fonts: []string{"chromohv.ttf"},
|
|
Fonts: []string{"chromohv.ttf"},
|
|
|
- }
|
|
|
|
|
- driver = driver.ConvertFonts()
|
|
|
|
|
- store := base64Captcha.DefaultMemStore
|
|
|
|
|
- c := base64Captcha.NewCaptcha(driver, store)
|
|
|
|
|
- idKeyC, base64stringC, err := c.Generate()
|
|
|
|
|
- liberr.ErrIsNil(ctx, err)
|
|
|
|
|
|
|
+ },
|
|
|
|
|
+ store: base64Captcha.DefaultMemStore,
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// GetVerifyImgString 获取字母数字混合验证码
|
|
|
|
|
+func (s *captchaImpl) GetVerifyImgString(ctx context.Context) (idKeyC string, base64stringC string, err error) {
|
|
|
|
|
+ driver := s.driver.ConvertFonts()
|
|
|
|
|
+ c := base64Captcha.NewCaptcha(driver, s.store)
|
|
|
|
|
+ idKeyC, base64stringC, err = c.Generate()
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// VerifyString 验证输入的验证码是否正确
|
|
// VerifyString 验证输入的验证码是否正确
|
|
|
-func (s *captcha) VerifyString(id, answer string) bool {
|
|
|
|
|
- driver := new(base64Captcha.DriverString)
|
|
|
|
|
- store := base64Captcha.DefaultMemStore
|
|
|
|
|
- c := base64Captcha.NewCaptcha(driver, store)
|
|
|
|
|
|
|
+func (s *captchaImpl) VerifyString(id, answer string) bool {
|
|
|
|
|
+ c := base64Captcha.NewCaptcha(s.driver, s.store)
|
|
|
answer = gstr.ToLower(answer)
|
|
answer = gstr.ToLower(answer)
|
|
|
return c.Verify(id, answer, true)
|
|
return c.Verify(id, answer, true)
|
|
|
}
|
|
}
|