优化短信发送逻辑,增加错误处理和业务类型支持
This commit is contained in:
parent
877a9e10d3
commit
f0afe45aad
25
const.go
25
const.go
|
@ -13,10 +13,25 @@ const (
|
||||||
DDDateField = "DDDateField"
|
DDDateField = "DDDateField"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type RequestPath string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
accessToken = "/oauth/v1/accesstoken"
|
accessToken RequestPath = "/oauth/v1/accesstoken"
|
||||||
oaCreat = "/msg/v1/dingtalk/oa/create"
|
oaCreat RequestPath = "/msg/v1/dingtalk/oa/create"
|
||||||
oaGet = "/msg/v1/dingtalk/oa/get"
|
oaGet RequestPath = "/msg/v1/dingtalk/oa/get"
|
||||||
oaComment = "/msg/v1/dingtalk/oa/comment"
|
oaComment RequestPath = "/msg/v1/dingtalk/oa/comment"
|
||||||
sendSms = "/msg/v1/sms/send"
|
sendSms RequestPath = "/msg/v1/sms/send"
|
||||||
|
sendSmsHs RequestPath = "/msg/v1/sms/send/hs"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type SmsBusiness string
|
||||||
|
|
||||||
|
const (
|
||||||
|
SmsBusinessHs SmsBusiness = "hs"
|
||||||
|
SmsBusinessDefault SmsBusiness = "aliyun"
|
||||||
|
)
|
||||||
|
|
||||||
|
var smsBusinessWithRequestPath = map[SmsBusiness]RequestPath{
|
||||||
|
SmsBusinessHs: sendSmsHs,
|
||||||
|
SmsBusinessDefault: sendSms,
|
||||||
|
}
|
||||||
|
|
23
msg.go
23
msg.go
|
@ -2,6 +2,7 @@ package l_msg_api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MessageCenter struct {
|
type MessageCenter struct {
|
||||||
|
@ -31,7 +32,7 @@ func NewMessageCenter(host, clientKey, clientSecret, serverIndex, tempIndex stri
|
||||||
|
|
||||||
// OACreate 发起OA审批
|
// OACreate 发起OA审批
|
||||||
func (m *MessageCenter) OACreate(dTalkUserId, treadNo string, formModel *FormsData) (data OAResponse, err error) {
|
func (m *MessageCenter) OACreate(dTalkUserId, treadNo string, formModel *FormsData) (data OAResponse, err error) {
|
||||||
formModel.FormBase = FormBase{
|
formModel.formBase = formBase{
|
||||||
OutTradeNo: treadNo,
|
OutTradeNo: treadNo,
|
||||||
OriginatorUserId: dTalkUserId,
|
OriginatorUserId: dTalkUserId,
|
||||||
}
|
}
|
||||||
|
@ -56,18 +57,28 @@ func (m *MessageCenter) OAGetDetail(outTradeNo string) (data OAGetDetailData, er
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendSms 短信
|
// SendSms 短信
|
||||||
func (m *MessageCenter) SendSms(tels []string, jsonParam string) (data SmsSend, err error) {
|
// business SmsBusiness
|
||||||
|
func (m *MessageCenter) SendSms(tels []string, jsonParam string, business SmsBusiness) (data SmsSend, err error) {
|
||||||
|
var (
|
||||||
|
path RequestPath
|
||||||
|
ex bool
|
||||||
|
)
|
||||||
|
if len(tels) == 0 {
|
||||||
|
err = errors.New("手机号不能为空")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if path, ex = smsBusinessWithRequestPath[business]; !ex {
|
||||||
|
err = errors.New("未知的供应商")
|
||||||
|
return
|
||||||
|
}
|
||||||
param := m.parseSmsSendParam(tels, jsonParam)
|
param := m.parseSmsSendParam(tels, jsonParam)
|
||||||
err = m.post(sendSms, param, &data)
|
err = m.post(path, param, &data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// BlackboardReceiverView struct {
|
|
||||||
|
|
||||||
// }
|
|
||||||
// SendBlackBoard 钉钉公告
|
// SendBlackBoard 钉钉公告
|
||||||
// deptidList //接收部门ID列表,最大的列表长度为20。
|
// deptidList //接收部门ID列表,最大的列表长度为20。
|
||||||
// UseridList //接收部用户ID列表,最大的列表长度为20。
|
// UseridList //接收部用户ID列表,最大的列表长度为20。
|
||||||
|
|
|
@ -14,7 +14,7 @@ func (m *MessageCenter) parseOACreateParam(formModel *FormsData) (out []byte) {
|
||||||
}
|
}
|
||||||
req.FormComponentValues = formModel.FormComponentValues
|
req.FormComponentValues = formModel.FormComponentValues
|
||||||
req.Finance = formModel.Finance
|
req.Finance = formModel.Finance
|
||||||
req.FormBase = formModel.FormBase
|
req.formBase = formModel.formBase
|
||||||
out, _ = json.Marshal(req)
|
out, _ = json.Marshal(req)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,7 @@ func (m *MessageCenter) getAccessToken() (string, error) {
|
||||||
return data.AccessToken, err
|
return data.AccessToken, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MessageCenter) post(path string, data []byte, resReflect interface{}) (err error) {
|
func (m *MessageCenter) post(path RequestPath, data []byte, resReflect interface{}) (err error) {
|
||||||
var body responseBody
|
var body responseBody
|
||||||
res, err := httpclient.FastHttpPost(fmt.Sprintf("%s%s", m.Host, path), m.header, data, timeOut)
|
res, err := httpclient.FastHttpPost(fmt.Sprintf("%s%s", m.Host, path), m.header, data, timeOut)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -77,7 +77,7 @@ func (m *MessageCenter) post(path string, data []byte, resReflect interface{}) (
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MessageCenter) accessPost(path string, data []byte, resReflect interface{}) (err error) {
|
func (m *MessageCenter) accessPost(path RequestPath, data []byte, resReflect interface{}) (err error) {
|
||||||
var body responseBody
|
var body responseBody
|
||||||
res, err := httpclient.FastHttpPost(fmt.Sprintf("%s%s", m.Host, path), m.header, data, timeOut)
|
res, err := httpclient.FastHttpPost(fmt.Sprintf("%s%s", m.Host, path), m.header, data, timeOut)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue