code migration

This commit is contained in:
landaiqing
2024-11-15 02:02:19 +08:00
parent b2d753e832
commit 34c4690f80
85 changed files with 4349 additions and 421 deletions

View File

@@ -0,0 +1,38 @@
package captcha
import (
"github.com/wenlng/go-captcha-assets/resources/images"
"github.com/wenlng/go-captcha-assets/resources/shapes"
"github.com/wenlng/go-captcha/v2/base/option"
"github.com/wenlng/go-captcha/v2/click"
)
// NewClickShapeCaptcha 初始化点击形状验证码
func NewClickShapeCaptcha() click.Captcha {
builder := click.NewBuilder(
click.WithRangeLen(option.RangeVal{Min: 3, Max: 6}),
click.WithRangeVerifyLen(option.RangeVal{Min: 2, Max: 3}),
click.WithRangeThumbBgDistort(1),
click.WithIsThumbNonDeformAbility(true),
)
// shape
// click.WithUseShapeOriginalColor(false) -> Random rewriting of graphic colors
shapeMaps, err := shapes.GetShapes()
if err != nil {
panic(err)
}
// background images
imgs, err := images.GetImages()
if err != nil {
panic(err)
}
// set resources
builder.SetResources(
click.WithShapes(shapeMaps),
click.WithBackgrounds(imgs),
)
return builder.MakeWithShape()
}

View File

@@ -0,0 +1,26 @@
package captcha
import (
"github.com/wenlng/go-captcha-assets/resources/images"
"github.com/wenlng/go-captcha/v2/base/option"
"github.com/wenlng/go-captcha/v2/rotate"
)
// NewRotateCaptcha 初始化旋转验证码
func NewRotateCaptcha() rotate.Captcha {
builder := rotate.NewBuilder(rotate.WithRangeAnglePos([]option.RangeVal{
{Min: 20, Max: 330},
}))
// background images
imgs, err := images.GetImages()
if err != nil {
panic(err)
}
// set resources
builder.SetResources(
rotate.WithImages(imgs),
)
return builder.Make()
}

View File

@@ -0,0 +1,44 @@
package captcha
import (
"github.com/wenlng/go-captcha-assets/resources/images"
"github.com/wenlng/go-captcha-assets/resources/tiles"
"github.com/wenlng/go-captcha/v2/slide"
)
// NewSlideCaptcha 初始化滑动验证码
func NewSlideCaptcha() slide.Captcha {
builder := slide.NewBuilder(
// slide.WithGenGraphNumber(2),
slide.WithEnableGraphVerticalRandom(true),
)
// background images
imgs, err := images.GetImages()
if err != nil {
panic(err)
}
graphs, err := tiles.GetTiles()
if err != nil {
panic(err)
}
var newGraphs = make([]*slide.GraphImage, 0, len(graphs))
for i := 0; i < len(graphs); i++ {
graph := graphs[i]
newGraphs = append(newGraphs, &slide.GraphImage{
OverlayImage: graph.OverlayImage,
MaskImage: graph.MaskImage,
ShadowImage: graph.ShadowImage,
})
}
// set resources
builder.SetResources(
slide.WithGraphImages(newGraphs),
slide.WithBackgrounds(imgs),
)
return builder.Make()
}

View File

@@ -0,0 +1,43 @@
package captcha
import (
"github.com/wenlng/go-captcha-assets/resources/images"
"github.com/wenlng/go-captcha-assets/resources/tiles"
"github.com/wenlng/go-captcha/v2/slide"
)
// NewSlideRegionCaptcha 初始化滑动区域验证码
func NewSlideRegionCaptcha() slide.Captcha {
builder := slide.NewBuilder(
slide.WithGenGraphNumber(2),
slide.WithEnableGraphVerticalRandom(true),
)
// background image
imgs, err := images.GetImages()
if err != nil {
panic(err)
}
graphs, err := tiles.GetTiles()
if err != nil {
panic(err)
}
var newGraphs = make([]*slide.GraphImage, 0, len(graphs))
for i := 0; i < len(graphs); i++ {
graph := graphs[i]
newGraphs = append(newGraphs, &slide.GraphImage{
OverlayImage: graph.OverlayImage,
MaskImage: graph.MaskImage,
ShadowImage: graph.ShadowImage,
})
}
// set resources
builder.SetResources(
slide.WithGraphImages(newGraphs),
slide.WithBackgrounds(imgs),
)
return builder.MakeWithRegion()
}

View File

@@ -0,0 +1,97 @@
package captcha
import (
"github.com/golang/freetype/truetype"
"github.com/wenlng/go-captcha-assets/resources/fonts/fzshengsksjw"
"github.com/wenlng/go-captcha-assets/resources/images"
"github.com/wenlng/go-captcha-assets/sourcedata/chars"
"github.com/wenlng/go-captcha/v2/base/option"
"github.com/wenlng/go-captcha/v2/click"
)
// NewTextCaptcha 初始化点选验证码
func NewTextCaptcha() click.Captcha {
builder := click.NewBuilder(
click.WithRangeLen(option.RangeVal{Min: 4, Max: 6}),
click.WithRangeVerifyLen(option.RangeVal{Min: 2, Max: 4}),
click.WithRangeThumbColors([]string{
"#1f55c4",
"#780592",
"#2f6b00",
"#910000",
"#864401",
"#675901",
"#016e5c",
}),
click.WithRangeColors([]string{
"#fde98e",
"#60c1ff",
"#fcb08e",
"#fb88ff",
"#b4fed4",
"#cbfaa9",
"#78d6f8",
}),
)
// fonts
fonts, err := fzshengsksjw.GetFont()
if err != nil {
panic(err)
}
// background images
imgs, err := images.GetImages()
if err != nil {
panic(err)
}
// thumb images
// thumbImages, err := thumbs.GetThumbs()
// if err != nil {
// log.Fatalln(err)
// }
// set resources
builder.SetResources(
click.WithChars(chars.GetChineseChars()),
// click.WithChars([]string{
// "1A",
// "5E",
// "3d",
// "0p",
// "78",
// "DL",
// "CB",
// "9M",
// }),
// click.WithChars(chars.GetAlphaChars()),
click.WithFonts([]*truetype.Font{fonts}),
click.WithBackgrounds(imgs),
// click.WithThumbBackgrounds(thumbImages),
)
// global.TextCaptcha = builder.Make()
// ============================
builder.Clear()
builder.SetOptions(
click.WithRangeLen(option.RangeVal{Min: 4, Max: 6}),
click.WithRangeVerifyLen(option.RangeVal{Min: 2, Max: 4}),
click.WithRangeThumbColors([]string{
"#4a85fb",
"#d93ffb",
"#56be01",
"#ee2b2b",
"#cd6904",
"#b49b03",
"#01ad90",
}),
)
builder.SetResources(
click.WithChars(chars.GetChineseChars()),
click.WithFonts([]*truetype.Font{fonts}),
click.WithBackgrounds(imgs),
)
return builder.Make()
}