goweb-gin-demo/api/web/captcha.go

42 lines
1.4 KiB
Go
Raw Normal View History

2021-10-29 14:39:50 +08:00
package web
import (
"github.com/gin-gonic/gin"
"github.com/mojocn/base64Captcha"
2021-11-02 17:48:39 +08:00
"go.uber.org/zap"
2021-10-29 14:44:41 +08:00
"goweb-gin-demo/global"
"goweb-gin-demo/model/common/response"
webRes "goweb-gin-demo/model/web/response"
2021-10-29 14:39:50 +08:00
)
// 当开启多服务器部署时替换下面的配置使用redis共享存储验证码
//var store = captcha.NewDefaultRedisStore()
var store = base64Captcha.DefaultMemStore
type BaseApi struct {
}
// @Tags Base
// @Summary 生成验证码
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"验证码获取成功"}"
// @Router /base/captcha [post]
func (b *BaseApi) Captcha(c *gin.Context) {
// 字符,公式,验证码配置
// 生成默认数字的driver
driver := base64Captcha.NewDriverDigit(global.GLOBAL_CONFIG.Captcha.ImgHeight, global.GLOBAL_CONFIG.Captcha.ImgWidth, global.GLOBAL_CONFIG.Captcha.KeyLong, 0.7, 80)
//cp := base64Captcha.NewCaptcha(driver, store.UseWithCtx(c)) // v8下使用redis
cp := base64Captcha.NewCaptcha(driver, store)
if id, b64s, err := cp.Generate(); err != nil {
global.GLOBAL_LOG.Error("验证码获取失败!", zap.Any("err", err))
response.FailWithMessage("验证码获取失败", c)
} else {
response.OkWithDetailed(webRes.SysCaptchaResponse{
CaptchaId: id,
PicPath: b64s,
}, "验证码获取成功", c)
}
}