-
Notifications
You must be signed in to change notification settings - Fork 130
/
captcha.go
50 lines (42 loc) · 1.23 KB
/
captcha.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package system
import (
"context"
"github.com/gogf/gf/v2/text/gstr"
"github.com/sagoo-cloud/sagooiot/internal/service"
"image/color"
"github.com/mojocn/base64Captcha"
)
type sCaptcha struct{}
var (
captchaStore = base64Captcha.DefaultMemStore
captchaDriver = &base64Captcha.DriverString{
Height: 80,
Width: 240,
NoiseCount: 50,
ShowLineOptions: 20,
Length: 4,
Source: "abcdefghjkmnpqrstuvwxyz23456789",
Fonts: []string{"chromohv.ttf"},
BgColor: &color.RGBA{R: 209, G: 205, B: 205, A: 90},
}
)
func init() {
service.RegisterCaptcha(New())
}
// Captcha 验证码管理服务
func New() *sCaptcha {
return &sCaptcha{}
}
// GetVerifyImgString 获取字母数字混合验证码
func (s *sCaptcha) GetVerifyImgString(ctx context.Context) (idKeyC string, base64stringC string, err error) {
driver := captchaDriver.ConvertFonts()
c := base64Captcha.NewCaptcha(driver, captchaStore)
idKeyC, base64stringC, err = c.Generate()
return
}
// VerifyString 验证输入的验证码是否正确
func (s *sCaptcha) VerifyString(id, answer string) bool {
c := base64Captcha.NewCaptcha(captchaDriver, captchaStore)
answer = gstr.ToLower(answer)
return c.Verify(id, answer, true)
}