add go-captcha / add base auth api

This commit is contained in:
landaiqing
2024-08-06 00:08:33 +08:00
parent 9b36d2fff0
commit d0496b34f0
29 changed files with 687 additions and 121 deletions

100
core/captcha.go Normal file
View File

@@ -0,0 +1,100 @@
package core
import (
"github.com/golang/freetype/truetype"
"github.com/wenlng/go-captcha-assets/bindata/chars"
"github.com/wenlng/go-captcha-assets/resources/fonts/fzshengsksjw"
"github.com/wenlng/go-captcha-assets/resources/images"
"github.com/wenlng/go-captcha-assets/resources/tiles"
"github.com/wenlng/go-captcha/v2/base/option"
"github.com/wenlng/go-captcha/v2/click"
"github.com/wenlng/go-captcha/v2/rotate"
"github.com/wenlng/go-captcha/v2/slide"
"log"
"schisandra-cloud-album/global"
)
// initTextCaptcha 初始化点选验证码
func initTextCaptcha() {
builder := click.NewBuilder()
// fonts
fonts, err := fzshengsksjw.GetFont()
if err != nil {
global.LOG.Fatalln(err)
}
// background images
imgs, err := images.GetImages()
if err != nil {
global.LOG.Fatalln(err)
}
builder.SetResources(
click.WithChars(chars.GetChineseChars()),
click.WithFonts([]*truetype.Font{fonts}),
click.WithBackgrounds(imgs),
)
global.TextCaptcha = builder.Make()
}
// initSlideCaptcha 初始化滑动验证码
func initsSlideCaptcha() {
builder := slide.NewBuilder(
//slide.WithGenGraphNumber(2),
slide.WithEnableGraphVerticalRandom(true),
)
// background images
imgs, err := images.GetImages()
if err != nil {
log.Fatalln(err)
}
graphs, err := tiles.GetTiles()
if err != nil {
log.Fatalln(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),
)
global.SlideCaptcha = builder.Make()
}
// initRotateCaptcha 初始化旋转验证码
func initRotateCaptcha() {
builder := rotate.NewBuilder(rotate.WithRangeAnglePos([]option.RangeVal{
{Min: 20, Max: 330},
}))
// background images
imgs, err := images.GetImages()
if err != nil {
log.Fatalln(err)
}
// set resources
builder.SetResources(
rotate.WithImages(imgs),
)
global.RotateCaptcha = builder.Make()
}
func InitCaptcha() {
initTextCaptcha()
}