12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- package system
- import (
- "github.com/gin-gonic/gin"
- "github.com/mojocn/base64Captcha"
- "github.com/sirupsen/logrus"
- "go.uber.org/zap"
- "lc-fangdaosha/global"
- "lc-fangdaosha/model/common/response"
- systemResp "lc-fangdaosha/model/system/response"
- "time"
- )
- var store = base64Captcha.DefaultMemStore
- type BaseApi struct{}
- func (b *BaseApi) Captcha(c *gin.Context) {
-
- openCaptcha := global.Config.Captcha.OpenCaptcha
- openCaptchaTimeOut := global.Config.Captcha.OpenCaptchaTimeOut
- key := c.ClientIP()
- v, ok := global.BlackCache.Get(key)
- if !ok {
- global.BlackCache.Set(key, 1, time.Second*time.Duration(openCaptchaTimeOut))
- }
- var oc bool
- if openCaptcha == 0 || openCaptcha < interfaceToInt(v) {
- oc = true
- }
-
-
- driver := base64Captcha.NewDriverDigit(global.Config.Captcha.ImgHeight, global.Config.Captcha.ImgWidth, global.Config.Captcha.KeyLong, 0.7, 80)
-
- cp := base64Captcha.NewCaptcha(driver, store)
- id, b64s, err := cp.Generate()
- if err != nil {
- logrus.Error("验证码获取失败!", zap.Error(err))
- response.FailWithMessage("验证码获取失败", c)
- return
- }
- response.OkWithDetailed(systemResp.SysCaptchaResponse{
- CaptchaId: id,
- PicPath: b64s,
- CaptchaLength: global.Config.Captcha.KeyLong,
- OpenCaptcha: oc,
- }, "验证码获取成功", c)
- }
- func interfaceToInt(v interface{}) (i int) {
- switch v := v.(type) {
- case int:
- i = v
- default:
- i = 0
- }
- return
- }
|