This commit is contained in:
lzh 2025-06-18 17:37:21 +08:00
parent 695dcf3c49
commit e2d9685db3
3 changed files with 17 additions and 28 deletions

View File

@ -9,4 +9,5 @@ type ICacheAdapter interface {
Get(ctx context.Context, key string) (value interface{}, err error) Get(ctx context.Context, key string) (value interface{}, err error)
Set(ctx context.Context, key string, value interface{}, expire time.Duration) (err error) Set(ctx context.Context, key string, value interface{}, expire time.Duration) (err error)
Del(ctx context.Context, key string) (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)
} }

View File

@ -8,7 +8,6 @@ import (
dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v5/client" dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v5/client"
util "github.com/alibabacloud-go/tea-utils/v2/service" util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea" "github.com/alibabacloud-go/tea/tea"
"github.com/redis/go-redis/v9"
"time" "time"
) )
@ -81,11 +80,8 @@ func (c *SmsClient) SaveCode(ctx context.Context, key string, code string, expir
} }
//保存code //保存code
err = c.Cache.Set(ctx, key, code, expire) err = c.Cache.Set(ctx, key, code, expire)
if err != nil {
return err return err
} }
return nil
}
// DeleteCode 删除验证码 // DeleteCode 删除验证码
func (c *SmsClient) DeleteCode(ctx context.Context, key string) (err error) { func (c *SmsClient) DeleteCode(ctx context.Context, key string) (err error) {
@ -102,35 +98,19 @@ func (c *SmsClient) SaveCodeWithFrequency(ctx context.Context, key string, code
return errors.New("缓存不能为空") return errors.New("缓存不能为空")
} }
frequencyKey := fmt.Sprintf(CodeFrequencyKey, key) frequencyKey := fmt.Sprintf(CodeFrequencyKey, key)
if frequency != 0 { if frequency <= 0 {
// 判断验证码是否频繁 return errors.New("频率不能小于0")
frequencyValue, err := c.Cache.Get(ctx, frequencyKey) }
ok, err := c.Cache.SetNX(ctx, frequencyKey, CodeFrequencyValue, frequency)
if err != nil { if err != nil {
if errors.Is(err, redis.Nil) {
// 缓存中没有数据
fmt.Printf("缓存中没有数据")
} else {
return err return err
} }
} if !ok {
if frequencyValue != nil && frequencyValue.(string) == CodeFrequencyValue {
return errors.New("code发送频繁请稍后再试") return errors.New("code发送频繁请稍后再试")
} }
}
//保存code
err = c.SaveCode(ctx, key, code, expire) err = c.SaveCode(ctx, key, code, expire)
if err != nil {
return err return err
} }
if frequency != 0 {
// 保存验证码发送频率
err = c.Cache.Set(ctx, frequencyKey, CodeFrequencyValue, frequency)
if err != nil {
return err
}
}
return nil
}
// VerifyCode 校验验证码 // VerifyCode 校验验证码
func (c *SmsClient) VerifyCode(ctx context.Context, key string, verifyCode string) (ok bool, err error) { func (c *SmsClient) VerifyCode(ctx context.Context, key string, verifyCode string) (ok bool, err error) {

View File

@ -51,6 +51,14 @@ func (r *RedisCacheAdapter) Del(ctx context.Context, key string) error {
return r.client.Del(ctx, key).Err() 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 ( var (
//SMS //SMS
accessKeyId = os.Getenv("SMS_ALIBABA_CLOUD_ACCESS_KEY_ID") accessKeyId = os.Getenv("SMS_ALIBABA_CLOUD_ACCESS_KEY_ID")