Skip to content

Commit

Permalink
feat: add disable random verify length
Browse files Browse the repository at this point in the history
  • Loading branch information
wenlng committed Aug 22, 2024
1 parent 7094bf6 commit 60487af
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func main() {

- click.WithThumbImageSize(option.Size)
- click.WithRangeVerifyLen(option.RangeVal)
- click.WithDisabledRangeVerifyLen(bool)
- click.WithRangeThumbSize(option.RangeVal)
- click.WithRangeThumbColors([]string)
- click.WithRangeThumbBgColors([]string)
Expand Down
1 change: 1 addition & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func main() {

- click.WithThumbImageSize(option.Size) 设置缩略尺寸,默认 150x40
- click.WithRangeVerifyLen(option.RangeVal) 设置校验内容的随机长度范围
- click.WithDisabledRangeVerifyLen(bool) 禁用校验内容的随机长度,与主图内容的长度保持一致
- click.WithRangeThumbSize(option.RangeVal) 设置随机缩略内容随机大小范围
- click.WithRangeThumbColors([]string) 设置缩略随机颜色范围
- click.WithRangeThumbBgColors([]string) 设置缩略随机背景颜色范围
Expand Down
5 changes: 3 additions & 2 deletions v2/click/click.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,10 @@ func (c *captcha) rangeCheckDots(dots map[int]*Dot) (map[int]*Dot, []string) {
count := random.RandInt(c.opts.rangeVerifyLen.Min, c.opts.rangeVerifyLen.Max)
var values []string
for i, value := range rs {
if i >= count {
continue
if !c.opts.disabledRangeVerifyLen && i >= count {
break
}

dot := dots[value]
dot.Index = i
chkDots[i] = dot
Expand Down
1 change: 1 addition & 0 deletions v2/click/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func defaultOptions() Option {
opts.imageAlpha = 1

opts.rangeVerifyLen = &option.RangeVal{Min: 2, Max: 4}
opts.disabledRangeVerifyLen = false
opts.thumbImageSize = &option.Size{Width: 150, Height: 40}
opts.rangeThumbSize = &option.RangeVal{Min: 22, Max: 28}
opts.rangeThumbColors = getDefaultThumbColors()
Expand Down
13 changes: 13 additions & 0 deletions v2/click/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Options struct {

thumbImageSize *option.Size
rangeVerifyLen *option.RangeVal
disabledRangeVerifyLen bool
rangeThumbSize *option.RangeVal
rangeThumbColors []string
rangeThumbBgColors []string
Expand Down Expand Up @@ -123,6 +124,11 @@ func (o *Options) GetRangeVerifyLen() *option.RangeVal {
}
}

// GetDisabledRangeVerifyLen .
func (o *Options) GetDisabledRangeVerifyLen() bool {
return o.disabledRangeVerifyLen
}

// GetRangeThumbSize .
func (o *Options) GetRangeThumbSize() *option.RangeVal {
return &option.RangeVal{
Expand Down Expand Up @@ -292,6 +298,13 @@ func WithRangeVerifyLen(val option.RangeVal) Option {
}
}

// WithDisabledRangeVerifyLen .
func WithDisabledRangeVerifyLen(disabled bool) Option {
return func(opts *Options) {
opts.disabledRangeVerifyLen = disabled
}
}

// WithRangeThumbSize .
func WithRangeThumbSize(val option.RangeVal) Option {
return func(opts *Options) {
Expand Down
4 changes: 3 additions & 1 deletion v2/tests/click_text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func init() {
builder := click.NewBuilder(
click.WithRangeLen(option.RangeVal{Min: 4, Max: 6}),
click.WithRangeVerifyLen(option.RangeVal{Min: 2, Max: 4}),
click.WithDisabledRangeVerifyLen(true),
)

fontN, err := loadFont("../.cache/fzshengsksjw_cu.ttf")
Expand All @@ -31,7 +32,8 @@ func init() {
}

builder.SetResources(
click.WithChars([]string{"这", "是", "随", "机", "的", "文", "本", "种", "子", "呀"}),
//click.WithChars([]string{"这", "是", "随", "机", "的", "文", "本", "种", "子", "呀"}),
click.WithChars([]string{"A1", "B2", "C3", "D4", "E5", "F6", "G7", "H8", "I9", "J0"}),
click.WithFonts([]*truetype.Font{
fontN,
}),
Expand Down

0 comments on commit 60487af

Please sign in to comment.