f
This commit is contained in:
@@ -131,7 +131,7 @@ sms:
|
||||
endpoint: "sms.tencentcloudapi.com" # 可空,默认 sms.tencentcloudapi.com
|
||||
sms_sdk_app_id: "1401111903" # 短信 SdkAppId
|
||||
sign_name: "海宇数科广东横琴科技" # 短信签名
|
||||
template_id: "2631130" # 验证码模板 ID(单变量:验证码)
|
||||
template_id: "2631130" # 验证码模板 ID(优先双变量:验证码、有效分钟;也兼容单变量)
|
||||
# 低余额与欠费为两套模板(变量顺序一般为:企业名、时间、金额)
|
||||
low_balance_template_id: "2631956" # 余额不足预警
|
||||
arrears_template_id: "2631956" # 欠费预警
|
||||
|
||||
@@ -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