@@ -26,6 +26,11 @@ import (
func Send ( ctx context . Context , in * pb . SmsSendRequest ) ( reply * pb . SmsReply , err error ) {
var smsCode string
// 验证码配置缺失时返回明确错误,避免运行期空指针 panic
if config . Spec . Code == nil {
return nil , excode . ErrCodeConfig
}
if in . GetPhone ( ) == "" || ! VerifyPhone ( in . GetPhone ( ) ) {
return nil , excode . ErrPhone
}
@@ -33,25 +38,27 @@ func Send(ctx context.Context, in *pb.SmsSendRequest) (reply *pb.SmsReply, err e
return nil , excode . ErrTemplate
}
// 是否验证黑名单
if impl . RedisService . Client . SIsMember ( impl . RedisService . Ctx , BlackListCacheKey , in . GetPhone ( ) ) . Val ( ) {
// 是否验证黑名单: Redis 黑名单集合与配置黑名单任一命中即拦截
if impl . RedisService . Client . SIsMember ( impl . RedisService . Ctx , BlackListCacheKey , in . GetPhone ( ) ) . Val ( ) ||
inBlackListConf ( in . GetPhone ( ) ) {
return nil , excode . ErrInBlackList
}
// 每天限制
limitKey := LimitCacheKey + time . Now ( ) . Format ( FormatDay ) + in . GetPhone ( )
// check limit
// check limit:发送前校验,达到上限即拒绝
twice , err := impl . RedisService . Client . Get ( impl . RedisService . Ctx , limitKey ) . Int ( )
if err != nil && ! errors . Is ( err , redis . Nil ) {
return nil , errcode . ErrRedis
}
if twice > config . Spec . Code . MaxSentLimit {
if config . Spec . Code . MaxSentLimit > 0 && twice >= config . Spec . Code . MaxSentLimit {
return nil , excode . ErrSentLimit
}
// 从redis获取验证码, 如果没有重新生成
key := KeyPrefix + in . GetPhone ( )
expire := time . Second * time . Duration ( config . Spec . Code . Expire )
if in . GetIsGenCode ( ) {
//验证码最少4位, 最大10位。
if config . Spec . Code . Length < 4 || config . Spec . Code . Length > 10 {
@@ -59,20 +66,40 @@ func Send(ctx context.Context, in *pb.SmsSendRequest) (reply *pb.SmsReply, err e
}
// 新生成验证码
sms Code = GenValidateCode ( config . Spec . Code . Length )
new Code : = GenValidateCode ( config . Spec . Code . Length )
//sms code write to redis
expire := t ime . Second * time . Duration ( config . Spec . Code. E xpire)
impl . RedisService . Client . SetNX ( impl . RedisService . Ctx , key , smsCode , expire )
} else {
// 获取验证码
if code , ok := in . Paramters [ "code" ] ; ok {
smsCode = code
} else {
return nil , excode . ErrCode
ok , err := impl . RedisService . Client . SetNX ( impl . RedisService . Ctx , key , new Code, e xpire) . Result ( )
if err != nil {
return nil , errcode . ErrRedis
}
if ok {
smsCode = newCode
// 新码写入成功,重置该号码的校验失败计数
impl . RedisService . Client . Del ( impl . RedisService . Ctx , VerifyFailCacheKey + in . GetPhone ( ) )
} else {
// 键已存在: 不覆盖Redis中的旧码, 必须发送实际保存的验证码, 否则用户收码后校验不过
exist , err := impl . RedisService . Client . Get ( impl . RedisService . Ctx , key ) . Result ( )
if err != nil {
return nil , errcode . ErrRedis
}
smsCode = exist
}
} else {
// 不生成新码时仅允许复用Redis中已有的验证码, 禁止调用方自行指定验证码内容
exist , err := impl . RedisService . Client . Get ( impl . RedisService . Ctx , key ) . Result ( )
if err != nil {
if errors . Is ( err , redis . Nil ) {
return nil , excode . ErrExpired
}
return nil , errcode . ErrRedis
}
smsCode = exist
}
var result map [ string ] interface { }
if impl . Provider == nil {
return nil , excode . ErrProviderIsNil
}
switch strings . ToLower ( in . GetProvider ( ) ) {
case "aliyun" :
if impl . Provider . Aliyun == nil {
@@ -92,6 +119,17 @@ func Send(ctx context.Context, in *pb.SmsSendRequest) (reply *pb.SmsReply, err e
return nil , err
}
// 未返回发送结果的实现不能视为发送成功
if result == nil {
return nil , excode . ErrNotImplement
}
// 发送成功后累计当日发送量,并在首次计数时把过期时间设到当天结束
count := impl . RedisService . Client . Incr ( impl . RedisService . Ctx , limitKey ) . Val ( )
if count == 1 {
impl . RedisService . Client . ExpireAt ( impl . RedisService . Ctx , limitKey , endOfDay ( time . Now ( ) ) )
}
jsonBytes , _ := json . Marshal ( result )
fmt . Println ( "短信发送结果:" , string ( jsonBytes ) )
return & pb . SmsReply {
@@ -99,12 +137,31 @@ func Send(ctx context.Context, in *pb.SmsSendRequest) (reply *pb.SmsReply, err e
} , nil
}
// inBlackListConf 判断号码是否命中配置中的短信黑名单
func inBlackListConf ( phone string ) bool {
for _ , v := range config . Spec . Code . BlackListFilter {
if v == phone {
return true
}
}
return false
}
// endOfDay 返回当天23:59:59, 用于设置日发送量计数键的过期时间
func endOfDay ( t time . Time ) time . Time {
return time . Date ( t . Year ( ) , t . Month ( ) , t . Day ( ) , 23 , 59 , 59 , 0 , t . Location ( ) )
}
func AliyunSender ( args * pb . SmsSendRequest , code string ) ( map [ string ] any , error ) {
// 构建发送参数
var templateParam = map [ string ] any {
"code" : code ,
}
for key , val := range args . Paramters {
// 验证码字段由服务端生成或从Redis读取, 禁止调用方覆盖
if key == "code" {
continue
}
templateParam [ key ] = val
}
jsonBytes , _ := json . Marshal ( templateParam )
@@ -149,7 +206,9 @@ func AliyunSender(args *pb.SmsSendRequest, code string) (map[string]any, error)
}
func TencentSender ( args * pb . SmsSendRequest ) ( map [ string ] any , error ) {
return nil , nil
// 腾讯云短信需要账号级 SmsSdkAppId 与有序的模板参数,现有 proto/config 均无法提供,
// 因此显式返回未实现错误,绝不能返回 nil,nil 让调用方误判为发送成功。
return nil , excode . ErrNotImplement
}
func VerifyPhone ( phone string ) bool {