You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1013 B
Go

package captcha
import (
"git.noahlan.cn/n-admin/n-admin-server/dal/errx"
"git.noahlan.cn/noahlan/ntool-biz/core/captcha"
"git.noahlan.cn/noahlan/ntool-biz/core/config"
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus"
"github.com/mojocn/base64Captcha"
"strings"
)
type CaptchaMgr struct {
Captcha *base64Captcha.Captcha
}
func NewCaptchaManager(config captcha.Config, redisConf config.RedisConf) *CaptchaMgr {
return &CaptchaMgr{
Captcha: captcha.MustNewRedisCaptcha(config, redisConf),
}
}
func (c *CaptchaMgr) Verify(captchaId, captchaStr string, clear bool) error {
if captchaId != "" && captchaStr != "" {
if ok := c.Captcha.Verify("CAPTCHA_"+captchaId, strings.ToLower(captchaStr), clear); !ok {
return nstatus.NewBizErrWithCode(errx.InvalidCaptchaErr)
}
}
return nil
}
func (c *CaptchaMgr) Generate() (id, b64s string, err error) {
id, b64s, err = c.Captcha.Generate()
if err != nil {
return "", "", nstatus.NewBizErrWithCode(errx.CaptchaGenerateErr)
}
return id, b64s, nil
}