浏览代码

新增消息推送渠道

longyu 2 年之前
父节点
当前提交
052de37e35
共有 7 个文件被更改,包括 237 次插入6 次删除
  1. 60 0
      controllers/alaram_msg.go
  2. 1 1
      controllers/sms.go
  3. 0 0
      index.html
  4. 8 0
      models/alarm_msg.go
  5. 5 0
      routers/router.go
  6. 133 0
      services/alarm_msg.go
  7. 30 5
      utils/email.go

+ 60 - 0
controllers/alaram_msg.go

@@ -0,0 +1,60 @@
+package controllers
+
+import (
+	"encoding/json"
+	"hongze/hongze_robot/cache"
+	"hongze/hongze_robot/models"
+	"hongze/hongze_robot/services"
+	"hongze/hongze_robot/utils"
+)
+
+//监控信息管理
+type AlarmMsgController struct {
+	BaseCommonController
+}
+
+// @Title 监控信息发送接口
+// @Description 监控信息发送接口
+// @Param	request	body company.CompanyApplyApproveReq true "type json string"
+// @Success Ret=200 转发成功
+// @router /send [post]
+func (this *AlarmMsgController) SmsSend() {
+	br := new(models.BaseResponse).Init()
+	var err error
+	defer func() {
+		if err != nil {
+			utils.FileLog.Info("send msg err: %s", err.Error())
+		}
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	reqBody := this.Ctx.Input.RequestBody
+	utils.FileLog.Info("%s", string(reqBody))
+	var req *models.AlarmMsgReq
+	err = json.Unmarshal(reqBody, &req)
+	if err != nil {
+		br.Msg = "参数解析异常!"
+		br.ErrMsg = "参数解析失败,Err:" + err.Error()
+		return
+	}
+	if req.MsgBody == "" {
+		br.Ret = 200
+		br.Success = true
+		br.Msg = "发送失败,消息内容为空"
+		return
+	}
+	//钉钉
+	{
+		go services.SendAlarmMsgByDingTalk(req.ProjectName, req.RunMode, req.MsgBody, req.Level)
+	}
+	//邮箱
+	{
+		configItem, _ := cache.GetSmsConfigByCache("email")
+		if configItem != nil {
+			go services.SendAlarmMsgByEmail(req.ProjectName, req.RunMode, req.MsgBody, configItem.ConfigValue, req.Level)
+		}
+	}
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "发送成功"
+}

+ 1 - 1
controllers/sms.go

@@ -122,4 +122,4 @@ func (this *SmsController) SmsSend() {
 //	services.SendSms()
 //	fmt.Println("end")
 //	return
-//}
+//}

+ 0 - 0
index.html


+ 8 - 0
models/alarm_msg.go

@@ -0,0 +1,8 @@
+package models
+
+type AlarmMsgReq struct {
+	ProjectName string `description:"项目名称"`
+	RunMode     string `description:"运行模式"`
+	MsgBody     string `description:"消息内容"`
+	Level       int    `description:"消息级别,1:提示消息,2:警告消息,3:严重错误信息,默认为1 提示消息"`
+}

+ 5 - 0
routers/router.go

@@ -19,6 +19,11 @@ func init() {
 				&controllers.SmsController{},
 			),
 		),
+		web.NSNamespace("/alarm",
+			web.NSInclude(
+				&controllers.SmsController{},
+			),
+		),
 	)
 	web.AddNamespace(ns)
 }

+ 133 - 0
services/alarm_msg.go

@@ -0,0 +1,133 @@
+package services
+
+import (
+	"github.com/blinkbean/dingtalk"
+	"gopkg.in/gomail.v2"
+	"hongze/hongze_robot/utils"
+	"strings"
+	"time"
+)
+
+//projectName-项目名称
+//runMode-运行模式
+//msgBody-消息内容
+//level:消息基本,1:提示消息,2:警告消息,3:严重错误信息,默认为1 提示消息
+
+//钉钉机器人
+func SendAlarmMsgByDingTalk(projectName, runMode, msgBody string, level int) {
+	if level <= 0 {
+		level = 1
+	}
+	time.Sleep(time.Second)
+	var err error
+	defer func() {
+		if err != nil {
+			go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "SendSms Err:"+err.Error(), utils.EmailSendToUsers)
+		}
+	}()
+
+	var dingToken = []string{}
+
+	dingToken = []string{
+		"c0ff5de65a46c8a340c9846dcae6f182782247dbd910f4fcfa0758e12c28a030",
+		"84780b28c4cd6f2ca3edd650e068cd56a4ab99fc78003bdd0983c35b573b3e75",
+		"dbdb7f0650e920740c952bfc29d9217d8dbbfc3d949dc8bd25ad8ed744ca1b40",
+	}
+
+	smsDateTime := time.Now().Format(utils.FormatDateTime)
+	var fontColor string
+	if level == 1 {
+		fontColor = `#008000`
+	} else if level == 2 {
+		fontColor = `#FF8C00`
+	} else {
+		fontColor = `#FF0000`
+	}
+	title := projectName + "【" + runMode + "】"
+	if runMode == "" {
+		title = projectName
+	}
+	msg := []string{
+		"### " + title,
+		"---",
+		"- <font color=" + fontColor + ">" + msgBody + "</font>",
+		"- " + smsDateTime,
+	}
+	cli := dingtalk.InitDingTalk(dingToken, "报警")
+	//cli.SendTextMessage("content", dingtalk.WithAtAll())
+	err = cli.SendMarkDownMessageBySlice("华瑞信息", msg)
+	if err != nil {
+		return
+	}
+}
+
+var (
+	emails = []struct {
+		address  string
+		authCode string
+	}{
+		{
+			address:  "qunao007@qq.com",
+			authCode: "cyhjgffhaobteihd",
+		},
+		{
+			address:  "2370889154@qq.com",
+			authCode: "pfujkfwyqaprdjgb",
+		},
+	}
+)
+
+//邮件
+func SendAlarmMsgByEmail(projectName, runMode, msgBody, touser string, level int) bool {
+	var arr []string
+	sub := strings.Index(touser, ";")
+	if sub >= 0 {
+		spArr := strings.Split(touser, ";")
+		for _, v := range spArr {
+			arr = append(arr, v)
+		}
+	} else {
+		arr = append(arr, touser)
+	}
+	rand := utils.GetRandInt(1, len(emails)+1)
+	emailObj := emails[rand-1]
+
+	emailAddress := emailObj.address
+	emailAuthCode := emailObj.authCode
+
+	var fontColor string
+	if level == 1 {
+		fontColor = `#008000`
+	} else if level == 2 {
+		fontColor = `#FF8C00`
+	} else {
+		fontColor = `#FF0000`
+	}
+
+	msgBody = "<div>" + "</div>"
+
+	body := `<!DOCTYPE html>
+             <html lang="en">
+              <head>
+				<meta charset="UTF-8">
+				<title>Title</title>
+			  </head>
+            <body>
+               <div style="color: ` + fontColor + `">
+               ` + msgBody + `
+               </div>
+            </body>
+            </html>`
+
+	title := projectName + "【" + runMode + "】"
+	m := gomail.NewMessage()
+	m.SetHeader("From", emailAddress)
+	m.SetHeader("To", arr...)
+	m.SetHeader("Subject", title+" "+utils.GetRandStringNoSpecialChar(10))
+	m.SetBody("text/html", body)
+	d := gomail.NewDialer("smtp.qq.com", 587, emailAddress, emailAuthCode)
+	if err := d.DialAndSend(m); err != nil {
+		return false
+	}
+	return true
+}

+ 30 - 5
utils/email.go

@@ -1,12 +1,29 @@
 package utils
 
 import (
+	"fmt"
 	"gopkg.in/gomail.v2"
 	"strings"
 )
 
+var (
+	emails = []struct {
+		address  string
+		authCode string
+	}{
+		{
+			address:  "qunao007@qq.com",
+			authCode: "cyhjgffhaobteihd",
+		},
+		{
+			address:  "2370889154@qq.com",
+			authCode: "pfujkfwyqaprdjgb",
+		},
+	}
+)
+
 //发送邮件
-func SendEmail(title, content string, touser string)bool {
+func SendEmail(title, content string, touser string) bool {
 	var arr []string
 	sub := strings.Index(touser, ";")
 	if sub >= 0 {
@@ -14,17 +31,25 @@ func SendEmail(title, content string, touser string)bool {
 		for _, v := range spArr {
 			arr = append(arr, v)
 		}
-	}else{
+	} else {
 		arr = append(arr, touser)
 	}
+	rand := GetRandInt(1, len(emails)+1)
+	emailObj := emails[rand-1]
+	fmt.Println(emailObj.address, emailObj.authCode)
+
+	emailAddress := emailObj.address
+	emailAuthCode := emailObj.authCode
+
 	m := gomail.NewMessage()
-	m.SetHeader("From", "317699326@qq.com ")
+	m.SetHeader("From", emailAddress)
 	m.SetHeader("To", arr...)
 	m.SetHeader("Subject", title+" "+GetRandString(16))
 	m.SetBody("text/html", content)
-	d := gomail.NewDialer("smtp.qq.com", 587, "317699326@qq.com", "oqdypwfcvruwcbea")
+	d := gomail.NewDialer("smtp.qq.com", 587, emailAddress, emailAuthCode)
 	if err := d.DialAndSend(m); err != nil {
+		fmt.Println("DialAndSend Err:" + err.Error())
 		return false
 	}
 	return true
-}
+}