restructure
This commit is contained in:
@@ -1,20 +1,16 @@
|
||||
package captcha
|
||||
|
||||
import (
|
||||
"atom/container"
|
||||
"atom/providers/config"
|
||||
"errors"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/mojocn/base64Captcha"
|
||||
"github.com/rogeecn/atom/container"
|
||||
"github.com/spf13/viper"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
func init() {
|
||||
if err := container.Container.Provide(NewCaptcha); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
type CaptchaResponse struct {
|
||||
CaptchaId string `json:"captcha_id,omitempty"`
|
||||
PicPath string `json:"pic_path,omitempty"`
|
||||
@@ -23,16 +19,32 @@ type CaptchaResponse struct {
|
||||
}
|
||||
|
||||
type Captcha struct {
|
||||
conf *config.Config
|
||||
captcha *base64Captcha.Captcha
|
||||
}
|
||||
|
||||
func NewCaptcha(conf *config.Config, driver base64Captcha.Driver) (*Captcha, error) {
|
||||
var store = base64Captcha.DefaultMemStore
|
||||
return &Captcha{
|
||||
conf: conf,
|
||||
captcha: base64Captcha.NewCaptcha(driver, store),
|
||||
}, nil
|
||||
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) {
|
||||
@@ -44,8 +56,8 @@ func (c *Captcha) Generate() (*CaptchaResponse, error) {
|
||||
return &CaptchaResponse{
|
||||
CaptchaId: id,
|
||||
PicPath: b64s,
|
||||
CaptchaLength: c.conf.Captcha.KeyLong,
|
||||
OpenCaptcha: c.conf.Captcha.OpenCaptcha,
|
||||
CaptchaLength: viper.GetUint("CAPTCHA_IMG_KEY_LONG"),
|
||||
OpenCaptcha: viper.GetUint("CAPTCHA_IMG_OPEN"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
25
providers/captcha/config.go
Normal file
25
providers/captcha/config.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package captcha
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Long uint // 验证码长度
|
||||
Width uint // 验证码宽度
|
||||
Height uint // 验证码高度
|
||||
Open uint // 防爆破验证码开启此数,0代表每次登录都需要验证码,其他数字代表错误密码此数,如3代表错误三次后出现验证码
|
||||
OpenTimeOut string // 防爆破验证码超时时间,单位:s(秒)
|
||||
MaxScrew float64 // MaxSkew max absolute skew factor of a single digit.
|
||||
DotCount int // Number of background circles.
|
||||
|
||||
}
|
||||
|
||||
func (c *Config) OpenCaptchaTimeOutDuration() time.Duration {
|
||||
d, err := time.ParseDuration(c.OpenTimeOut)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
return d
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"atom/container"
|
||||
"atom/providers/config"
|
||||
"log"
|
||||
|
||||
"github.com/mojocn/base64Captcha"
|
||||
)
|
||||
|
||||
func init() {
|
||||
if err := container.Container.Provide(NewCaptchaDriverDigit); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func NewCaptchaDriverDigit(conf *config.Config) (base64Captcha.Driver, error) {
|
||||
// 字符,公式,验证码配置
|
||||
// 生成默认数字的driver
|
||||
return base64Captcha.NewDriverDigit(
|
||||
int(conf.Captcha.ImgHeight),
|
||||
int(conf.Captcha.ImgWidth),
|
||||
int(conf.Captcha.KeyLong),
|
||||
0.7,
|
||||
80), nil
|
||||
}
|
||||
Reference in New Issue
Block a user