37 lines
1.1 KiB
Go
37 lines
1.1 KiB
Go
package common
|
|
|
|
import (
|
|
"github.com/mojocn/base64Captcha"
|
|
)
|
|
|
|
// CaptchaBiz 定义处理验证码请求所需的方法.
|
|
type CaptchaBiz interface {
|
|
Generate(height int, width int, length int, maxSkew float64, dotCount int) (id, b64s, answer string, err error)
|
|
Verify(id, answer string, clear bool) bool
|
|
}
|
|
|
|
// captchaBiz 是 CaptchaBiz 接口的实现.
|
|
type captchaBiz struct{}
|
|
|
|
// 确保 captchaBiz 实现了 CaptchaBiz 接口.
|
|
var _ CaptchaBiz = (*captchaBiz)(nil)
|
|
|
|
func NewCaptcha() *captchaBiz {
|
|
return &captchaBiz{}
|
|
}
|
|
|
|
var captchaStore base64Captcha.Store = base64Captcha.DefaultMemStore
|
|
|
|
func (b *captchaBiz) Generate(height int, width int, length int, maxSkew float64, dotCount int) (id, b64s, answer string, err error) {
|
|
driver := base64Captcha.NewDriverDigit(height, width, length, maxSkew, dotCount)
|
|
// driver := base64Captcha.NewDriverString(config.File.Captcha.ImgHeight,
|
|
// config.File.Captcha.ImgWidth,
|
|
// 6, 1, keyLong, source, nil, nil, nil)
|
|
cp := base64Captcha.NewCaptcha(driver, captchaStore)
|
|
return cp.Generate()
|
|
}
|
|
|
|
func (b *captchaBiz) Verify(id, answer string, clear bool) bool {
|
|
return captchaStore.Verify(id, answer, clear)
|
|
}
|