Documentation
¶
Index ¶
- func CreateSuccessResponse(captchaID, imageBase64 string) []byte
- func CreateVerifyResponse(success bool, message string) []byte
- func FontData() string
- func GenerateID() string
- func GetPinyin(ch rune) string
- func ImageToImage(r *render.CaptchaRenderer) image.Image
- func NewImage(width, height int, bgColor color.Color) *render.CaptchaRenderer
- func RandomFloat64(min, max float64) float64
- func RandomInt(min, max int) int
- func RandomString(length int, charset string) string
- func ValidateCaptchaJSON(data []byte, expectedAnswer string) (bool, error)
- type ArithmeticOp
- type ArithmeticStrategy
- type Captcha
- type CaptchaConfig
- type CaptchaImage
- type CaptchaJSON
- type CaptchaResponse
- type CaptchaVerifyRequest
- type CaptchaVerifyResponse
- type ChineseStrategy
- type ClickCaptchaData
- type ClickStrategy
- func (s *ClickStrategy) Generate(config CaptchaConfig) (*CaptchaImage, error)
- func (s *ClickStrategy) GenerateWithTargets(config CaptchaConfig) (*ClickCaptchaData, error)
- func (s *ClickStrategy) Verify(answer, expected string) bool
- func (s *ClickStrategy) VerifyClickSequence(clicks []ClickTarget, targets []ClickTarget) bool
- type ClickTarget
- type DigitsStrategy
- type GIFStrategy
- type LettersStrategy
- type MemoryStore
- type Store
- type Strategy
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateSuccessResponse ¶
CreateSuccessResponse 创建成功响应
func CreateVerifyResponse ¶
CreateVerifyResponse 创建验证响应
func ImageToImage ¶
func ImageToImage(r *render.CaptchaRenderer) image.Image
ImageToImage 将渲染器转换为 image.Image
Types ¶
type ArithmeticOp ¶
type ArithmeticOp int
ArithmeticOp 算术运算符
const ( OpAdd ArithmeticOp = iota OpSub OpMul )
type ArithmeticStrategy ¶
type ArithmeticStrategy struct {
// contains filtered or unexported fields
}
ArithmeticStrategy 算术验证码策略
func NewArithmeticStrategy ¶
func NewArithmeticStrategy(ops ...ArithmeticOp) *ArithmeticStrategy
NewArithmeticStrategy 创建算术验证码策略
func (*ArithmeticStrategy) Generate ¶
func (s *ArithmeticStrategy) Generate(config CaptchaConfig) (*CaptchaImage, error)
Generate 生成算术验证码
func (*ArithmeticStrategy) Verify ¶
func (s *ArithmeticStrategy) Verify(answer, expected string) bool
Verify 验证答案
type Captcha ¶
type Captcha struct {
// contains filtered or unexported fields
}
Captcha 验证码管理器
type CaptchaConfig ¶
CaptchaConfig 验证码配置
type CaptchaImage ¶
type CaptchaImage struct {
Image image.Image `json:"-"`
Answer string `json:"answer"`
CaptchaID string `json:"captcha_id"`
CreatedAt time.Time `json:"created_at"`
}
CaptchaImage 验证码图片
func FromJSON ¶
func FromJSON(data []byte) (*CaptchaImage, error)
FromJSON 从 JSON 恢复验证码(注意:Image 字段无法序列化)
func FromJSONWithImage ¶
func FromJSONWithImage(data []byte) (*CaptchaImage, []byte, error)
FromJSONWithImage 从包含图片数据的 JSON 恢复验证码
func (*CaptchaImage) MarshalJSON ¶
func (c *CaptchaImage) MarshalJSON() ([]byte, error)
MarshalJSON 实现 json.Marshaler 接口
func (*CaptchaImage) ToJSONWithImage ¶
func (c *CaptchaImage) ToJSONWithImage(imageData []byte) ([]byte, error)
ToJSONWithImage 将验证码转换为包含图片数据的 JSON
func (*CaptchaImage) UnmarshalJSON ¶
func (c *CaptchaImage) UnmarshalJSON(data []byte) error
UnmarshalJSON 实现 json.Unmarshaler 接口
type CaptchaJSON ¶
type CaptchaJSON struct {
CaptchaID string `json:"captcha_id"`
Answer string `json:"answer"`
CreatedAt time.Time `json:"created_at"`
ImageData []byte `json:"image_data,omitempty"`
}
CaptchaJSON 验证码的 JSON 序列化结构
type CaptchaResponse ¶
type CaptchaResponse struct {
Success bool `json:"success"`
Message string `json:"message,omitempty"`
Data struct {
CaptchaID string `json:"captcha_id"`
Image string `json:"image"` // base64 编码的图片
} `json:"data,omitempty"`
}
CaptchaResponse API 响应结构
type CaptchaVerifyRequest ¶
type CaptchaVerifyRequest struct {
CaptchaID string `json:"captcha_id"`
Answer string `json:"answer"`
}
CaptchaVerifyRequest 验证请求
type CaptchaVerifyResponse ¶
type CaptchaVerifyResponse struct {
Success bool `json:"success"`
Message string `json:"message,omitempty"`
}
CaptchaVerifyResponse 验证响应
type ChineseStrategy ¶
type ChineseStrategy struct {
// contains filtered or unexported fields
}
ChineseStrategy 中文验证码策略
func NewChineseStrategy ¶
func NewChineseStrategy(chars ...string) *ChineseStrategy
NewChineseStrategy 创建中文验证码策略
func (*ChineseStrategy) Generate ¶
func (s *ChineseStrategy) Generate(config CaptchaConfig) (*CaptchaImage, error)
Generate 生成中文验证码
func (*ChineseStrategy) Verify ¶
func (s *ChineseStrategy) Verify(answer, expected string) bool
Verify 验证答案
type ClickCaptchaData ¶
type ClickCaptchaData struct {
Image *CaptchaImage `json:"-"`
Targets []ClickTarget `json:"targets"`
Sequence []int `json:"sequence"` // 正确的点击顺序
}
ClickCaptchaData 点击验证码数据
type ClickStrategy ¶
type ClickStrategy struct {
// contains filtered or unexported fields
}
ClickStrategy 点击验证码策略
func NewClickStrategy ¶
func NewClickStrategy(targetCount, targetSize int) *ClickStrategy
NewClickStrategy 创建点击验证码策略
func (*ClickStrategy) Generate ¶
func (s *ClickStrategy) Generate(config CaptchaConfig) (*CaptchaImage, error)
Generate 生成点击验证码
func (*ClickStrategy) GenerateWithTargets ¶
func (s *ClickStrategy) GenerateWithTargets(config CaptchaConfig) (*ClickCaptchaData, error)
GenerateWithTargets 生成带目标数据的点击验证码
func (*ClickStrategy) Verify ¶
func (s *ClickStrategy) Verify(answer, expected string) bool
Verify 验证点击顺序
func (*ClickStrategy) VerifyClickSequence ¶
func (s *ClickStrategy) VerifyClickSequence(clicks []ClickTarget, targets []ClickTarget) bool
VerifyClickSequence 验证点击坐标序列
type ClickTarget ¶
type ClickTarget struct {
X int `json:"x"`
Y int `json:"y"`
Width int `json:"width"`
Height int `json:"height"`
Label string `json:"label"`
Order int `json:"order"`
}
ClickTarget 点击目标
type DigitsStrategy ¶
type DigitsStrategy struct{}
DigitsStrategy 数字验证码策略
func (*DigitsStrategy) Generate ¶
func (s *DigitsStrategy) Generate(config CaptchaConfig) (*CaptchaImage, error)
Generate 生成数字验证码
func (*DigitsStrategy) Verify ¶
func (s *DigitsStrategy) Verify(answer, expected string) bool
Verify 验证答案
type GIFStrategy ¶
type GIFStrategy struct {
// contains filtered or unexported fields
}
GIFStrategy GIF 动画验证码策略
func NewGIFStrategy ¶
func NewGIFStrategy(frameCount, delay int) *GIFStrategy
NewGIFStrategy 创建 GIF 动画验证码策略
func (*GIFStrategy) Generate ¶
func (s *GIFStrategy) Generate(config CaptchaConfig) (*CaptchaImage, error)
Generate 生成 GIF 动画验证码
func (*GIFStrategy) GenerateGIF ¶
func (s *GIFStrategy) GenerateGIF(config CaptchaConfig) ([]byte, *CaptchaImage, error)
GenerateGIF 生成 GIF 字节数据
type LettersStrategy ¶
type LettersStrategy struct{}
LettersStrategy 字母验证码策略
func (*LettersStrategy) Generate ¶
func (s *LettersStrategy) Generate(config CaptchaConfig) (*CaptchaImage, error)
Generate 生成字母验证码
func (*LettersStrategy) Verify ¶
func (s *LettersStrategy) Verify(answer, expected string) bool
Verify 验证答案
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore 内存存储实现
type Store ¶
type Store interface {
// Set 存储验证码答案
Set(id string, answer string, ttl time.Duration)
// Get 获取验证码答案
Get(id string) (string, bool)
// Delete 删除验证码
Delete(id string)
}
Store 验证码存储接口
type Strategy ¶
type Strategy interface {
// Generate 生成验证码图片
Generate(config CaptchaConfig) (*CaptchaImage, error)
// Verify 验证答案是否正确
Verify(answer, expected string) bool
}
Strategy 验证码策略接口