wireguard-dashboard/http/api/captcha.go

37 lines
812 B
Go
Raw Normal View History

2024-03-06 17:31:39 +08:00
package api
import (
"github.com/gin-gonic/gin"
"github.com/mojocn/base64Captcha"
2024-03-07 17:32:38 +08:00
"wireguard-dashboard/component"
2024-03-06 17:31:39 +08:00
"wireguard-dashboard/utils"
)
type captcha struct{}
func Captcha() captcha {
return captcha{}
}
// GenerateCaptcha
// @description: 生成验证码
// @receiver captcha
// @param c
func (captcha) GenerateCaptcha(c *gin.Context) {
2024-05-13 09:15:27 +08:00
math := base64Captcha.DriverMath{Height: 120, Width: 480, Fonts: []string{"ApothecaryFont.ttf", "3Dumb.ttf"}}
2024-03-06 17:31:39 +08:00
mathDriver := math.ConvertFonts()
2024-03-07 17:32:38 +08:00
capt := base64Captcha.NewCaptcha(mathDriver, component.CaptchaStore{})
2024-03-06 17:31:39 +08:00
id, base64Str, _, err := capt.Generate()
if err != nil {
utils.GinResponse(c).FailedWithErr("生成验证码失败: %v", err)
return
}
utils.GinResponse(c).OKWithData(map[string]any{
"id": id,
"captcha": base64Str,
})
}