优化短信发送逻辑,支持可选业务类型参数

This commit is contained in:
renzhiyuan 2025-04-08 11:04:41 +08:00
parent f0afe45aad
commit 761b8371d8
2 changed files with 28 additions and 6 deletions

View File

@ -35,3 +35,17 @@ var smsBusinessWithRequestPath = map[SmsBusiness]RequestPath{
SmsBusinessHs: sendSmsHs, SmsBusinessHs: sendSmsHs,
SmsBusinessDefault: sendSms, SmsBusinessDefault: sendSms,
} }
type (
SmsOption func(*SmsOptionData)
SmsOptionData struct {
Business SmsBusiness
}
)
func WithBusiness(business SmsBusiness) SmsOption {
return func(OptionData *SmsOptionData) {
OptionData.Business = business
}
}

20
msg.go
View File

@ -58,19 +58,27 @@ func (m *MessageCenter) OAGetDetail(outTradeNo string) (data OAGetDetailData, er
// SendSms 短信 // SendSms 短信
// business SmsBusiness // business SmsBusiness
func (m *MessageCenter) SendSms(tels []string, jsonParam string, business SmsBusiness) (data SmsSend, err error) { func (m *MessageCenter) SendSms(tels []string, jsonParam string, args ...SmsOption) (data SmsSend, err error) {
var ( var (
path RequestPath e = new(SmsOptionData)
ex bool
) )
if len(tels) == 0 { if len(tels) == 0 {
err = errors.New("手机号不能为空") err = errors.New("手机号不能为空")
return return
} }
if path, ex = smsBusinessWithRequestPath[business]; !ex { for _, arg := range args {
err = errors.New("未知的供应商") arg(e)
return
} }
if e.Business != "" {
if _, ex := smsBusinessWithRequestPath[e.Business]; !ex {
err = errors.New("business参数错误")
return
}
} else {
e.Business = SmsBusinessDefault
}
path := smsBusinessWithRequestPath[e.Business]
param := m.parseSmsSendParam(tels, jsonParam) param := m.parseSmsSendParam(tels, jsonParam)
err = m.post(path, param, &data) err = m.post(path, param, &data)
if err != nil { if err != nil {