|
@@ -9,8 +9,12 @@ import (
|
|
|
"eta/eta_mini_api/services/go_redis"
|
|
|
"eta/eta_mini_api/utils"
|
|
|
"fmt"
|
|
|
+ "image/color"
|
|
|
+ "strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
|
+
|
|
|
+ "github.com/mojocn/base64Captcha"
|
|
|
)
|
|
|
|
|
|
type UserController struct {
|
|
@@ -115,6 +119,65 @@ func (this *UserAuthController) Login() {
|
|
|
br.Ret = 200
|
|
|
}
|
|
|
|
|
|
+// GenerateCaptcha
|
|
|
+// @Title 生成图形验证码
|
|
|
+// @Description 生成图形验证码
|
|
|
+// @Success 200 Ret=200 获取成功
|
|
|
+// @router /getCaptcha [get]
|
|
|
+func (this *UserController) GenerateCaptcha() {
|
|
|
+ br := new(models.BaseResponse).Init()
|
|
|
+ defer func() {
|
|
|
+ if br.ErrMsg == "" {
|
|
|
+ br.IsSendEmail = false
|
|
|
+ }
|
|
|
+ this.Data["json"] = br
|
|
|
+ this.ServeJSON()
|
|
|
+ }()
|
|
|
+
|
|
|
+ // 自定义验证码样式
|
|
|
+ var driver base64Captcha.Driver
|
|
|
+ driverString := base64Captcha.DriverString{
|
|
|
+ Height: 60, //高度
|
|
|
+ Width: 120, //宽度
|
|
|
+ NoiseCount: 0, //干扰数
|
|
|
+ ShowLineOptions: 2 | 4, //展示个数
|
|
|
+ Length: 4, //长度
|
|
|
+ //Source: "1234567890qwertyuioplkjhgfdsazxcvbnm", //验证码随机字符串来源
|
|
|
+ Source: "1234567890", //验证码随机字符串来源
|
|
|
+ BgColor: &color.RGBA{ // 背景颜色
|
|
|
+ R: 0,
|
|
|
+ G: 0,
|
|
|
+ B: 0,
|
|
|
+ A: 0,
|
|
|
+ },
|
|
|
+ Fonts: []string{"wqy-microhei.ttc"}, // 字体
|
|
|
+ }
|
|
|
+ driver = driverString.ConvertFonts()
|
|
|
+
|
|
|
+ // 生成验证码
|
|
|
+ store := services.CaptchaRedis{}
|
|
|
+ captcha := base64Captcha.NewCaptcha(driver, store)
|
|
|
+ id, b64s, _, err := captcha.Generate()
|
|
|
+ if err != nil {
|
|
|
+ br.Msg = "生成失败"
|
|
|
+ br.ErrMsg = "生成验证码失败, Err: " + err.Error()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ type CaptchaResult struct {
|
|
|
+ Id string
|
|
|
+ Base64Blob string
|
|
|
+ }
|
|
|
+ res := new(CaptchaResult)
|
|
|
+ res.Id = id
|
|
|
+ res.Base64Blob = b64s
|
|
|
+
|
|
|
+ br.Ret = 200
|
|
|
+ br.Success = true
|
|
|
+ br.Msg = "获取成功"
|
|
|
+ br.Data = res
|
|
|
+}
|
|
|
+
|
|
|
// @Title 获取短信/邮箱验证码
|
|
|
// @Description 用户登录
|
|
|
// @Param request body models.LoginReq true "type json string"
|
|
@@ -138,6 +201,10 @@ func (this *UserController) GetVerifyCode() {
|
|
|
br.Msg = "验证方式有误"
|
|
|
br.ErrMsg = fmt.Sprintf("验证方式异常<%d>", req.VerifyType)
|
|
|
}
|
|
|
+ if req.CaptchaId == "" || req.CaptchaCode == "" {
|
|
|
+ br.Msg = "请输入图形验证码"
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
code := utils.GetRandDigit(6)
|
|
|
fmt.Println(code)
|
|
@@ -157,11 +224,24 @@ func (this *UserController) GetVerifyCode() {
|
|
|
}
|
|
|
phoneKey := utils.CACHE_ACCESS_PHONE_LOGIN_CODE + req.AreaCode + req.Phone
|
|
|
res, _ := go_redis.RedisInt(phoneKey)
|
|
|
- if res > 5 {
|
|
|
+ if res >= 5 {
|
|
|
br.Msg = "验证码发送太频繁,请稍后重试"
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+ phoneCountKey := utils.CACHE_ACCESS_PHONE_COUNT_LOGIN_CODE + req.AreaCode + req.Phone
|
|
|
+ resCount, _ := go_redis.RedisInt(phoneCountKey)
|
|
|
+ if resCount >= utils.VerifyCodeSendLimit {
|
|
|
+ br.Msg = fmt.Sprintf("一天最多获取%s次,已超限", strconv.Itoa(utils.VerifyCodeSendLimit))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ store := services.CaptchaRedis{}
|
|
|
var ok bool
|
|
|
+ ok = store.Verify(req.CaptchaId, req.CaptchaCode, true)
|
|
|
+ if !ok {
|
|
|
+ br.Msg = "图形验证码错误"
|
|
|
+ return
|
|
|
+ }
|
|
|
if req.AreaCode == "86" {
|
|
|
ok = services.SendSmsCode(req.Phone, code)
|
|
|
}
|
|
@@ -183,11 +263,14 @@ func (this *UserController) GetVerifyCode() {
|
|
|
return
|
|
|
}
|
|
|
br.Msg = "发送成功"
|
|
|
- isExist := go_redis.IsExist(phoneKey)
|
|
|
- if isExist {
|
|
|
- go_redis.Incr(phoneKey)
|
|
|
- } else {
|
|
|
- go_redis.SetNX(phoneKey, 1, time.Minute*15)
|
|
|
+ phoneVerifyCahcheSvc := &services.VerifyCacheIncrService{}
|
|
|
+ err = phoneVerifyCahcheSvc.VerifyCacheIncr(phoneKey, 15*int(time.Minute.Seconds()))
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info("验证码手机号临时缓存失败", err.Error())
|
|
|
+ }
|
|
|
+ err = phoneVerifyCahcheSvc.VerifyCacheIncr(phoneCountKey, int(utils.SetKeyExpireToday().Seconds()))
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info("验证码手机号当日缓存失败", err.Error())
|
|
|
}
|
|
|
}
|
|
|
case 2:
|
|
@@ -201,11 +284,24 @@ func (this *UserController) GetVerifyCode() {
|
|
|
|
|
|
emailKey := utils.CACHE_ACCESS_EMAIL_LOGIN_CODE + req.Email
|
|
|
res, _ := go_redis.RedisInt(emailKey)
|
|
|
- if res > 5 {
|
|
|
+ if res >= 5 {
|
|
|
br.Msg = "验证码发送太频繁,请稍后重试"
|
|
|
return
|
|
|
}
|
|
|
+ emailCountKey := utils.CACHE_ACCESS_EMAIL_COUNT_LOGIN_CODE + req.Email
|
|
|
+ resCount, _ := go_redis.RedisInt(emailCountKey)
|
|
|
+ if resCount >= utils.VerifyCodeSendLimit {
|
|
|
+ br.Msg = fmt.Sprintf("一天最多获取%s次,已超限", strconv.Itoa(utils.VerifyCodeSendLimit))
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
+ store := services.CaptchaRedis{}
|
|
|
+ var ok bool
|
|
|
+ ok = store.Verify(req.CaptchaId, req.CaptchaCode, true)
|
|
|
+ if !ok {
|
|
|
+ br.Msg = "图形验证码错误"
|
|
|
+ return
|
|
|
+ }
|
|
|
date := time.Now()
|
|
|
content := "尊敬的用户:</br>本次请求的验证码为:" + code + "(为了保障您账号的安全性,请在15分钟内完成验证。)</br>东吴期货研究团队 </br>" + fmt.Sprintf("%d年%02d月%02d日", date.Year(), date.Month(), date.Day())
|
|
|
title := "东吴期货登录验证"
|
|
@@ -229,11 +325,14 @@ func (this *UserController) GetVerifyCode() {
|
|
|
return
|
|
|
}
|
|
|
br.Msg = "发送成功"
|
|
|
- isExist := go_redis.IsExist(emailKey)
|
|
|
- if isExist {
|
|
|
- go_redis.Incr(emailKey)
|
|
|
- } else {
|
|
|
- go_redis.SetNX(emailKey, 1, time.Minute*15)
|
|
|
+ emailVerifyCahcheSvc := &services.VerifyCacheIncrService{}
|
|
|
+ err = emailVerifyCahcheSvc.VerifyCacheIncr(emailKey, 15*int(time.Minute.Seconds()))
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info("验证码邮箱临时缓存失败, err:", err.Error())
|
|
|
+ }
|
|
|
+ err = emailVerifyCahcheSvc.VerifyCacheIncr(emailCountKey, int(utils.SetKeyExpireToday().Seconds()))
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info("验证码邮箱当日缓存失败, err:", err.Error())
|
|
|
}
|
|
|
} else {
|
|
|
br.Msg = "发送失败"
|