diff --git a/apis/process/workOrder.go b/apis/process/workOrder.go index f4107df..dc64fb3 100644 --- a/apis/process/workOrder.go +++ b/apis/process/workOrder.go @@ -12,6 +12,7 @@ import ( "ferry/tools/app" "fmt" "strconv" + "time" "github.com/gin-gonic/gin" ) @@ -206,9 +207,22 @@ func CreateWorkOrder(c *gin.Context) { return } - go notify.SendNotify(noticeList, map[string]interface{}{ - "userList": sendToUserList, - }, "您有一条待办工单,请及时处理。", "测试") + // 发送通知 + go func() { + bodyData := notify.BodyData{ + SendTo: map[string]interface{}{ + "userList": sendToUserList, + }, + Subject: "您有一条待办工单,请及时处理。", + Classify: noticeList, + Id: workOrderValue.Id, + Title: workOrderValue.Title, + Creator: userInfo.NickName, + Priority: workOrderValue.Priority, + CreatedAt: time.Now().Format("2006-01-02 15:04:05"), + } + bodyData.SendNotify() + }() } // 执行任务 diff --git a/pkg/notify/send.go b/pkg/notify/send.go index 5ae200f..774b5a8 100644 --- a/pkg/notify/send.go +++ b/pkg/notify/send.go @@ -1,8 +1,13 @@ package notify import ( + "bytes" "ferry/models/system" "ferry/pkg/notify/email" + "os" + "text/template" + + log "github.com/sirupsen/logrus" ) /* @@ -10,17 +15,68 @@ import ( @同时发送多种通知方式 */ -func SendNotify(classify []int, sendTo interface{}, subject, body string) { +type BodyData struct { + SendTo interface{} // 接受人 + Subject string // 标题 + Classify []int // 通知类型 + Id int // 工单ID + Title string // 工单标题 + Creator string // 工单创建人 + Priority int // 工单优先级 + PriorityValue string // 工单优先级 + CreatedAt string // 工单创建时间 + Content string // 通知的内容 +} + +func (b *BodyData) ParsingTemplate() (err error) { + // 读取模版数据 + var ( + buf bytes.Buffer + ) + + log.Info(os.Getwd()) + tmpl, err := template.ParseFiles("./pkg/notify/template/email.html") + if err != nil { + return + } + + err = tmpl.Execute(&buf, b) + if err != nil { + return + } + + b.Content = buf.String() + + return +} + +func (b *BodyData) SendNotify() { var ( emailList []string + err error ) - for _, c := range classify { + + switch b.Priority { + case 1: + b.PriorityValue = "正常" + case 2: + b.PriorityValue = "紧急" + case 3: + b.PriorityValue = "非常紧急" + } + + for _, c := range b.Classify { switch c { case 1: // 邮件 - for _, user := range sendTo.(map[string]interface{})["userList"].([]system.SysUser) { + for _, user := range b.SendTo.(map[string]interface{})["userList"].([]system.SysUser) { emailList = append(emailList, user.Email) } - go email.SendMail(emailList, subject, body) + err = b.ParsingTemplate() + if err != nil { + log.Errorf("模版内容解析失败,%v", err.Error()) + return + } + go email.SendMail(emailList, b.Subject, b.Content) } } } diff --git a/pkg/notify/template/email.html b/pkg/notify/template/email.html new file mode 100644 index 0000000..20dbf61 --- /dev/null +++ b/pkg/notify/template/email.html @@ -0,0 +1,44 @@ + + + + + ferry + + +
+ 您有一条待办工单请及时处理,以下为工单详情: +
+
+ + + + + + + + + + + + + + + + + +
标题:{{ .Title }}
申请人:{{ .Creator }}
优先级:{{ .PriorityValue }}
申请时间:{{ .CreatedAt }}
+ +
+ 点击此处跳转工单详情 + + + \ No newline at end of file