Files
x_admin/server/service/commonService/captchaService.go
2025-07-26 00:56:50 +08:00

65 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package commonService
import (
"image/color"
"x_admin/core"
"x_admin/schema/commonSchema"
"x_admin/util/aj-captcha-go/config"
"x_admin/util/aj-captcha-go/constant"
"x_admin/util/aj-captcha-go/service"
)
// var captcha_config = config.NewConfig()// 默认配置,可以根据项目自行配置,将其他类型配置序列化上去
var captcha_config = config.Config{
CacheType: constant.RedisCacheKey,
Watermark: &config.WatermarkConfig{
FontSize: 12,
Color: color.RGBA{R: 255, G: 255, B: 255, A: 255},
Text: "admin",
},
ClickWord: &config.ClickWordConfig{
FontSize: 25,
FontNum: 4,
},
BlockPuzzle: &config.BlockPuzzleConfig{Offset: 10},
CacheExpireSec: 2 * 60, // 缓存有效时间
}
// 服务工厂,主要用户注册 获取 缓存和验证服务
var factory = service.NewCaptchaServiceFactory(&captcha_config)
func init() {
// 这里默认是注册了 内存缓存,但是不足以应对生产环境,希望自行注册缓存驱动 实现缓存接口即可替换CacheType就是注册进去的 key
// factory.RegisterCache(constant.MemCacheKey, service.NewMemCacheService(200000)) // 这里20指的是缓存阈值
// //注册自定义配置redis数据库
factory.RegisterCache(constant.RedisCacheKey, service.NewConfigRedisCacheService(core.Redis))
// 注册了两种验证码服务 可以自行实现更多的验证
factory.RegisterService(constant.ClickWordCaptcha, service.NewClickWordCaptchaService(factory))
factory.RegisterService(constant.BlockPuzzleCaptcha, service.NewBlockPuzzleCaptchaService(factory))
}
func CaptchaGet(captchaType string) (interface{}, error) {
// 根据参数类型获取不同服务即可
data, err := factory.GetService(captchaType).Get()
return data, err
}
// 检查是否正确
func CaptchaCheck(params commonSchema.ClientParams) error {
ser := factory.GetService(params.CaptchaType)
// 登录验证并删除
err := ser.Check(params.Token, params.PointJson)
return err
}
// 登录等场景验证,并删除
func CaptchaVerify(params commonSchema.ClientParams) error {
ser := factory.GetService(params.CaptchaType)
// 登录验证并删除
err := ser.Verification(params.Token, params.PointJson)
return err
}