初始化提交
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/mojocn/base64Captcha"
|
||||
"hospital-admin-api/api/requests"
|
||||
"image/color"
|
||||
"time"
|
||||
)
|
||||
|
||||
// GenerateCaptcha 生成验证码-base64
|
||||
func GenerateCaptcha() (id, b64s string, err error) {
|
||||
var driver *base64Captcha.DriverString
|
||||
// 配置验证码的参数
|
||||
driverString := &base64Captcha.DriverString{
|
||||
Height: 40,
|
||||
Width: 100,
|
||||
NoiseCount: 0,
|
||||
ShowLineOptions: 0,
|
||||
Length: 4,
|
||||
Source: "1234567890",
|
||||
BgColor: &color.RGBA{R: 3, G: 102, B: 214, A: 125},
|
||||
Fonts: []string{"wqy-microhei.ttc"},
|
||||
}
|
||||
// ConvertFonts 按名称加载字体
|
||||
driver = driverString.ConvertFonts()
|
||||
|
||||
base64Captcha.Expiration = 30 * time.Minute
|
||||
store := base64Captcha.DefaultMemStore
|
||||
|
||||
captcha := base64Captcha.NewCaptcha(driver, store)
|
||||
id, b64s, err = captcha.Generate()
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return id, b64s, nil
|
||||
}
|
||||
|
||||
// VerifyCaptcha 验证验证码
|
||||
func VerifyCaptcha(login requests.Login) bool {
|
||||
// 创建验证码实例
|
||||
base64Captcha.Expiration = 30 * time.Minute
|
||||
store := base64Captcha.DefaultMemStore
|
||||
captcha := base64Captcha.NewCaptcha(nil, store)
|
||||
|
||||
// 验证验证码
|
||||
isValid := captcha.Verify(login.CaptchaId, login.Captcha, true)
|
||||
if !isValid {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user