重构项目结构,将 orm 和 rds 包迁移到 web/globals
This commit is contained in:
@@ -8,7 +8,6 @@ import (
|
||||
"log/slog"
|
||||
"math/rand"
|
||||
"platform/pkg/env"
|
||||
"platform/pkg/rds"
|
||||
"platform/pkg/u"
|
||||
g "platform/web/globals"
|
||||
"strconv"
|
||||
@@ -50,7 +49,7 @@ func (s *verifierService) SendSms(ctx context.Context, phone string, purpose Ver
|
||||
keyLock := key + ":lock"
|
||||
|
||||
// 检查发送频率,1 分钟内只能发送一次
|
||||
err := rds.Client.Watch(ctx, func(tx *redis.Tx) error {
|
||||
err := g.Redis.Watch(ctx, func(tx *redis.Tx) error {
|
||||
result, err := tx.TTL(ctx, keyLock).Result()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -62,7 +61,7 @@ func (s *verifierService) SendSms(ctx context.Context, phone string, purpose Ver
|
||||
return VerifierServiceError("验证码检查异常")
|
||||
}
|
||||
|
||||
pipe := rds.Client.Pipeline()
|
||||
pipe := g.Redis.Pipeline()
|
||||
pipe.Set(ctx, keyLock, "", 1*time.Minute)
|
||||
_, err = pipe.Exec(ctx)
|
||||
if err != nil {
|
||||
@@ -92,19 +91,19 @@ func (s *verifierService) SendSms(ctx context.Context, phone string, purpose Ver
|
||||
TemplateParam: u.P(string(params)),
|
||||
})
|
||||
if err != nil {
|
||||
_ = rds.Client.Del(ctx, key, keyLock).Err()
|
||||
_ = g.Redis.Del(ctx, key, keyLock).Err()
|
||||
return err
|
||||
}
|
||||
if response.Body.Code == nil || *response.Body.Code != "OK" {
|
||||
_ = rds.Client.Del(ctx, key, keyLock).Err()
|
||||
_ = g.Redis.Del(ctx, key, keyLock).Err()
|
||||
return VerifierServiceError("验证码发送失败")
|
||||
}
|
||||
}
|
||||
|
||||
// 设置验证码
|
||||
err = rds.Client.Set(ctx, key, code, 5*time.Minute).Err()
|
||||
err = g.Redis.Set(ctx, key, code, 5*time.Minute).Err()
|
||||
if err != nil {
|
||||
_ = rds.Client.Del(ctx, key, keyLock).Err()
|
||||
_ = g.Redis.Del(ctx, key, keyLock).Err()
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -116,10 +115,10 @@ func (s *verifierService) VerifySms(ctx context.Context, phone, code string) err
|
||||
key := smsKey(phone, VerifierSmsPurposeLogin)
|
||||
keyLock := key + ":lock"
|
||||
|
||||
err := rds.Client.Watch(ctx, func(tx *redis.Tx) error {
|
||||
err := g.Redis.Watch(ctx, func(tx *redis.Tx) error {
|
||||
|
||||
// 检查验证码
|
||||
val, err := rds.Client.Get(ctx, key).Result()
|
||||
val, err := g.Redis.Get(ctx, key).Result()
|
||||
if err != nil && !errors.Is(err, redis.Nil) {
|
||||
slog.Error("验证码获取失败", slog.Any("err", err))
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user