f
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
||||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
|
||||||
@@ -89,6 +90,36 @@ func (s *TencentSMSService) SendVerificationCode(ctx context.Context, phone stri
|
|||||||
if st.Message != nil {
|
if st.Message != nil {
|
||||||
msg = *st.Message
|
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.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("腾讯云短信发送成功(模板双参数重试)",
|
||||||
|
zap.String("phone", phone),
|
||||||
|
zap.Int64("expire_minutes", expireMinutes),
|
||||||
|
zap.String("serial_no", safeStrPtr(retryStatus.SerialNo)))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
s.logger.Error("腾讯云短信双参数重试失败",
|
||||||
|
zap.String("phone", phone),
|
||||||
|
zap.Int64("expire_minutes", expireMinutes),
|
||||||
|
zap.Error(retryErr))
|
||||||
|
}
|
||||||
|
|
||||||
s.logger.Error("腾讯云短信业务失败",
|
s.logger.Error("腾讯云短信业务失败",
|
||||||
zap.String("phone", phone),
|
zap.String("phone", phone),
|
||||||
zap.String("message", msg))
|
zap.String("message", msg))
|
||||||
@@ -185,3 +216,20 @@ func resolveTencentBalanceTemplateID(tc config.TencentSMSConfig, alertType strin
|
|||||||
}
|
}
|
||||||
return tc.BalanceAlertTemplateID
|
return tc.BalanceAlertTemplateID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isTemplateContentMismatch(msg string) bool {
|
||||||
|
m := strings.ToLower(strings.TrimSpace(msg))
|
||||||
|
return strings.Contains(m, "request content does not match template content") ||
|
||||||
|
strings.Contains(m, "内容与模板不符")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getExpireMinutes(expire time.Duration) int64 {
|
||||||
|
if expire <= 0 {
|
||||||
|
return 5
|
||||||
|
}
|
||||||
|
minutes := int64(expire / time.Minute)
|
||||||
|
if minutes <= 0 {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return minutes
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user