...
|
...
|
@@ -3,22 +3,34 @@ package sms |
|
|
import (
|
|
|
"encoding/json"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"regexp"
|
|
|
|
|
|
"github.com/astaxie/beego"
|
|
|
"github.com/astaxie/beego/httplib"
|
|
|
)
|
|
|
|
|
|
//ISmsServe 短信服务接口协议
|
|
|
type ISmsServe interface {
|
|
|
Send() error //调用远端接口发送短信
|
|
|
TextContent(v ...interface{}) (string, error) //构建短信文本内容
|
|
|
ValidReturn() (string, error) //检查调用远端接口后的返回内容,可用于记日志等操作
|
|
|
Name() string
|
|
|
}
|
|
|
|
|
|
//SmsServe 生产线上使用的短信服务
|
|
|
type SmsServe struct {
|
|
|
ToPhone string
|
|
|
UseTpl string
|
|
|
smsContent string //非导出
|
|
|
serveReturn *YunPainReturn //非导出
|
|
|
ToPhone string //对方的电话号码
|
|
|
UseTpl string //使用的短信模板
|
|
|
smsContent string //非导出,最终发送的短信内容
|
|
|
serveReturn *YunPianReturn //非导出,调用远端接口后的响应内容
|
|
|
}
|
|
|
|
|
|
type YunPainReturn struct {
|
|
|
var (
|
|
|
_ ISmsServe = &SmsServe{}
|
|
|
)
|
|
|
|
|
|
type YunPianReturn struct {
|
|
|
Code int `json:"code"` //0代表发送成功,其他code代表出错,详细见"返回值说明"页面
|
|
|
Msg string `json:"msg"` //例如""发送成功"",或者相应错误信息
|
|
|
Count int `json:"count"` //发送成功短信的计费条数(计费条数:70个字一条,超出70个字时按每67字一条计费)
|
...
|
...
|
@@ -36,17 +48,34 @@ func NewSmsServe(toPhone string, useTpl string) *SmsServe { |
|
|
}
|
|
|
|
|
|
//Send 短信服务发送动作
|
|
|
//实现接口 ISmsServe
|
|
|
func (s *SmsServe) Send() error {
|
|
|
if len(s.ToPhone) == 0 {
|
|
|
return nil
|
|
|
}
|
|
|
//TODO
|
|
|
//校验号码合法性/^1[3456789][0-9]{9}$/
|
|
|
ok, err := regexp.MatchString(`^1[3456789][0-9]{9}$`, s.ToPhone)
|
|
|
if !ok {
|
|
|
return fmt.Errorf("ToPhone err:%w", err)
|
|
|
}
|
|
|
//发送短信
|
|
|
var (
|
|
|
resp *YunPianReturn
|
|
|
)
|
|
|
post := httplib.Post(beego.AppConfig.String("yunpian_sms_sdk_url"))
|
|
|
post.Param("apikey", beego.AppConfig.String("yunpian_app_key"))
|
|
|
post.Param("mobile", s.ToPhone)
|
|
|
post.Param("text", s.smsContent)
|
|
|
if err := post.ToJSON(&resp); err != nil {
|
|
|
return fmt.Errorf("parse yunpian response err:%w ", err)
|
|
|
}
|
|
|
//获取并设置返回值
|
|
|
s.serveReturn = resp
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
// TextContent 短信正文内容设置
|
|
|
//实现接口 ISmsServe
|
|
|
func (s *SmsServe) TextContent(v ...interface{}) (string, error) {
|
|
|
tpl, err := getSmsTpl(s.UseTpl)
|
|
|
if err != nil {
|
...
|
...
|
@@ -61,6 +90,7 @@ func (s *SmsServe) TextContent(v ...interface{}) (string, error) { |
|
|
}
|
|
|
|
|
|
//ValidReturn 校验调用远端接口后返回的响应内容
|
|
|
//实现接口 ISmsServe
|
|
|
func (s *SmsServe) ValidReturn() (string, error) {
|
|
|
if s.serveReturn == nil {
|
|
|
return "", errors.New("serveReturn is nil")
|
...
|
...
|
@@ -71,3 +101,69 @@ func (s *SmsServe) ValidReturn() (string, error) { |
|
|
}
|
|
|
return string(str), nil
|
|
|
}
|
|
|
|
|
|
func (s *SmsServe) Name() string {
|
|
|
return "SmsServe"
|
|
|
}
|
|
|
|
|
|
//DevSmsServe 开发调试使用的短信服务
|
|
|
type DevSmsServe struct {
|
|
|
ToPhone string //对方的电话号码
|
|
|
UseTpl string //使用的短信模板
|
|
|
smsContent string //非导出,最终发送的短信内容
|
|
|
serveReturn string //非导出,调用远端接口后的响应内容
|
|
|
}
|
|
|
|
|
|
var (
|
|
|
_ ISmsServe = &DevSmsServe{}
|
|
|
)
|
|
|
|
|
|
func NewDevSmsServe(toPhone string, useTpl string) *DevSmsServe {
|
|
|
return &DevSmsServe{
|
|
|
ToPhone: toPhone,
|
|
|
UseTpl: useTpl,
|
|
|
serveReturn: "",
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//Send 实现接口 ISmsServe
|
|
|
func (s *DevSmsServe) Send() error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
//TextContent 实现接口 ISmsServe
|
|
|
func (s *DevSmsServe) 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
|
|
|
}
|
|
|
|
|
|
//ValidReturn 实现接口 ISmsServe
|
|
|
func (s *DevSmsServe) ValidReturn() (string, error) {
|
|
|
return "ok", nil
|
|
|
}
|
|
|
|
|
|
func (s *DevSmsServe) Name() string {
|
|
|
return "DevSmsServe"
|
|
|
}
|
|
|
|
|
|
//SendSms 发送短信调用出口 TODO
|
|
|
func SendSms(phone string, code string, param ...interface{}) error {
|
|
|
var serve ISmsServe
|
|
|
serve = NewSmsServe(phone, code)
|
|
|
// serve = NewDevSmsServe(phone, code)
|
|
|
|
|
|
_, err := serve.TextContent(param)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
return serve.Send()
|
|
|
} |
...
|
...
|
|