From 870759fcbd8409116feec1802cc1d4937de8dfe0 Mon Sep 17 00:00:00 2001 From: Awen Date: Sat, 22 Jun 2024 18:37:42 +0800 Subject: [PATCH] feat: added builder mode to generate instances --- README.md | 48 +++++++++++++++----------- README_zh.md | 46 ++++++++++++++----------- v2/capts.go | 28 +++++---------- v2/click/builder.go | 67 ++++++++++++++++++++++++++++++++++++ v2/click/click.go | 34 +++++++----------- v2/click/default.go | 14 ++++---- v2/rotate/builder.go | 58 +++++++++++++++++++++++++++++++ v2/rotate/rotate.go | 28 +++++++-------- v2/slide/builder.go | 67 ++++++++++++++++++++++++++++++++++++ v2/slide/slide.go | 36 +++++++------------ v2/tests/click_shape_test.go | 6 ++-- v2/tests/click_text_test.go | 6 ++-- v2/tests/rotate_test.go | 6 ++-- v2/tests/slide_tile_test.go | 10 +++--- 14 files changed, 319 insertions(+), 135 deletions(-) create mode 100644 v2/click/builder.go create mode 100644 v2/rotate/builder.go create mode 100644 v2/slide/builder.go diff --git a/README.md b/README.md index 95c8582..88b6055 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ ## Install Captcha Module ```shell -$ go get -u github.com/wenlng/go-captcha/v2@v2.0.0-beta.2 +$ go get -u github.com/wenlng/go-captcha/v2@latest ``` ## Import Captcha Module @@ -78,7 +78,7 @@ import ( var textCapt click.Captcha func init() { - textCapt = click.New( + builder := click.NewBuilder( click.WithRangeLen(option.RangeVal{Min: 4, Max: 6}), click.WithRangeVerifyLen(option.RangeVal{Min: 2, Max: 4}), ) @@ -93,7 +93,7 @@ func init() { log.Fatalln(err) } - textCapt.SetResources( + builder.SetResources( click.WithChars([]string{ "1A", "5E", @@ -112,6 +112,8 @@ func init() { bgImage, }), ) + + textCapt= builder.Make() } func loadPng(p string) (image.Image, error) { @@ -157,11 +159,11 @@ func main() { ``` ### Create instance method -- click.New() -- click.NewWithShape() +- builder.Make() +- builder.MakeWithShape() ### Configuration options -click.New(click.WithXxx(), ...) +> click.NewBuilder(click.WithXxx(), ...) OR builder.SetOptions()(click.WithXxx(), ...) - click.WithImageSize(option.Size) - click.WithRangeLen(option.RangeVal) - click.WithRangeAnglePos([]option.RangeVal) @@ -185,7 +187,7 @@ click.New(click.WithXxx(), ...) ### Set resources -xxx.SetResources(click.WithXxx(), ...) +> builder.SetResources(click.WithXxx(), ...) - click.WithChars([]string) - click.WithShapes(map[string]image.Image) - click.WithFonts([]*truetype.Font) @@ -219,7 +221,7 @@ import ( var slideTileCapt slide.Captcha func init() { - slideTileCapt = slide.New() + builder := slide.NewBuilder() bgImage, err := loadPng("../resources/bg.png") if err != nil { @@ -233,13 +235,15 @@ func init() { graphs := getSlideTileGraphArr() - slideTileCapt.SetResources( + builder.SetResources( slide.WithGraphImages(graphs), slide.WithBackgrounds([]image.Image{ bgImage, bgImage1, }), ) + + slideTileCapt = builder.Make() } func getSlideTileGraphArr() []*slide.GraphImage { @@ -301,12 +305,12 @@ func loadPng(p string) (image.Image, error) { ### Create instance method -- slide.New() -- slide.NewWithRegion() +- builder.Make() +- builder.MakeWithRegion() ### Configuration options -slide.New(slide.WithXxx(), ...) +> slide.NewBuilder(slide.WithXxx(), ...) OR builder.SetOptions(slide.WithXxx(), ...) - slide.WithImageSize(*option.Size) - slide.WithImageAlpha(float32) - slide.WithRangeGraphSize(val option.RangeVal) @@ -317,7 +321,7 @@ slide.New(slide.WithXxx(), ...) ### Set resources -xxx.SetResources(slide.WithXxx(), ...) +builder.SetResources(slide.WithXxx(), ...) - slide.WithBackgrounds([]image.Image) - slide.WithGraphImages(images []*GraphImage) @@ -348,7 +352,7 @@ import ( var rotateCapt rotate.Captcha func init() { - rotateCapt = rotate.New() + builder := rotate.NewBuilder() bgImage, err := loadPng("../resources/bg.png") if err != nil { @@ -360,12 +364,14 @@ func init() { log.Fatalln(err) } - rotateCapt.SetResources( + builder.SetResources( rotate.WithImages([]image.Image{ bgImage, bgImage1, }), ) + + rotateCapt = builder.Make() } func main() { @@ -403,11 +409,11 @@ func loadPng(p string) (image.Image, error) { ### Create instance method -- rotate.New() +- builder.Make() ### Configuration options -rotate.New(rotate.WithXxx(), ...) +> rotate.NewBuilder(rotate.WithXxx(), ...) OR builder.SetOptions(rotate.WithXxx(), ...) - rotate.WithImageSquareSize(val int) - rotate.WithRangeAnglePos(vals []option.RangeVal) - rotate.WithRangeThumbImageSquareSize(val []int) @@ -415,7 +421,7 @@ rotate.New(rotate.WithXxx(), ...) ### Set resources -xxx.SetResources(rotate.WithXxx(), ...) +builder.SetResources(rotate.WithXxx(), ...) - rotate.WithBackgrounds([]image.Image) ### Captcha Data @@ -447,9 +453,9 @@ xxx.SetResources(rotate.WithXxx(), ...) -

Web Native ✔

-

Vue ✔

-

React ✔

--

Angular

--

Svelte

--

Solid

+-

Angular ✔

+-

Svelte ✔

+-

Solid ✔

-

MinProgram

-

UniApp

-

Android App

diff --git a/README_zh.md b/README_zh.md index 857e17b..57cce4b 100644 --- a/README_zh.md +++ b/README_zh.md @@ -15,7 +15,7 @@ > [English](README.md) | 中文

-Go Captcha 是一个简洁易用、交互友好、高安全的行为式验证码库 (Golang),支持文本点/图形点选、滑动/拖拽、旋转等验证模式。 +Go Captcha 是一个简洁易用、交互友好、高安全的行为式验证码库 (Golang),支持文本/图形点选、滑动/拖拽、旋转等验证模式。

⭐️ 如果能帮助到你,请随手给点一个star

@@ -37,7 +37,7 @@ - Angular Package:[https://github.com/wenlng/go-captcha-angular](https://github.com/wenlng/go-captcha-angular) - Svelte Package:[https://github.com/wenlng/go-captcha-svelte](https://github.com/wenlng/go-captcha-svelte) - Solid Package:[https://github.com/wenlng/go-captcha-solid](https://github.com/wenlng/go-captcha-solid) -- 在线演示:[http://gocaptcha.wencodes.com/demo](http://gocaptcha.wencodes.com/demo) +- 在线演示:[http://gocaptcha.wencodes.com/demo/](http://gocaptcha.wencodes.com/demo/) - ...
@@ -65,7 +65,7 @@ $ source ~/.profile ## 安装模块 ```shell -$ go get -u github.com/wenlng/go-captcha/v2@v2.0.0-beta.2 +$ go get -u github.com/wenlng/go-captcha/v2@latest ``` ## 引入模块 @@ -103,7 +103,7 @@ import ( var textCapt click.Captcha func init() { - textCapt = click.New( + builder := click.NewBuilder( click.WithRangeLen(option.RangeVal{Min: 4, Max: 6}), click.WithRangeVerifyLen(option.RangeVal{Min: 2, Max: 4}), ) @@ -118,7 +118,7 @@ func init() { log.Fatalln(err) } - textCapt.SetResources( + builder.SetResources( click.WithChars([]string{"这", "是", "随", "机", "的", "文", "本", "种", "子", "呀"}), click.WithFonts([]*truetype.Font{ fontN, @@ -127,6 +127,8 @@ func init() { bgImage, }), ) + + textCapt = builder.Make() } func loadPng(p string) (image.Image, error) { @@ -172,11 +174,11 @@ func main() { ``` ### 创建实例方法 -- click.New() 中文文本、字母数字混合点选 -- click.NewWithShape() 图形点选 +- builder.Make() 中文文本、字母数字混合点选 +- builder.MakeWithShape() 图形点选 ### 配置选项 -click.New(click.WithXxx(), ...) +> click.NewBuilder(click.WithXxx(), ...) 或 builder.SetOptions(click.WithXxx(), ...) - click.WithImageSize(option.Size) 设置主图尺寸,默认 300x220 - click.WithRangeLen(option.RangeVal) 设置随机内容长度范围 - click.WithRangeAnglePos([]option.RangeVal) 设置随机角度范围 @@ -200,7 +202,7 @@ click.New(click.WithXxx(), ...) ### 设置资源 -xxx.SetResources(click.WithXxx(), ...) +> builder.SetResources(click.WithXxx(), ...) - click.WithChars([]string) 设置文本种子 - click.WithShapes(map[string]image.Image) 设置图形种子 - click.WithFonts([]*truetype.Font) 设置字体 @@ -234,7 +236,7 @@ import ( var slideTileCapt slide.Captcha func init() { - slideTileCapt = slide.New() + builder := slide.NewBuilder() bgImage, err := loadPng("../resources/bg.png") if err != nil { @@ -248,13 +250,15 @@ func init() { graphs := getSlideTileGraphArr() - slideTileCapt.SetResources( + builder.SetResources( slide.WithGraphImages(graphs), slide.WithBackgrounds([]image.Image{ bgImage, bgImage1, }), ) + + slideTileCapt = builder.Make() } func getSlideTileGraphArr() []*slide.GraphImage { @@ -316,12 +320,12 @@ func loadPng(p string) (image.Image, error) { ### 创建实例方法 -- slide.New() 滑动式 -- slide.NewWithRegion() 区域内拖拽滑动式 +- builder.Make() 滑动式 +- builder.MakeWithRegion() 区域内拖拽滑动式 ### 配置选项 -slide.New(slide.WithXxx(), ...) +> slide.NewBuilder(slide.WithXxx(), ...) 或 builder.SetOptions(slide.WithXxx(), ...) - slide.WithImageSize(*option.Size) 设置主图尺寸,默认 300x220 - slide.WithImageAlpha(float32) 设置主图透明度 - slide.WithRangeGraphSize(val option.RangeVal) 设置图形随机尺寸范围 @@ -332,7 +336,7 @@ slide.New(slide.WithXxx(), ...) ### 设置资源 -xxx.SetResources(slide.WithXxx(), ...) +> builder.SetResources(slide.WithXxx(), ...) - slide.WithBackgrounds([]image.Image) 设置主图背景 - slide.WithGraphImages(images []*GraphImage) 设置贴图的图形 @@ -363,7 +367,7 @@ import ( var rotateCapt rotate.Captcha func init() { - rotateCapt = rotate.New() + builder := rotate.NewBuilder() bgImage, err := loadPng("../resources/bg.png") if err != nil { @@ -375,12 +379,14 @@ func init() { log.Fatalln(err) } - rotateCapt.SetResources( + builder.SetResources( rotate.WithImages([]image.Image{ bgImage, bgImage1, }), ) + + rotateCapt = builder.Make() } func main() { @@ -418,11 +424,11 @@ func loadPng(p string) (image.Image, error) { ### 创建实例方法 -- rotate.New() 旋转式 +- builder.Make() 旋转式 ### 配置选项 -rotate.New(rotate.WithXxx(), ...) +> rotate.NewBuilder(rotate.WithXxx(), ...) 或 builder.SetOptions(rotate.WithXxx(), ...) - rotate.WithImageSquareSize(val int) 设置主图大小 - rotate.WithRangeAnglePos(vals []option.RangeVal) 设置校验随机角度范围 - rotate.WithRangeThumbImageSquareSize(val []int) 设置缩略图大小 @@ -430,7 +436,7 @@ rotate.New(rotate.WithXxx(), ...) ### 设置资源 -xxx.SetResources(rotate.WithXxx(), ...) +> builder.SetResources(rotate.WithXxx(), ...) - rotate.WithBackgrounds([]image.Image) 设置主图图片 ### 验证码数据 diff --git a/v2/capts.go b/v2/capts.go index 896cd25..31b1a70 100644 --- a/v2/capts.go +++ b/v2/capts.go @@ -6,27 +6,17 @@ import ( "github.com/wenlng/go-captcha/v2/slide" ) -// NewClick . -func NewClick(opts ...click.Option) click.Captcha { - return click.New(opts...) +// NewClickBuilder . +func NewClickBuilder(opts ...click.Option) click.Builder { + return click.NewBuilder(opts...) } -// NewClickWithShape . -func NewClickWithShape(opts ...click.Option) click.Captcha { - return click.NewWithShape(opts...) +// NewSlideBuilder . +func NewSlideBuilder(opts ...slide.Option) slide.Builder { + return slide.NewBuilder(opts...) } -// NewSlide . -func NewSlide(opts ...slide.Option) slide.Captcha { - return slide.New(opts...) -} - -// NewSlideWithRegion . -func NewSlideWithRegion(opts ...slide.Option) slide.Captcha { - return slide.NewWithRegion(opts...) -} - -// NewRotate . -func NewRotate(opts ...rotate.Option) rotate.Captcha { - return rotate.New(opts...) +// NewRotateBuilder . +func NewRotateBuilder(opts ...rotate.Option) rotate.Builder { + return rotate.NewBuilder(opts...) } diff --git a/v2/click/builder.go b/v2/click/builder.go new file mode 100644 index 0000000..3f19e8f --- /dev/null +++ b/v2/click/builder.go @@ -0,0 +1,67 @@ +package click + +// Builder . +type Builder interface { + SetOptions(opts ...Option) + SetResources(resources ...Resource) + Clear() + Make() Captcha + MakeWithShape() Captcha +} + +var _ Builder = (*builder)(nil) + +// builder . +type builder struct { + opts []Option + resources []Resource +} + +// NewBuilder . +func NewBuilder(opts ...Option) Builder { + build := &builder{ + opts: make([]Option, 0), + resources: make([]Resource, 0), + } + + if len(opts) > 0 { + build.opts = opts + } + + return build +} + +func (b *builder) Clear() { + b.opts = make([]Option, 0) + b.resources = make([]Resource, 0) +} + +// SetOptions is the set option +func (b *builder) SetOptions(opts ...Option) { + if len(opts) > 0 { + b.opts = append(b.opts, opts...) + } +} + +// SetResources is the set resource +func (b *builder) SetResources(resources ...Resource) { + if len(resources) > 0 { + b.resources = append(b.resources, resources...) + } +} + +// Make . +func (b *builder) Make() Captcha { + capt := newWithMode(ModeText) + capt.setOptions(b.opts...) + capt.setResources(b.resources...) + return capt +} + +// MakeWithShape . +func (b *builder) MakeWithShape() Captcha { + capt := newWithMode(ModeShape) + capt.setOptions(b.opts...) + capt.setResources(b.resources...) + return capt +} diff --git a/v2/click/click.go b/v2/click/click.go index 31817be..32f3308 100644 --- a/v2/click/click.go +++ b/v2/click/click.go @@ -26,9 +26,9 @@ const Version = "2.0.0" // Captcha . type Captcha interface { - SetOptions(opts ...Option) + setOptions(opts ...Option) + setResources(resources ...Resource) GetOptions() *Options - SetResources(resources ...Resource) Generate() (CaptchaData, error) } @@ -51,21 +51,6 @@ type captcha struct { mode Mode } -// GetOptions is to get options -func (c *captcha) GetOptions() *Options { - return c.opts -} - -// New is to create a text captcha -func New(opts ...Option) Captcha { - return newWithMode(ModeText, opts...) -} - -// NewWithShape is to create a graphical captcha -func NewWithShape(opts ...Option) Captcha { - return newWithMode(ModeShape, opts...) -} - // newWithMode is to create a captcha func newWithMode(mode Mode, opts ...Option) *captcha { capt := &captcha{ @@ -86,25 +71,30 @@ func newWithMode(mode Mode, opts ...Option) *captcha { capt.opts.rangeThumbSize = &option.RangeVal{Min: 14, Max: 20} } - capt.SetOptions(opts...) + capt.setOptions(opts...) return capt } -// SetOptions is the set option -func (c *captcha) SetOptions(opts ...Option) { +// setOptions is the set option +func (c *captcha) setOptions(opts ...Option) { for _, opt := range opts { opt(c.opts) } } -// SetResources is the set resource -func (c *captcha) SetResources(resources ...Resource) { +// setResources is the set resource +func (c *captcha) setResources(resources ...Resource) { for _, resource := range resources { resource(c.resources) } } +// GetOptions is to get options +func (c *captcha) GetOptions() *Options { + return c.opts +} + // Generate is to generate the captcha data func (c *captcha) Generate() (CaptchaData, error) { if c.mode == ModeShape { diff --git a/v2/click/default.go b/v2/click/default.go index 38aae14..aae8569 100644 --- a/v2/click/default.go +++ b/v2/click/default.go @@ -18,15 +18,17 @@ var colors = []string{ "#fb88ff", "#b4fed4", "#cbfaa9", + "#78d6f8", } var thumbColors = []string{ - "#1f62ee", - "#4e87ff", - "#9f0bd9", - "#db1042", - "#b15c07", - "#b7055e", + "#1f55c4", + "#780592", + "#2f6b00", + "#910000", + "#864401", + "#675901", + "#016e5c", } var shadowColor = "#101010" diff --git a/v2/rotate/builder.go b/v2/rotate/builder.go new file mode 100644 index 0000000..7b6dfda --- /dev/null +++ b/v2/rotate/builder.go @@ -0,0 +1,58 @@ +package rotate + +// Builder . +type Builder interface { + SetOptions(opts ...Option) + SetResources(resources ...Resource) + Clear() + Make() Captcha +} + +var _ Builder = (*builder)(nil) + +// builder . +type builder struct { + opts []Option + resources []Resource +} + +// NewBuilder . +func NewBuilder(opts ...Option) Builder { + build := &builder{ + opts: make([]Option, 0), + resources: make([]Resource, 0), + } + + if len(opts) > 0 { + build.opts = opts + } + + return build +} + +func (b *builder) Clear() { + b.opts = make([]Option, 0) + b.resources = make([]Resource, 0) +} + +// SetOptions is the set option +func (b *builder) SetOptions(opts ...Option) { + if len(opts) > 0 { + b.opts = append(b.opts, opts...) + } +} + +// SetResources is the set resource +func (b *builder) SetResources(resources ...Resource) { + if len(resources) > 0 { + b.resources = append(b.resources, resources...) + } +} + +// Make . +func (b *builder) Make() Captcha { + capt := newRotate() + capt.setOptions(b.opts...) + capt.setResources(b.resources...) + return capt +} diff --git a/v2/rotate/rotate.go b/v2/rotate/rotate.go index 2658050..f7a5ef1 100644 --- a/v2/rotate/rotate.go +++ b/v2/rotate/rotate.go @@ -22,9 +22,9 @@ const Version = "2.0.0" // Captcha . type Captcha interface { - SetOptions(opts ...Option) + setOptions(opts ...Option) + setResources(resources ...Resource) GetOptions() *Options - SetResources(resources ...Resource) Generate() (CaptchaData, error) } @@ -39,13 +39,8 @@ type captcha struct { resources *Resources } -// GetOptions is to get options -func (c *captcha) GetOptions() *Options { - return c.opts -} - -// New . -func New(opts ...Option) Captcha { +// newRotate . +func newRotate(opts ...Option) Captcha { capt := &captcha{ version: Version, logger: logger.New(), @@ -57,25 +52,30 @@ func New(opts ...Option) Captcha { defaultOptions()(capt.opts) defaultResource()(capt.resources) - capt.SetOptions(opts...) + capt.setOptions(opts...) return capt } -// SetOptions is the set option -func (c *captcha) SetOptions(opts ...Option) { +// setOptions is the set option +func (c *captcha) setOptions(opts ...Option) { for _, opt := range opts { opt(c.opts) } } -// SetResources is the set resource -func (c *captcha) SetResources(resources ...Resource) { +// setResources is the set resource +func (c *captcha) setResources(resources ...Resource) { for _, resource := range resources { resource(c.resources) } } +// GetOptions is to get options +func (c *captcha) GetOptions() *Options { + return c.opts +} + // Generate is to generate the captcha data func (c *captcha) Generate() (CaptchaData, error) { if err := c.check(); err != nil { diff --git a/v2/slide/builder.go b/v2/slide/builder.go new file mode 100644 index 0000000..b584ca9 --- /dev/null +++ b/v2/slide/builder.go @@ -0,0 +1,67 @@ +package slide + +// Builder . +type Builder interface { + SetOptions(opts ...Option) + SetResources(resources ...Resource) + Clear() + Make() Captcha + MakeWithRegion() Captcha +} + +var _ Builder = (*builder)(nil) + +// builder . +type builder struct { + opts []Option + resources []Resource +} + +// NewBuilder . +func NewBuilder(opts ...Option) Builder { + build := &builder{ + opts: make([]Option, 0), + resources: make([]Resource, 0), + } + + if len(opts) > 0 { + build.opts = opts + } + + return build +} + +func (b *builder) Clear() { + b.opts = make([]Option, 0) + b.resources = make([]Resource, 0) +} + +// SetOptions is the set option +func (b *builder) SetOptions(opts ...Option) { + if len(opts) > 0 { + b.opts = append(b.opts, opts...) + } +} + +// SetResources is the set resource +func (b *builder) SetResources(resources ...Resource) { + if len(resources) > 0 { + b.resources = append(b.resources, resources...) + } +} + +// Make . +func (b *builder) Make() Captcha { + capt := newWithMode(ModeBasic) + capt.setOptions(b.opts...) + capt.setResources(b.resources...) + return capt +} + +// MakeWithRegion . +func (b *builder) MakeWithRegion() Captcha { + capt := newWithMode(ModeRegion) + capt.setOptions(b.opts...) + capt.setResources(b.resources...) + return capt +} diff --git a/v2/slide/slide.go b/v2/slide/slide.go index 00dc42b..445da46 100644 --- a/v2/slide/slide.go +++ b/v2/slide/slide.go @@ -31,9 +31,9 @@ const ( // Captcha . type Captcha interface { - SetOptions(opts ...Option) + setOptions(opts ...Option) + setResources(resources ...Resource) GetOptions() *Options - SetResources(resources ...Resource) Generate() (CaptchaData, error) } @@ -49,22 +49,7 @@ type captcha struct { mode Mode } -// GetOptions is to get options -func (c *captcha) GetOptions() *Options { - return c.opts -} - -// New . -func New(opts ...Option) Captcha { - return newWithMode(ModeBasic, opts...) -} - -// NewWithRegion . -func NewWithRegion(opts ...Option) Captcha { - return newWithMode(ModeRegion, opts...) -} - -// NewWithMode . +// newWithMode . func newWithMode(mode Mode, opts ...Option) Captcha { capt := &captcha{ version: Version, @@ -78,7 +63,7 @@ func newWithMode(mode Mode, opts ...Option) Captcha { defaultOptions()(capt.opts) defaultResource()(capt.resources) - capt.SetOptions(opts...) + capt.setOptions(opts...) if mode == ModeBasic { capt.opts.rangeDeadZoneDirections = []DeadZoneDirectionType{DeadZoneDirectionTypeLeft} @@ -88,20 +73,25 @@ func newWithMode(mode Mode, opts ...Option) Captcha { return capt } -// SetOptions is the set option -func (c *captcha) SetOptions(opts ...Option) { +// setOptions is the set option +func (c *captcha) setOptions(opts ...Option) { for _, opt := range opts { opt(c.opts) } } -// SetResources is the set resource -func (c *captcha) SetResources(resources ...Resource) { +// setResources is the set resource +func (c *captcha) setResources(resources ...Resource) { for _, resource := range resources { resource(c.resources) } } +// GetOptions is to get options +func (c *captcha) GetOptions() *Options { + return c.opts +} + // Generate is to generate the captcha data func (c *captcha) Generate() (CaptchaData, error) { if err := c.check(); err != nil { diff --git a/v2/tests/click_shape_test.go b/v2/tests/click_shape_test.go index 08c001f..00f28cc 100644 --- a/v2/tests/click_shape_test.go +++ b/v2/tests/click_shape_test.go @@ -22,7 +22,7 @@ import ( var shapeCapt click.Captcha func init() { - shapeCapt = click.NewWithShape( + builder := click.NewBuilder( click.WithRangeLen(option.RangeVal{Min: 3, Max: 6}), click.WithRangeVerifyLen(option.RangeVal{Min: 2, Max: 3}), click.WithRangeThumbBgDistort(1), @@ -40,7 +40,7 @@ func init() { shapes := getShapeMaps() - shapeCapt.SetResources( + builder.SetResources( click.WithShapes(shapes), click.WithBackgrounds([]image.Image{ bgImage, @@ -50,6 +50,8 @@ func init() { // img1, //}), ) + + shapeCapt = builder.MakeWithShape() } func getShapeMaps() map[string]image.Image { diff --git a/v2/tests/click_text_test.go b/v2/tests/click_text_test.go index a19392a..06d19e8 100644 --- a/v2/tests/click_text_test.go +++ b/v2/tests/click_text_test.go @@ -15,7 +15,7 @@ import ( var textCapt click.Captcha func init() { - textCapt = click.New( + builder := click.NewBuilder( click.WithRangeLen(option.RangeVal{Min: 4, Max: 6}), click.WithRangeVerifyLen(option.RangeVal{Min: 2, Max: 4}), ) @@ -30,7 +30,7 @@ func init() { log.Fatalln(err) } - textCapt.SetResources( + builder.SetResources( click.WithChars([]string{"这", "是", "随", "机", "的", "文", "本", "种", "子", "呀"}), click.WithFonts([]*truetype.Font{ fontN, @@ -42,6 +42,8 @@ func init() { // thumbImage, //}), ) + + textCapt = builder.Make() } func TestClickTextCaptcha(t *testing.T) { diff --git a/v2/tests/rotate_test.go b/v2/tests/rotate_test.go index 1483853..3ffed54 100644 --- a/v2/tests/rotate_test.go +++ b/v2/tests/rotate_test.go @@ -13,7 +13,7 @@ import ( var rotateCapt rotate.Captcha func init() { - rotateCapt = rotate.New() + builder := rotate.NewBuilder() bgImage, err := loadPng("../.cache/bg.png") if err != nil { @@ -25,12 +25,14 @@ func init() { log.Fatalln(err) } - rotateCapt.SetResources( + builder.SetResources( rotate.WithImages([]image.Image{ bgImage, bgImage1, }), ) + + rotateCapt = builder.Make() } func TestRotateDirectionCaptcha(t *testing.T) { diff --git a/v2/tests/slide_tile_test.go b/v2/tests/slide_tile_test.go index 21125d0..46007bf 100644 --- a/v2/tests/slide_tile_test.go +++ b/v2/tests/slide_tile_test.go @@ -14,9 +14,9 @@ import ( var slideTileCapt slide.Captcha func init() { - slideTileCapt = slide.New( - //slide.WithGenGraphNumber(2), - //slide.WithEnableGraphVerticalRandom(true), + builder := slide.NewBuilder( + //slide.WithGenGraphNumber(2), + //slide.WithEnableGraphVerticalRandom(true), ) bgImage, err := loadPng("../.cache/bg.png") @@ -31,7 +31,7 @@ func init() { graphs := getSlideTileGraphArr() - slideTileCapt.SetResources( + builder.SetResources( slide.WithGraphImages(graphs), slide.WithBackgrounds([]image.Image{ bgImage, @@ -41,6 +41,8 @@ func init() { // img1, //}), ) + + slideTileCapt = builder.MakeWithRegion() } func getSlideTileGraphArr() []*slide.GraphImage {