Explorar o código

[REV] 新增短信使用切换,sqlrecord为true时,使用应用管理中的手机号进行短信发送

pujielan hai 1 mes
pai
achega
1034c2dd81
Modificáronse 5 ficheiros con 36 adicións e 18 borrados
  1. 1 1
      config/extend.go
  2. 2 2
      config/settings.yml
  3. 4 0
      handler/events.go
  4. 27 14
      handler/handler.go
  5. 2 1
      handler/sms.go

+ 1 - 1
config/extend.go

@@ -72,5 +72,5 @@ type SmsConfig struct {
 	Url       string `json:"url"`
 	AppsGroup string //1:17233413341,18723344112;2:17233413341,18723344112;3:17233413341,18723344112
 	SmsTpl    string
-	// SqlRecord bool
+	SqlRecord bool
 }

+ 2 - 2
config/settings.yml

@@ -106,6 +106,6 @@ settings:
       appSecret: "VXX8H0MzT7"
       url: "http://msa.zs.test.js.sgcc.com.cn/jssms/api/sendsmsbatch" 
       appsGroup: "opentelemetry-demo:18611342234,18722341127;demo:18511342234,18622341127"
-      smsTpl: "【江苏省电力公司】\n所属系统:{{app_alias}}\n事件名称:{{events_name}}\n事件创建时间:{{date_time}}\n事件描述:{{events}}"
-      # sqlRecord: False
+      smsTpl: "所属系统:{{app_alias}}\n事件名称:{{events_name}}\n事件创建时间:{{date_time}}\n事件描述:{{events}}"
+      sqlRecord: true
 

+ 4 - 0
handler/events.go

@@ -2,6 +2,7 @@ package handler
 
 import (
 	"encoding/json"
+	"errors"
 	"fmt"
 	amodels "go-admin/app/admin/models"
 	omodels "go-admin/app/observe/models"
@@ -51,6 +52,9 @@ func InitEventHandler(pMap *sync.Map, smsInfo *SmsConfig) *EventHandler {
 	e.Errs = make(map[string]error)
 	e.promMap = pMap
 	e.CheckStartTime = time.Now()
+	if smsInfo == nil {
+		e.Errs["smsinf error"] = errors.New("smsinfo 解析错误")
+	}
 	return e
 }
 

+ 27 - 14
handler/handler.go

@@ -37,7 +37,6 @@ const (
 	SENDSUCCESS
 
 	DEFAULTSMSTPL = `
-【江苏省电力公司】
 所属系统:{{app_name}}
 事件名称:{{events_name}}
 事件创建时间:{{date_time}}
@@ -132,7 +131,7 @@ func GetPrometheusInstance() string {
 	return extConfig.ExtConfig.Prometheus.Address
 }
 
-func GetSmsConfigInstance() *SmsConfig {
+func GetSmsConfigInstance(db *gorm.DB) *SmsConfig {
 	sc := new(SmsConfig)
 	sc.Appkey = extConfig.ExtConfig.SmsConfig.Appkey
 	sc.Appsecret = extConfig.ExtConfig.SmsConfig.Appsecret
@@ -140,20 +139,34 @@ func GetSmsConfigInstance() *SmsConfig {
 	// TODO: set default sms template,需要修改为外部模版
 	sc.SmsTpl = DEFAULTSMSTPL
 	sc.Url = extConfig.ExtConfig.SmsConfig.Url
-	// sc.SQLRecord = extConfig.ExtConfig.SmsConfig.SQLRecord
+	sc.SQLRecord = extConfig.ExtConfig.SmsConfig.SqlRecord
+
+	//判断使用配置文件默认联系人还是应用管理的联系人,通常快速测试时使用配置文件,此时sqlrecord为false
 	recordMap := make(map[string][]string)
-	parts := strings.Split(extConfig.ExtConfig.SmsConfig.AppsGroup, ";")
-	for _, part := range parts {
-		if part == "" {
-			continue
+	if sc.SQLRecord {
+		apps := make([]amodels.OtApps, 0)
+		if err := db.Find(&apps).Error; err != nil {
+			logger.Error("sms, get app list err: ", err)
+			return nil
+		}
+		for _, app := range apps {
+			recordMap[app.Alias] = strings.Split(app.ContractPhone, ",")
 		}
-		subParts := strings.SplitN(part, ":", 2)
-		if len(subParts) != 2 {
-			continue
+	} else {
+		parts := strings.Split(extConfig.ExtConfig.SmsConfig.AppsGroup, ";")
+		for _, part := range parts {
+			if part == "" {
+				continue
+			}
+			subParts := strings.SplitN(part, ":", 2)
+			if len(subParts) != 2 {
+				continue
+			}
+			id := subParts[0]
+			phoneNumbers := subParts[1]
+			recordMap[id] = strings.Split(phoneNumbers, ",")
 		}
-		id := subParts[0]
-		phoneNumbers := subParts[1]
-		recordMap[id] = strings.Split(phoneNumbers, ",")
+
 	}
 	sc.AppsGroup = recordMap
 	logger.Info("sms phone map: ", sc.AppsGroup)
@@ -278,7 +291,7 @@ func (a *AlertManager) PolicyConsumer() {
 func (a *AlertManager) consumerHandler(i interface{}) {
 	var err error
 	c := 0
-	eHandler := InitEventHandler(a.PromMap, GetSmsConfigInstance())
+	eHandler := InitEventHandler(a.PromMap, GetSmsConfigInstance(a.DB))
 	rp, ok := i.(omodels.OtRulesPolicy)
 	if ok {
 		eHandler.RP = &rp

+ 2 - 1
handler/sms.go

@@ -108,6 +108,7 @@ func (e *EventHandler) SendMsg() *EventHandler {
 		e.Errs["sms json marshal error"] = err
 		return e
 	}
+	logger.Debug("sms json content: ", string(jsonData))
 
 	client := &http.Client{}
 	req, err := http.NewRequest("POST", e.SmsInfo.Url, strings.NewReader(string(jsonData)))
@@ -121,7 +122,7 @@ func (e *EventHandler) SendMsg() *EventHandler {
 	}
 	// 设定json格式
 	req.Header.Set("Content-Type", "application/json")
-
+	logger.Debug("sms json header: ", req.Header)
 	resp, err := client.Do(req)
 	if err != nil {
 		e.Errs["sms post do error"] = err