|
|
package sms
|
|
|
|
|
|
import (
|
|
|
"ability/protocol"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
|
|
|
"github.com/astaxie/beego"
|
|
|
"github.com/astaxie/beego/httplib"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
|
|
|
)
|
|
|
|
|
|
type ISmsService interface {
|
|
|
Send(request *protocol.SmsCodeRequest) (err error)
|
|
|
GetSmsTpl(code string, content ...interface{}) (string, error)
|
|
|
}
|
|
|
|
|
|
type YunPianSmsService struct{}
|
|
|
|
|
|
func assertImplement() {
|
|
|
var _ ISmsService = (*YunPianSmsService)(nil)
|
|
|
}
|
|
|
|
|
|
//发送
|
|
|
|
|
|
func (s *YunPianSmsService) Send(request *protocol.SmsCodeRequest) (err error) {
|
|
|
var (
|
|
|
resp *YunPianResponse
|
|
|
)
|
|
|
log.Debug("[sms] mobile:", request.Phone, " content:", request.Content)
|
|
|
if beego.BConfig.RunMode != "prod" {
|
|
|
return
|
|
|
}
|
|
|
post := httplib.Post(beego.AppConfig.String("yunpian_sms_sdk_url"))
|
|
|
post.Param("apikey", beego.AppConfig.String("yunpian_app_key"))
|
|
|
post.Param("mobile", request.Phone)
|
|
|
post.Param("text", request.Content)
|
|
|
if err = post.ToJSON(&resp); err != nil {
|
|
|
return
|
|
|
}
|
|
|
if resp.Code != 0 || resp.Mobile != request.Phone {
|
|
|
log.Error("yunpian send sms code:", resp.Code, " error msg:", resp.Msg)
|
|
|
err = common.NewErrorWithMsg(1, resp.Msg)
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func (s *YunPianSmsService) GetSmsTpl(code string, content ...interface{}) (string, error) {
|
|
|
if data, ok := yunPianSmsTpl[code]; !ok {
|
|
|
return fmt.Sprintf(data, content...), nil
|
|
|
}
|
|
|
|
|
|
return "", errors.New("GetSmsTpl err,code=" + code)
|
|
|
}
|
|
|
|
|
|
type YunPianResponse 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位整型
|
|
|
} |
...
|
...
|
|