|
|
package yunpian
|
|
|
|
|
|
import (
|
|
|
"bytes"
|
|
|
"fmt"
|
|
|
"github.com/astaxie/beego/httplib"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
|
|
|
"net/url"
|
|
|
"openapi/pkg/constant"
|
|
|
"openapi/pkg/infrastructure/sms"
|
|
|
"strconv"
|
|
|
)
|
|
|
|
|
|
type (
|
|
|
YPSmsService struct{}
|
|
|
YPSmsResponse struct {
|
|
|
Code int `json:"code"` //0代表发送成功,其他code代表出错,详细见"返回值说明"页面
|
|
|
Msg string `json:"msg"` //例如""发送成功"",或者相应错误信息
|
|
|
Count int `json:"count"` //发送成功短信的计费条数(计费条数:70个字一条,超出70个字时按每67字一条计费)
|
|
|
Mobile string `json:"string"` //发送手机号
|
|
|
Fee float64 `json:"fee"` //扣费金额,单位:元,类型:双精度浮点型/double
|
|
|
Sid int64 `json:"sid"` //短信id,64位整型
|
|
|
}
|
|
|
)
|
|
|
|
|
|
func (svr *YPSmsService) Send(option *sms.Options) error {
|
|
|
var err error
|
|
|
if option.SendType == sms.SendSingle {
|
|
|
_, err = sendSingle(option)
|
|
|
} else if option.SendType == sms.SendSingleByTpl {
|
|
|
_, err = sendSingleByTpl(option)
|
|
|
}
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
func sendSingle(option *sms.Options) (map[string]interface{}, error) {
|
|
|
var (
|
|
|
resp *YPSmsResponse
|
|
|
err error
|
|
|
url = constant.YunPianSDKHost + constant.SmsSendSingle
|
|
|
)
|
|
|
post := httplib.Post(url)
|
|
|
post.Param("apikey", constant.YunPianAppKey)
|
|
|
post.Param("mobile", option.Phone)
|
|
|
post.Param("text", option.Content)
|
|
|
if err = post.ToJSON(&resp); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
err = resolveResponse(resp, err)
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
func sendSingleByTpl(option *sms.Options) (map[string]interface{}, error) {
|
|
|
var (
|
|
|
resp *YPSmsResponse
|
|
|
err error
|
|
|
url = constant.YunPianSDKHost + constant.SmsSendSingleByTpl
|
|
|
)
|
|
|
post := httplib.Post(url)
|
|
|
post.Param("apikey", constant.YunPianAppKey)
|
|
|
post.Param("mobile", option.Phone)
|
|
|
post.Param("tpl_id", strconv.Itoa(option.TplId))
|
|
|
if len(option.TplValues) > 0 {
|
|
|
post.Param("tpl_value", tplValue(option.TplValues))
|
|
|
log.Debug(tplValue(option.TplValues))
|
|
|
}
|
|
|
if err = post.ToJSON(&resp); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
err = resolveResponse(resp, err)
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
func resolveResponse(resp *YPSmsResponse, err error) error {
|
|
|
if resp.Code != 0 {
|
|
|
log.Error("【sms】 send sms code:", resp.Code, " error msg:", resp.Msg)
|
|
|
err = common.NewErrorWithMsg(1, resp.Msg)
|
|
|
return err
|
|
|
}
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
func tplValue(tplValues map[string]interface{}) string {
|
|
|
var value bytes.Buffer
|
|
|
for k, v := range tplValues {
|
|
|
value.WriteString(url.PathEscape(fmt.Sprintf("#%v#=%v&", k, v)))
|
|
|
}
|
|
|
return string(value.Bytes()[:value.Len()-1])
|
|
|
} |
...
|
...
|
|