新增短信限制

This commit is contained in:
chy
2025-06-24 16:41:58 +08:00
parent 8342c92247
commit 2b1fd69601
2 changed files with 21 additions and 2 deletions

View File

@ -3,6 +3,7 @@ package consts
// 用户
const (
UserBindPhoneKey = "user:bindPhone:%s"
UserBindPhoneLimitKey = "user:bindPhoneLimit:%s"
UserCodeExpire = 5 * 60
)

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"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/util/grand"
"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) {
// 限制 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)
// 存入 redis