Documentation
¶
Index ¶
- func CloseGlobalService() error
- func InitGlobalService(config *CaptchaConfig) error
- func Verify(id, answer string, clear bool) bool
- func VerifyAndClear(id, answer string) bool
- type AudioCaptchaResult
- type AudioConfig
- type CaptchaConfig
- type CaptchaResult
- type CaptchaType
- type ChineseConfig
- type DigitConfig
- type MathConfig
- type MemoryStore
- type RandomConfig
- type RedisConfig
- type RedisStore
- type Service
- func (s *Service) Clear(id string)
- func (s *Service) Close() error
- func (s *Service) Generate() (*CaptchaResult, error)
- func (s *Service) GenerateAudio() (*AudioCaptchaResult, error)
- func (s *Service) GenerateChinese() (*CaptchaResult, error)
- func (s *Service) GenerateDigit() (*CaptchaResult, error)
- func (s *Service) GenerateMath() (*CaptchaResult, error)
- func (s *Service) GenerateRandom() (*CaptchaResult, error)
- func (s *Service) GenerateString() (*CaptchaResult, error)
- func (s *Service) Verify(id, answer string, clear bool) bool
- func (s *Service) VerifyAndClear(id, answer string) bool
- type Store
- type StoreConfig
- type StoreType
- type StringConfig
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InitGlobalService ¶
func InitGlobalService(config *CaptchaConfig) error
InitGlobalService 初始化全局验证码服务
Example ¶
ExampleInitGlobalService 演示如何使用全局服务
// 创建配置
config := &CaptchaConfig{
Type: TypeString,
String: StringConfig{
Height: 60,
Width: 240,
Length: 4,
Source: "abcdefghijklmnopqrstuvwxyz0123456789",
NoiseCount: 0,
},
Store: StoreConfig{
Type: StoreTypeMemory,
Expire: 5 * time.Minute,
KeyPrefix: "captcha:",
},
}
// 初始化全局服务
err := InitGlobalService(config)
if err != nil {
log.Fatal(err)
}
defer CloseGlobalService()
// 使用全局函数生成验证码
result, err := Generate()
if err != nil {
log.Fatal(err)
}
fmt.Printf("验证码ID: %s\n", result.ID)
fmt.Printf("验证码答案: %s\n", result.Answer)
// 验证验证码
isValid := VerifyAndClear(result.ID, result.Answer)
fmt.Printf("验证结果: %t\n", isValid)
Types ¶
type AudioCaptchaResult ¶
type AudioCaptchaResult struct {
ID string `json:"id"` // 验证码ID
Base64Blob string `json:"base64Blob"` // Base64编码的音频数据
Body []byte `json:"body"` // 原始音频数据
Answer string `json:"answer"` // 验证码答案(仅用于测试)
}
AudioCaptchaResult 音频验证码生成结果
type AudioConfig ¶
type AudioConfig struct {
Length int `json:"length" yaml:"length"` // 验证码长度
Language string `json:"language" yaml:"language"` // 语言
}
AudioConfig 音频验证码配置
type CaptchaConfig ¶
type CaptchaConfig struct {
// 验证码类型配置
Type CaptchaType `json:"type" yaml:"type"`
// 数字验证码配置
Digit DigitConfig `json:"digit" yaml:"digit"`
// 字符串验证码配置
String StringConfig `json:"string" yaml:"string"`
// 数学验证码配置
Math MathConfig `json:"math" yaml:"math"`
// 中文验证码配置
Chinese ChineseConfig `json:"chinese" yaml:"chinese"`
// 音频验证码配置
Audio AudioConfig `json:"audio" yaml:"audio"`
// 存储配置
Store StoreConfig `json:"store" yaml:"store"`
// 随机验证码配置
Random RandomConfig `json:"random" yaml:"random"`
}
CaptchaConfig 验证码配置
type CaptchaResult ¶
type CaptchaResult struct {
ID string `json:"id"` // 验证码ID
Base64Blob string `json:"base64Blob"` // Base64编码的图片数据
Body []byte `json:"body"` // 原始图片数据
Answer string `json:"answer"` // 验证码答案(仅用于测试)
CaptchaType string `json:"captchaType"` // 验证码类型
}
CaptchaResult 验证码生成结果
type CaptchaType ¶
type CaptchaType string
CaptchaType 验证码类型
const ( TypeDigit CaptchaType = "digit" // 数字验证码 TypeString CaptchaType = "string" // 字符串验证码 TypeMath CaptchaType = "math" // 数学验证码 TypeChinese CaptchaType = "chinese" // 中文验证码 TypeAudio CaptchaType = "audio" // 音频验证码 TypeRandom CaptchaType = "random" // 随机验证码类型 )
type ChineseConfig ¶
type ChineseConfig struct {
Height int `json:"height" yaml:"height"` // 图片高度
Width int `json:"width" yaml:"width"` // 图片宽度
NoiseCount int `json:"noiseCount" yaml:"noiseCount"` // 干扰线数量
ShowLineOptions int `json:"showLineOptions" yaml:"showLineOptions"` // 显示线条选项
Length int `json:"length" yaml:"length"` // 验证码长度
Source string `json:"source" yaml:"source"` // 中文字符源
BgColor *color.RGBA `json:"bgColor" yaml:"bgColor"` // 背景色
Fonts []string `json:"fonts" yaml:"fonts"` // 字体列表
}
ChineseConfig 中文验证码配置
type DigitConfig ¶
type DigitConfig struct {
Height int `json:"height" yaml:"height"` // 图片高度
Width int `json:"width" yaml:"width"` // 图片宽度
Length int `json:"length" yaml:"length"` // 验证码长度
MaxSkew float64 `json:"maxSkew" yaml:"maxSkew"` // 最大倾斜角度
DotCount int `json:"dotCount" yaml:"dotCount"` // 干扰点数量
}
DigitConfig 数字验证码配置
type MathConfig ¶
type MathConfig struct {
Height int `json:"height" yaml:"height"` // 图片高度
Width int `json:"width" yaml:"width"` // 图片宽度
NoiseCount int `json:"noiseCount" yaml:"noiseCount"` // 干扰线数量
ShowLineOptions int `json:"showLineOptions" yaml:"showLineOptions"` // 显示线条选项
BgColor *color.RGBA `json:"bgColor" yaml:"bgColor"` // 背景色
Fonts []string `json:"fonts" yaml:"fonts"` // 字体列表
}
MathConfig 数学验证码配置
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore 内存存储实现
func NewMemoryStore ¶
func NewMemoryStore(collectNum int, expiration time.Duration) *MemoryStore
NewMemoryStore 创建内存存储
type RandomConfig ¶
type RandomConfig struct {
EnabledTypes []CaptchaType `json:"enabledTypes" yaml:"enabledTypes"` // 启用的验证码类型
ExcludeAudio bool `json:"excludeAudio" yaml:"excludeAudio"` // 是否排除音频验证码
}
RandomConfig 随机验证码配置
type RedisConfig ¶
type RedisConfig struct {
Addr string `json:"addr" yaml:"addr"` // Redis地址
Password string `json:"password" yaml:"password"` // Redis密码
DB int `json:"db" yaml:"db"` // Redis数据库
PoolSize int `json:"poolSize" yaml:"poolSize"` // 连接池大小
}
RedisConfig Redis配置
type RedisStore ¶
type RedisStore struct {
// contains filtered or unexported fields
}
RedisStore Redis存储实现
Example ¶
ExampleRedisStore 演示如何使用Redis存储
// 注意:这个示例需要Redis服务器运行在localhost:6379
config := &CaptchaConfig{
Type: TypeMath,
Store: StoreConfig{
Type: StoreTypeRedis,
Expire: 10 * time.Minute,
KeyPrefix: "captcha:",
Redis: RedisConfig{
Addr: "localhost:6379",
Password: "",
DB: 0,
PoolSize: 10,
},
},
}
service, err := NewService(config)
if err != nil {
log.Printf("Redis连接失败,跳过示例: %v", err)
return
}
defer service.Close()
// 生成数学验证码
result, err := service.GenerateMath()
if err != nil {
log.Fatal(err)
}
fmt.Printf("验证码ID: %s\n", result.ID)
fmt.Printf("验证码答案: %s\n", result.Answer)
// 验证验证码
isValid := service.VerifyAndClear(result.ID, result.Answer)
fmt.Printf("验证结果: %t\n", isValid)
func NewRedisStore ¶
func NewRedisStore(config RedisConfig, keyPrefix string, expire time.Duration) (*RedisStore, error)
NewRedisStore 创建Redis存储
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service 验证码服务
func (*Service) Generate ¶
func (s *Service) Generate() (*CaptchaResult, error)
Generate 根据配置类型生成验证码
func (*Service) GenerateAudio ¶
func (s *Service) GenerateAudio() (*AudioCaptchaResult, error)
GenerateAudio 生成音频验证码
func (*Service) GenerateChinese ¶
func (s *Service) GenerateChinese() (*CaptchaResult, error)
GenerateChinese 生成中文验证码
func (*Service) GenerateDigit ¶
func (s *Service) GenerateDigit() (*CaptchaResult, error)
GenerateDigit 生成数字验证码
Example ¶
ExampleService_GenerateDigit 演示如何生成数字验证码
// 创建配置
config := DefaultConfig()
config.Store.Type = StoreTypeMemory // 使用内存存储用于示例
// 创建服务
service, err := NewService(config)
if err != nil {
log.Fatal(err)
}
defer service.Close()
// 生成数字验证码
result, err := service.GenerateDigit()
if err != nil {
log.Fatal(err)
}
fmt.Printf("验证码ID: %s\n", result.ID)
fmt.Printf("验证码答案: %s\n", result.Answer)
fmt.Printf("Base64图片数据长度: %d\n", len(result.Base64Blob))
// 验证验证码
isValid := service.VerifyAndClear(result.ID, result.Answer)
fmt.Printf("验证结果: %t\n", isValid)
func (*Service) GenerateMath ¶
func (s *Service) GenerateMath() (*CaptchaResult, error)
GenerateMath 生成数学验证码
func (*Service) GenerateRandom ¶
func (s *Service) GenerateRandom() (*CaptchaResult, error)
GenerateRandom 生成随机类型的验证码
func (*Service) GenerateString ¶
func (s *Service) GenerateString() (*CaptchaResult, error)
GenerateString 生成字符串验证码
func (*Service) VerifyAndClear ¶
VerifyAndClear 验证验证码并清除
type Store ¶
type Store interface {
base64Captcha.Store
// Close 关闭存储连接
Close() error
}
Store 验证码存储接口
type StoreConfig ¶
type StoreConfig struct {
Type StoreType `json:"type" yaml:"type"` // 存储类型
Expire time.Duration `json:"expire" yaml:"expire"` // 过期时间
KeyPrefix string `json:"keyPrefix" yaml:"keyPrefix"` // Key前缀
Redis RedisConfig `json:"redis" yaml:"redis"` // Redis配置
}
StoreConfig 存储配置
type StringConfig ¶
type StringConfig struct {
Height int `json:"height" yaml:"height"` // 图片高度
Width int `json:"width" yaml:"width"` // 图片宽度
NoiseCount int `json:"noiseCount" yaml:"noiseCount"` // 干扰线数量
ShowLineOptions int `json:"showLineOptions" yaml:"showLineOptions"` // 显示线条选项
Length int `json:"length" yaml:"length"` // 验证码长度
Source string `json:"source" yaml:"source"` // 字符源
BgColor *color.RGBA `json:"bgColor" yaml:"bgColor"` // 背景色
Fonts []string `json:"fonts" yaml:"fonts"` // 字体列表
}
StringConfig 字符串验证码配置