|
|
package sms
|
|
|
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
"errors"
|
|
|
)
|
|
|
|
|
|
type ISmsServe interface {
|
|
|
Send() error //调用远端接口发送短信
|
|
|
TextContent() (string, error) //构建短信文本内容
|
|
|
ValidReturn() (string, error) //返回调用远端接口后的返回内容,可用于记日志
|
|
|
TextContent(v ...interface{}) (string, error) //构建短信文本内容
|
|
|
ValidReturn() (string, error) //检查调用远端接口后的返回内容,可用于记日志等操作
|
|
|
}
|
|
|
|
|
|
type SmsServe struct {
|
|
|
ToPhones []string
|
|
|
Content string
|
|
|
ServeReturn interface{}
|
|
|
ToPhone string
|
|
|
UseTpl string
|
|
|
smsContent string //非导出
|
|
|
serveReturn *YunPainReturn //非导出
|
|
|
}
|
|
|
|
|
|
type YunPainReturn 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 NewSmsServe(toPhone string, useTpl string) *SmsServe {
|
|
|
return &SmsServe{
|
|
|
ToPhone: toPhone,
|
|
|
UseTpl: useTpl,
|
|
|
serveReturn: nil,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func (s *SmsServe) Send() error {
|
|
|
if len(s.ToPhone) == 0 {
|
|
|
return nil
|
|
|
}
|
|
|
//TODO
|
|
|
//发送短信
|
|
|
//获取并设置返回值
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func (s *SmsServe) TextContent() (string, error) {
|
|
|
return "", nil
|
|
|
func (s *SmsServe) TextContent(v ...interface{}) (string, error) {
|
|
|
tpl, err := getSmsTpl(s.UseTpl)
|
|
|
if err != nil {
|
|
|
return "", err
|
|
|
}
|
|
|
t, err := tpl.ParseTpl(v)
|
|
|
if err != nil {
|
|
|
return "", err
|
|
|
}
|
|
|
s.smsContent = t
|
|
|
return t, nil
|
|
|
}
|
|
|
|
|
|
func (s *SmsServe) ValidReturn() (string, error) {
|
|
|
return "", nil
|
|
|
if s.serveReturn == nil {
|
|
|
return "", errors.New("serveReturn is nil")
|
|
|
}
|
|
|
str, _ := json.Marshal(s.serveReturn)
|
|
|
if s.serveReturn.Code != 0 || s.serveReturn.Mobile != s.ToPhone {
|
|
|
return string(str), errors.New("send sms fail")
|
|
|
}
|
|
|
return string(str), nil
|
|
|
} |
...
|
...
|
|