From e2d9685db36139b950fba44be9f5f5b5e8c045ac Mon Sep 17 00:00:00 2001 From: lzh <18320341470> Date: Wed, 18 Jun 2025 17:37:21 +0800 Subject: [PATCH] sms_tool --- sms_tool/cache_model.go | 1 + sms_tool/sms_client.go | 36 ++++++++---------------------------- sms_tool/sms_client_test.go | 8 ++++++++ 3 files changed, 17 insertions(+), 28 deletions(-) diff --git a/sms_tool/cache_model.go b/sms_tool/cache_model.go index f06b667..b93e539 100644 --- a/sms_tool/cache_model.go +++ b/sms_tool/cache_model.go @@ -9,4 +9,5 @@ type ICacheAdapter interface { Get(ctx context.Context, key string) (value interface{}, err error) Set(ctx context.Context, key string, value interface{}, expire time.Duration) (err error) Del(ctx context.Context, key string) (err error) + SetNX(ctx context.Context, key string, value interface{}, expire time.Duration) (ok bool, err error) } diff --git a/sms_tool/sms_client.go b/sms_tool/sms_client.go index 1979a65..d0f815b 100644 --- a/sms_tool/sms_client.go +++ b/sms_tool/sms_client.go @@ -8,7 +8,6 @@ import ( dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v5/client" util "github.com/alibabacloud-go/tea-utils/v2/service" "github.com/alibabacloud-go/tea/tea" - "github.com/redis/go-redis/v9" "time" ) @@ -81,10 +80,7 @@ func (c *SmsClient) SaveCode(ctx context.Context, key string, code string, expir } //保存code err = c.Cache.Set(ctx, key, code, expire) - if err != nil { - return err - } - return nil + return err } // DeleteCode 删除验证码 @@ -102,34 +98,18 @@ func (c *SmsClient) SaveCodeWithFrequency(ctx context.Context, key string, code return errors.New("缓存不能为空") } frequencyKey := fmt.Sprintf(CodeFrequencyKey, key) - if frequency != 0 { - // 判断验证码是否频繁 - frequencyValue, err := c.Cache.Get(ctx, frequencyKey) - if err != nil { - if errors.Is(err, redis.Nil) { - // 缓存中没有数据 - fmt.Printf("缓存中没有数据") - } else { - return err - } - } - if frequencyValue != nil && frequencyValue.(string) == CodeFrequencyValue { - return errors.New("code发送频繁,请稍后再试") - } + if frequency <= 0 { + return errors.New("频率不能小于0") } - //保存code - err = c.SaveCode(ctx, key, code, expire) + ok, err := c.Cache.SetNX(ctx, frequencyKey, CodeFrequencyValue, frequency) if err != nil { return err } - if frequency != 0 { - // 保存验证码发送频率 - err = c.Cache.Set(ctx, frequencyKey, CodeFrequencyValue, frequency) - if err != nil { - return err - } + if !ok { + return errors.New("code发送频繁,请稍后再试") } - return nil + err = c.SaveCode(ctx, key, code, expire) + return err } // VerifyCode 校验验证码 diff --git a/sms_tool/sms_client_test.go b/sms_tool/sms_client_test.go index e3d44b7..95bbf6c 100644 --- a/sms_tool/sms_client_test.go +++ b/sms_tool/sms_client_test.go @@ -51,6 +51,14 @@ func (r *RedisCacheAdapter) Del(ctx context.Context, key string) error { return r.client.Del(ctx, key).Err() } +func (r *RedisCacheAdapter) SetNX(ctx context.Context, key string, value interface{}, expire time.Duration) (ok bool, err error) { + data, err := json.Marshal(value) + if err != nil { + return false, err + } + return r.client.SetNX(ctx, key, data, expire).Result() +} + var ( //SMS accessKeyId = os.Getenv("SMS_ALIBABA_CLOUD_ACCESS_KEY_ID")