优化短信发送逻辑,支持可选业务类型参数
This commit is contained in:
parent
f0afe45aad
commit
761b8371d8
14
const.go
14
const.go
|
@ -35,3 +35,17 @@ var smsBusinessWithRequestPath = map[SmsBusiness]RequestPath{
|
|||
SmsBusinessHs: sendSmsHs,
|
||||
SmsBusinessDefault: sendSms,
|
||||
}
|
||||
|
||||
type (
|
||||
SmsOption func(*SmsOptionData)
|
||||
SmsOptionData struct {
|
||||
Business SmsBusiness
|
||||
}
|
||||
)
|
||||
|
||||
func WithBusiness(business SmsBusiness) SmsOption {
|
||||
|
||||
return func(OptionData *SmsOptionData) {
|
||||
OptionData.Business = business
|
||||
}
|
||||
}
|
||||
|
|
18
msg.go
18
msg.go
|
@ -58,19 +58,27 @@ func (m *MessageCenter) OAGetDetail(outTradeNo string) (data OAGetDetailData, er
|
|||
|
||||
// SendSms 短信
|
||||
// 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 (
|
||||
path RequestPath
|
||||
ex bool
|
||||
e = new(SmsOptionData)
|
||||
)
|
||||
if len(tels) == 0 {
|
||||
err = errors.New("手机号不能为空")
|
||||
return
|
||||
}
|
||||
if path, ex = smsBusinessWithRequestPath[business]; !ex {
|
||||
err = errors.New("未知的供应商")
|
||||
for _, arg := range args {
|
||||
arg(e)
|
||||
}
|
||||
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)
|
||||
err = m.post(path, param, &data)
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue