f
This commit is contained in:
@@ -62,14 +62,16 @@ func normalizeTencentPhone(phone string) string {
|
||||
return "+86" + phone
|
||||
}
|
||||
|
||||
// SendVerificationCode 发送验证码(模板参数为单个验证码,与 bdqr 一致)
|
||||
// SendVerificationCode 发送验证码。
|
||||
// 默认按双参数模板发送:验证码 + 有效分钟数;若模板为单参数则自动降级重试。
|
||||
func (s *TencentSMSService) SendVerificationCode(ctx context.Context, phone string, code string) error {
|
||||
tc := s.cfg.TencentCloud
|
||||
expireMinutes := getExpireMinutes(s.cfg.ExpireTime)
|
||||
request := &sms.SendSmsRequest{}
|
||||
request.SmsSdkAppId = common.StringPtr(tc.SmsSdkAppId)
|
||||
request.SignName = common.StringPtr(tc.SignName)
|
||||
request.TemplateId = common.StringPtr(tc.TemplateID)
|
||||
request.TemplateParamSet = common.StringPtrs([]string{code})
|
||||
request.TemplateParamSet = common.StringPtrs([]string{code, fmt.Sprintf("%d", expireMinutes)})
|
||||
request.PhoneNumberSet = common.StringPtrs([]string{normalizeTencentPhone(phone)})
|
||||
|
||||
response, err := s.client.SendSms(request)
|
||||
@@ -91,32 +93,28 @@ func (s *TencentSMSService) SendVerificationCode(ctx context.Context, phone stri
|
||||
msg = *st.Message
|
||||
}
|
||||
|
||||
// 兼容模板变量从单参数升级为双参数(验证码 + 有效分钟数)的场景。
|
||||
// 命中“内容与模板不符”时,自动使用双参数重试一次,避免生产发布被模板变更阻断。
|
||||
// 兼容单参数模板:若当前双参数发送命中“内容与模板不符”,自动用单参数重试一次。
|
||||
if isTemplateContentMismatch(msg) {
|
||||
expireMinutes := getExpireMinutes(s.cfg.ExpireTime)
|
||||
retryReq := &sms.SendSmsRequest{}
|
||||
retryReq.SmsSdkAppId = common.StringPtr(tc.SmsSdkAppId)
|
||||
retryReq.SignName = common.StringPtr(tc.SignName)
|
||||
retryReq.TemplateId = common.StringPtr(tc.TemplateID)
|
||||
retryReq.TemplateParamSet = common.StringPtrs([]string{code, fmt.Sprintf("%d", expireMinutes)})
|
||||
retryReq.TemplateParamSet = common.StringPtrs([]string{code})
|
||||
retryReq.PhoneNumberSet = common.StringPtrs([]string{normalizeTencentPhone(phone)})
|
||||
|
||||
retryResp, retryErr := s.client.SendSms(retryReq)
|
||||
if retryErr == nil && retryResp != nil && len(retryResp.Response.SendStatusSet) > 0 {
|
||||
retryStatus := retryResp.Response.SendStatusSet[0]
|
||||
if retryStatus.Code != nil && *retryStatus.Code == "Ok" {
|
||||
s.logger.Info("腾讯云短信发送成功(模板双参数重试)",
|
||||
s.logger.Info("腾讯云短信发送成功(模板单参数降级重试)",
|
||||
zap.String("phone", phone),
|
||||
zap.Int64("expire_minutes", expireMinutes),
|
||||
zap.String("serial_no", safeStrPtr(retryStatus.SerialNo)))
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
s.logger.Error("腾讯云短信双参数重试失败",
|
||||
s.logger.Error("腾讯云短信单参数降级重试失败",
|
||||
zap.String("phone", phone),
|
||||
zap.Int64("expire_minutes", expireMinutes),
|
||||
zap.Error(retryErr))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user