新增短信限制
This commit is contained in:
@ -3,6 +3,7 @@ package consts
|
|||||||
// 用户
|
// 用户
|
||||||
const (
|
const (
|
||||||
UserBindPhoneKey = "user:bindPhone:%s"
|
UserBindPhoneKey = "user:bindPhone:%s"
|
||||||
|
UserBindPhoneLimitKey = "user:bindPhoneLimit:%s"
|
||||||
UserCodeExpire = 5 * 60
|
UserCodeExpire = 5 * 60
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
|
"github.com/gogf/gf/v2/os/glog"
|
||||||
"github.com/gogf/gf/v2/os/gtime"
|
"github.com/gogf/gf/v2/os/gtime"
|
||||||
"github.com/gogf/gf/v2/util/grand"
|
"github.com/gogf/gf/v2/util/grand"
|
||||||
"server/internal/consts"
|
"server/internal/consts"
|
||||||
@ -164,6 +165,23 @@ func (s *sUser) Info(ctx context.Context, in *model.UserInfoIn) (out *model.User
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *sUser) Code(ctx context.Context, in *model.GetPhoneCodeIn) (out *model.GetPhoneCodeOut, err error) {
|
func (s *sUser) Code(ctx context.Context, in *model.GetPhoneCodeIn) (out *model.GetPhoneCodeOut, err error) {
|
||||||
|
|
||||||
|
// 限制 1min只能调用一次
|
||||||
|
get, err := g.Redis().Get(ctx, fmt.Sprintf(consts.UserBindPhoneLimitKey, in.Phone))
|
||||||
|
if err != nil {
|
||||||
|
glog.Errorf(ctx, "Redis 获取绑定手机限制失败: %v", err)
|
||||||
|
return nil, ecode.Fail.Sub("Redis 获取绑定手机限制失败")
|
||||||
|
}
|
||||||
|
if get.IsEmpty() {
|
||||||
|
err = g.Redis().SetEX(ctx, fmt.Sprintf(consts.UserBindPhoneLimitKey, in.Phone), "1", consts.UserCodeExpire)
|
||||||
|
if err != nil {
|
||||||
|
glog.Errorf(ctx, "Redis 设置绑定手机限制失败: %v", err)
|
||||||
|
return nil, ecode.Fail.Sub("Redis 设置绑定手机限制失败")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return nil, ecode.InvalidOperation.Sub("请勿重复请求绑定手机验证码")
|
||||||
|
}
|
||||||
|
|
||||||
code := grand.Digits(6)
|
code := grand.Digits(6)
|
||||||
|
|
||||||
// 存入 redis
|
// 存入 redis
|
||||||
|
|||||||
Reference in New Issue
Block a user