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()
}

View File

@@ -26,19 +26,22 @@ func MySQlConnect() *gorm.DB {
mysqlLogger = logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags),
logger.Config{
SlowThreshold: time.Second, //慢sql日志
LogLevel: logger.Info, //级别
Colorful: true, //颜色
SlowThreshold: time.Second, //慢sql日志
LogLevel: logger.Info, //级别
Colorful: true, //颜色
IgnoreRecordNotFoundError: true, //忽略RecordNotFoundError
ParameterizedQueries: true, //格式化SQL语句
})
} else {
mysqlLogger = logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags),
logger.Config{
SlowThreshold: time.Second, //慢sql日志
LogLevel: logger.Error, //级别
Colorful: true, //颜色
SlowThreshold: time.Second, //慢sql日志
LogLevel: logger.Error, //级别
Colorful: true, //颜色
IgnoreRecordNotFoundError: true, //忽略RecordNotFoundError
ParameterizedQueries: true, //格式化SQL语句
})
}

View File

@@ -63,11 +63,11 @@ func InitLogger() *logrus.Logger {
}
newLog.SetLevel(level) //设置日志级别
global.LOG = newLog
InitDefaultLogger()
initDefaultLogger()
return newLog
}
func InitDefaultLogger() {
func initDefaultLogger() {
//全局日志
logrus.SetOutput(os.Stdout) //设置输出类型
logrus.SetReportCaller(global.CONFIG.Logger.ShowLine) //设置是否显示函数名和行号