60 lines
1.9 KiB
Go
60 lines
1.9 KiB
Go
package model
|
||
|
||
import "errors"
|
||
|
||
// Captcha 结构体定义了验证码的属性
|
||
type Captcha struct {
|
||
NonceStr string `json:"nonceStr"` // 随机字符串,用于唯一标识验证码
|
||
CanvasSrc string `json:"canvasSrc"` // 画布图像的Base64编码字符串
|
||
BlockSrc string `json:"blockSrc"` // 块图像的Base64编码字符串
|
||
BlockX int `json:"blockX"` // 块图像在画布上的X轴位置
|
||
BlockY int `json:"blockY"` // 块图像在画布上的Y轴位置
|
||
CanvasWidth int `json:"canvasWidth"` // 画布的宽度
|
||
CanvasHeight int `json:"canvasHeight"` // 画布的高度
|
||
BlockWidth int `json:"blockWidth"` // 块图像的宽度
|
||
BlockHeight int `json:"blockHeight"` // 块图像的高度
|
||
BlockRadius int `json:"blockRadius"` // 块图像的圆角半径
|
||
Place int `json:"place"` // 图像来源标识,0表示URL下载,1表示本地文件
|
||
}
|
||
|
||
// Verification 结构体定义了验证验证码的属性
|
||
type Verification struct {
|
||
NonceStr string `json:"nonceStr"` // 随机字符串,用于唯一标识验证码
|
||
BlockX string `json:"blockX"` // 画布图像的Base64编码字符串
|
||
}
|
||
|
||
// 检查并设置验证码的默认参数
|
||
func CheckCaptcha(captcha *Captcha) (err error) {
|
||
|
||
// 参数值不可以小于15
|
||
if captcha.CanvasWidth < 41 || captcha.CanvasHeight < 26 {
|
||
captcha.CanvasWidth = 320
|
||
captcha.CanvasHeight = 155
|
||
}
|
||
|
||
if captcha.BlockWidth < 15 || captcha.BlockHeight < 15 {
|
||
captcha.BlockWidth = 65
|
||
captcha.BlockHeight = 55
|
||
}
|
||
|
||
if captcha.BlockRadius < 5 {
|
||
captcha.BlockRadius = 9
|
||
}
|
||
|
||
if captcha.Place == 0 {
|
||
captcha.Place = 0
|
||
}
|
||
|
||
if (captcha.CanvasWidth-10)/2 <= captcha.BlockWidth {
|
||
err = errors.New("请输入符合规范的像素值")
|
||
return
|
||
}
|
||
|
||
if captcha.CanvasHeight-captcha.BlockHeight <= 10 {
|
||
err = errors.New("请输入符合规范的像素值")
|
||
return
|
||
}
|
||
|
||
return
|
||
}
|