正在显示
3 个修改的文件
包含
87 行增加
和
16 行删除
1 | package sms | 1 | package sms |
2 | 2 | ||
3 | +import ( | ||
4 | + "encoding/json" | ||
5 | + "errors" | ||
6 | +) | ||
7 | + | ||
3 | type ISmsServe interface { | 8 | type ISmsServe interface { |
4 | Send() error //调用远端接口发送短信 | 9 | Send() error //调用远端接口发送短信 |
5 | - TextContent() (string, error) //构建短信文本内容 | ||
6 | - ValidReturn() (string, error) //返回调用远端接口后的返回内容,可用于记日志 | 10 | + TextContent(v ...interface{}) (string, error) //构建短信文本内容 |
11 | + ValidReturn() (string, error) //检查调用远端接口后的返回内容,可用于记日志等操作 | ||
7 | } | 12 | } |
8 | 13 | ||
9 | type SmsServe struct { | 14 | type SmsServe struct { |
10 | - ToPhones []string | ||
11 | - Content string | ||
12 | - ServeReturn interface{} | 15 | + ToPhone string |
16 | + UseTpl string | ||
17 | + smsContent string //非导出 | ||
18 | + serveReturn *YunPainReturn //非导出 | ||
19 | +} | ||
20 | + | ||
21 | +type YunPainReturn struct { | ||
22 | + Code int `json:"code"` //0代表发送成功,其他code代表出错,详细见"返回值说明"页面 | ||
23 | + Msg string `json:"msg"` //例如""发送成功"",或者相应错误信息 | ||
24 | + Count int `json:"count"` //发送成功短信的计费条数(计费条数:70个字一条,超出70个字时按每67字一条计费) | ||
25 | + Mobile string `json:"string"` //发送手机号 | ||
26 | + Fee float64 `json:"fee"` //扣费金额,单位:元,类型:双精度浮点型/double | ||
27 | + Sid int64 `json:"sid"` //短信id,64位整型 | ||
28 | +} | ||
29 | + | ||
30 | +func NewSmsServe(toPhone string, useTpl string) *SmsServe { | ||
31 | + return &SmsServe{ | ||
32 | + ToPhone: toPhone, | ||
33 | + UseTpl: useTpl, | ||
34 | + serveReturn: nil, | ||
35 | + } | ||
13 | } | 36 | } |
14 | 37 | ||
15 | func (s *SmsServe) Send() error { | 38 | func (s *SmsServe) Send() error { |
39 | + if len(s.ToPhone) == 0 { | ||
40 | + return nil | ||
41 | + } | ||
42 | + //TODO | ||
43 | + //发送短信 | ||
44 | + //获取并设置返回值 | ||
16 | return nil | 45 | return nil |
17 | } | 46 | } |
18 | 47 | ||
19 | -func (s *SmsServe) TextContent() (string, error) { | ||
20 | - return "", nil | 48 | +func (s *SmsServe) TextContent(v ...interface{}) (string, error) { |
49 | + tpl, err := getSmsTpl(s.UseTpl) | ||
50 | + if err != nil { | ||
51 | + return "", err | ||
52 | + } | ||
53 | + t, err := tpl.ParseTpl(v) | ||
54 | + if err != nil { | ||
55 | + return "", err | ||
56 | + } | ||
57 | + s.smsContent = t | ||
58 | + return t, nil | ||
21 | } | 59 | } |
22 | 60 | ||
23 | func (s *SmsServe) ValidReturn() (string, error) { | 61 | func (s *SmsServe) ValidReturn() (string, error) { |
24 | - return "", nil | 62 | + if s.serveReturn == nil { |
63 | + return "", errors.New("serveReturn is nil") | ||
64 | + } | ||
65 | + str, _ := json.Marshal(s.serveReturn) | ||
66 | + if s.serveReturn.Code != 0 || s.serveReturn.Mobile != s.ToPhone { | ||
67 | + return string(str), errors.New("send sms fail") | ||
68 | + } | ||
69 | + return string(str), nil | ||
25 | } | 70 | } |
@@ -2,19 +2,46 @@ package sms | @@ -2,19 +2,46 @@ package sms | ||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "errors" | 4 | "errors" |
5 | + "fmt" | ||
5 | ) | 6 | ) |
6 | 7 | ||
7 | -var yunPianSmsTpl = map[string]string{ | ||
8 | - "10001": "【云片网】您的验证码是%s", | 8 | +var yunPianSmsTpl = map[string]SmsTpl{ |
9 | + "10001": SmsTpl{"【云片网】您的验证码是%s", 1}, | ||
9 | } | 10 | } |
10 | 11 | ||
11 | const ( | 12 | const ( |
12 | TplNotExit string = "tpl not exit" | 13 | TplNotExit string = "tpl not exit" |
14 | + TplFormateErr string = "tpl formate error" | ||
13 | ) | 15 | ) |
14 | 16 | ||
15 | -func getSmsTpl(code string) (string, error) { | 17 | +type ISmsTpl interface { |
18 | + ParseTpl(v ...interface{}) (string, error) | ||
19 | + String() string | ||
20 | +} | ||
21 | + | ||
22 | +type SmsTpl struct { | ||
23 | + Formate string | ||
24 | + ParamNum int | ||
25 | +} | ||
26 | + | ||
27 | +var ( | ||
28 | + _ ISmsTpl = SmsTpl{} | ||
29 | +) | ||
30 | + | ||
31 | +func (t SmsTpl) ParseTpl(v ...interface{}) (string, error) { | ||
32 | + if len(v) < t.ParamNum { | ||
33 | + return "", errors.New(TplFormateErr) | ||
34 | + } | ||
35 | + return fmt.Sprintf(t.String(), v), nil | ||
36 | +} | ||
37 | + | ||
38 | +func (t SmsTpl) String() string { | ||
39 | + return t.Formate | ||
40 | +} | ||
41 | + | ||
42 | +func getSmsTpl(code string) (ISmsTpl, error) { | ||
16 | if tpl, ok := yunPianSmsTpl[code]; ok { | 43 | if tpl, ok := yunPianSmsTpl[code]; ok { |
17 | return tpl, nil | 44 | return tpl, nil |
18 | } | 45 | } |
19 | - return "", errors.New(TplNotExit) | 46 | + return nil, errors.New(TplNotExit) |
20 | } | 47 | } |
@@ -3,7 +3,6 @@ package sms | @@ -3,7 +3,6 @@ package sms | ||
3 | import ( | 3 | import ( |
4 | "ability/protocol" | 4 | "ability/protocol" |
5 | "errors" | 5 | "errors" |
6 | - "fmt" | ||
7 | 6 | ||
8 | "github.com/astaxie/beego" | 7 | "github.com/astaxie/beego" |
9 | "github.com/astaxie/beego/httplib" | 8 | "github.com/astaxie/beego/httplib" |
@@ -47,9 +46,9 @@ func (s *YunPianSmsService) Send(request *protocol.SmsCodeRequest) (err error) { | @@ -47,9 +46,9 @@ func (s *YunPianSmsService) Send(request *protocol.SmsCodeRequest) (err error) { | ||
47 | } | 46 | } |
48 | 47 | ||
49 | func (s *YunPianSmsService) GetSmsTpl(code string, content ...interface{}) (string, error) { | 48 | func (s *YunPianSmsService) GetSmsTpl(code string, content ...interface{}) (string, error) { |
50 | - if data, ok := yunPianSmsTpl[code]; !ok { | ||
51 | - return fmt.Sprintf(data, content...), nil | ||
52 | - } | 49 | + // if data, ok := yunPianSmsTpl[code]; !ok { |
50 | + // return fmt.Sprintf(data, content...), nil | ||
51 | + // } | ||
53 | 52 | ||
54 | return "", errors.New("GetSmsTpl err,code=" + code) | 53 | return "", errors.New("GetSmsTpl err,code=" + code) |
55 | } | 54 | } |
-
请 注册 或 登录 后发表评论