Files
atom/providers/captcha/captcha.go
2023-04-20 12:11:34 +08:00

67 lines
1.5 KiB
Go

package captcha
import (
"errors"
"log"
"time"
"github.com/mojocn/base64Captcha"
"github.com/rogeecn/atom/container"
"github.com/spf13/viper"
"go.uber.org/dig"
)
type CaptchaResponse struct {
CaptchaId string `json:"captcha_id,omitempty"`
PicPath string `json:"pic_path,omitempty"`
CaptchaLength uint `json:"captcha_length,omitempty"`
OpenCaptcha uint `json:"open_captcha,omitempty"`
}
type Captcha struct {
captcha *base64Captcha.Captcha
}
func Provide(conf *Config, opts ...dig.ProvideOption) error {
return container.Container.Provide(func() (*Captcha, error) {
driver := base64Captcha.NewDriverDigit(
int(conf.Width),
int(conf.Height),
int(conf.Long),
conf.MaxScrew,
conf.DotCount,
)
store := base64Captcha.DefaultMemStore
return &Captcha{
captcha: base64Captcha.NewCaptcha(driver, store),
}, nil
})
}
func (c *Captcha) OpenCaptchaTimeOutDuration() time.Duration {
d, err := time.ParseDuration(viper.GetString("CAPTCHA_IMG_OPEN_TIMEOUT"))
if err != nil {
log.Panic(err)
}
return d
}
func (c *Captcha) Generate() (*CaptchaResponse, error) {
id, b64s, err := c.captcha.Generate()
if err != nil {
return nil, errors.New("验证码获取失败")
}
return &CaptchaResponse{
CaptchaId: id,
PicPath: b64s,
CaptchaLength: viper.GetUint("CAPTCHA_IMG_KEY_LONG"),
OpenCaptcha: viper.GetUint("CAPTCHA_IMG_OPEN"),
}, nil
}
func (c *Captcha) Verify(id, answer string) bool {
return c.captcha.Verify(id, answer, false)
}