|
@@ -5,7 +5,13 @@ import ( |
|
@@ -5,7 +5,13 @@ import ( |
5
|
"fmt"
|
5
|
"fmt"
|
6
|
)
|
6
|
)
|
7
|
|
7
|
|
8
|
-var yunPianSmsTpl = map[string]SmsTpl{
|
8
|
+type ISmsTpl interface {
|
|
|
9
|
+ ParseTpl(v ...interface{}) (string, error)
|
|
|
10
|
+ String() string
|
|
|
11
|
+}
|
|
|
12
|
+
|
|
|
13
|
+//注册短信模板
|
|
|
14
|
+var smsTpl = map[string]ISmsTpl{
|
9
|
"10001": SmsTpl{"【云片网】您的验证码是%s", 1},
|
15
|
"10001": SmsTpl{"【云片网】您的验证码是%s", 1},
|
10
|
}
|
16
|
}
|
11
|
|
17
|
|
|
@@ -14,11 +20,6 @@ const ( |
|
@@ -14,11 +20,6 @@ const ( |
14
|
TplFormateErr string = "tpl formate error"
|
20
|
TplFormateErr string = "tpl formate error"
|
15
|
)
|
21
|
)
|
16
|
|
22
|
|
17
|
-type ISmsTpl interface {
|
|
|
18
|
- ParseTpl(v ...interface{}) (string, error)
|
|
|
19
|
- String() string
|
|
|
20
|
-}
|
|
|
21
|
-
|
|
|
22
|
type SmsTpl struct {
|
23
|
type SmsTpl struct {
|
23
|
Formate string
|
24
|
Formate string
|
24
|
ParamNum int
|
25
|
ParamNum int
|
|
@@ -28,6 +29,7 @@ var ( |
|
@@ -28,6 +29,7 @@ var ( |
28
|
_ ISmsTpl = SmsTpl{}
|
29
|
_ ISmsTpl = SmsTpl{}
|
29
|
)
|
30
|
)
|
30
|
|
31
|
|
|
|
32
|
+//ParseTpl 根据传参填充模板内容
|
31
|
func (t SmsTpl) ParseTpl(v ...interface{}) (string, error) {
|
33
|
func (t SmsTpl) ParseTpl(v ...interface{}) (string, error) {
|
32
|
if len(v) < t.ParamNum {
|
34
|
if len(v) < t.ParamNum {
|
33
|
return "", errors.New(TplFormateErr)
|
35
|
return "", errors.New(TplFormateErr)
|
|
@@ -35,12 +37,15 @@ func (t SmsTpl) ParseTpl(v ...interface{}) (string, error) { |
|
@@ -35,12 +37,15 @@ func (t SmsTpl) ParseTpl(v ...interface{}) (string, error) { |
35
|
return fmt.Sprintf(t.String(), v), nil
|
37
|
return fmt.Sprintf(t.String(), v), nil
|
36
|
}
|
38
|
}
|
37
|
|
39
|
|
|
|
40
|
+//String 返回模板字符串
|
38
|
func (t SmsTpl) String() string {
|
41
|
func (t SmsTpl) String() string {
|
39
|
return t.Formate
|
42
|
return t.Formate
|
40
|
}
|
43
|
}
|
41
|
|
44
|
|
|
|
45
|
+//getSmsTpl 获取样板
|
|
|
46
|
+//TODO 待优化
|
42
|
func getSmsTpl(code string) (ISmsTpl, error) {
|
47
|
func getSmsTpl(code string) (ISmsTpl, error) {
|
43
|
- if tpl, ok := yunPianSmsTpl[code]; ok {
|
48
|
+ if tpl, ok := smsTpl[code]; ok {
|
44
|
return tpl, nil
|
49
|
return tpl, nil
|
45
|
}
|
50
|
}
|
46
|
return nil, errors.New(TplNotExit)
|
51
|
return nil, errors.New(TplNotExit)
|