ferry/pkg/notify/send.go

90 lines
1.9 KiB
Go
Raw Normal View History

2020-07-25 14:20:15 +08:00
package notify
import (
2020-07-25 16:11:47 +08:00
"bytes"
2020-07-25 14:20:15 +08:00
"ferry/models/system"
2020-08-15 23:34:37 +08:00
"ferry/pkg/logger"
2020-07-25 14:20:15 +08:00
"ferry/pkg/notify/email"
2020-07-25 16:11:47 +08:00
"text/template"
2020-08-14 21:22:11 +08:00
"github.com/spf13/viper"
2020-07-25 14:20:15 +08:00
)
/*
@Author : lanyulei
@同时发送多种通知方式
*/
2020-07-25 16:11:47 +08:00
type BodyData struct {
SendTo interface{} // 接受人
EmailCcTo []string // 抄送人邮箱列表
2020-07-25 16:11:47 +08:00
Subject string // 标题
Classify []int // 通知类型
Id int // 工单ID
Title string // 工单标题
Creator string // 工单创建人
Priority int // 工单优先级
PriorityValue string // 工单优先级
CreatedAt string // 工单创建时间
Content string // 通知的内容
2020-07-25 21:51:30 +08:00
Description string // 表格上面的描述信息
ProcessId int // 流程ID
2020-08-14 21:22:11 +08:00
Domain string // 域名地址
2020-07-25 16:11:47 +08:00
}
func (b *BodyData) ParsingTemplate() (err error) {
// 读取模版数据
var (
buf bytes.Buffer
)
2020-07-26 22:58:53 +08:00
tmpl, err := template.ParseFiles("./static/template/email.html")
2020-07-25 16:11:47 +08:00
if err != nil {
return
}
2020-08-21 18:35:16 +08:00
b.Domain = viper.GetString("settings.domain.url")
2020-07-25 16:11:47 +08:00
err = tmpl.Execute(&buf, b)
if err != nil {
return
}
b.Content = buf.String()
return
}
2020-07-26 21:27:08 +08:00
func (b *BodyData) SendNotify() (err error) {
2020-07-25 14:20:15 +08:00
var (
emailList []string
)
2020-07-25 16:11:47 +08:00
switch b.Priority {
case 1:
b.PriorityValue = "正常"
case 2:
b.PriorityValue = "紧急"
case 3:
b.PriorityValue = "非常紧急"
}
for _, c := range b.Classify {
2020-07-25 14:20:15 +08:00
switch c {
case 1: // 邮件
2020-07-25 21:51:30 +08:00
users := b.SendTo.(map[string]interface{})["userList"].([]system.SysUser)
if len(users) > 0 {
for _, user := range users {
emailList = append(emailList, user.Email)
}
err = b.ParsingTemplate()
if err != nil {
2020-08-15 23:34:37 +08:00
logger.Errorf("模版内容解析失败,%v", err.Error())
2020-07-25 21:51:30 +08:00
return
}
go email.SendMail(emailList, b.EmailCcTo, b.Subject, b.Content)
2020-07-25 14:20:15 +08:00
}
}
}
2020-07-26 21:27:08 +08:00
return
2020-07-25 14:20:15 +08:00
}