/
message.go
146 lines (121 loc) · 2.86 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package service
import (
"errors"
"github.com/astaxie/beego"
"github.com/astaxie/beego/utils"
"regexp"
"strings"
"tools/server/app/validate"
)
type MessageRes struct {
Status bool
Info string
}
func SendMsg(data *MessageData) (res *MessageRes, err error) {
mess := &validate.ValidateMessage{
TplId: data.TplId,
ToUser: data.ToUser,
ExtraParams: data.ExtraParams,
}
//参数验证
err = mess.ValidMessage()
if err != nil {
return nil, err
}
//查询模板消息
notifyDetail, err := NotifyDetailById(mess.TplId)
if err != nil {
return nil, err
}
//模板消息状态
notifyStatus := notifyDetail.Status
if notifyStatus != 1 {
return nil, errors.New("模板id状态异常")
}
//模板消息类型及内容
notifyType := notifyDetail.NotifyType
notifyContent := notifyDetail.TplData
switch notifyType {
case 1:
err = sendEmail(data, notifyContent)
if err != nil {
return nil, err
}
default:
return nil, errors.New("模板消息类型不存在")
}
res = &MessageRes{
Status: true,
Info: "发送成功",
}
return res, nil
}
/*
发送邮件
*/
func sendEmail(data *MessageData, notifyContent string) error {
_, isExist := data.ExtraParams["title"]
if !isExist {
return errors.New("邮件标题不能为空")
}
//检查替换content
content, err := checkExtraContent(notifyContent, data.ExtraParams)
if err != nil {
return err
}
username := beego.AppConfig.String("mail.username")
password := beego.AppConfig.String("mail.password")
host := beego.AppConfig.String("mail.host")
port := beego.AppConfig.String("mail.port")
mail := &validate.ValidateEmail{
Title: data.ExtraParams["title"],
ToUser: data.ToUser,
Content: content,
FromUser: username,
PassWord: password,
Host: host,
Port: port,
}
//参数验证
err = mail.ValidEmail()
if err != nil {
return err
}
config := `{"username":` + mail.FromUser + `,"password":` + mail.PassWord + `,"host":` + mail.Host + `,"port":` + mail.Port + `}`
email := utils.NewEMail(config)
email.To = mail.ToUser
email.From = mail.FromUser
email.Subject = mail.Title
email.Text = mail.Content
err = email.Send()
if err != nil {
return err
}
return nil
}
//校验并替换消息内容
func checkExtraContent(content string, extra map[string]string) (string, error) {
reg := regexp.MustCompile("{{.*?\\.DATA}}")
arrContent := reg.FindAllString(content, -1)
if len(arrContent) > 0 {
//校验内容中需替换字段是否为空
f := func(c rune) bool {
if c == '{' || c == '.' {
return true
} else {
return false
}
}
for _, v := range arrContent {
result := strings.FieldsFunc(v, f)
ekey := result[0]
_, isExist := extra[ekey]
if !isExist {
return "", errors.New(ekey + "不能为空")
}
//替换字符串
content = strings.Replace(content, v, extra[ekey], -1)
}
}
return content, nil
}