jason 1 yıl önce
ebeveyn
işleme
d88ef4049f
2 değiştirilmiş dosya ile 32 ekleme ve 19 silme
  1. 1 0
      common.in/config/config.go
  2. 31 19
      utils/hystrix_config.go

+ 1 - 0
common.in/config/config.go

@@ -84,6 +84,7 @@ type ThirdPartConfig struct {
 	DingTalkWebhook       string `json:"ding_talk_webhook"`
 	AdmAppkey             string `json:"adm_appkey"`
 	HystrixPublishChannel string `json:"hystrix_publish_channel"`
+	RobotUrl              string `json:"robot_url"`
 }
 
 type Configure struct {

+ 31 - 19
utils/hystrix_config.go

@@ -8,16 +8,17 @@ import (
 	"fmt"
 	"gd_vehicle/consts"
 	"gd_vehicle/errors"
-	"io/ioutil"
-	"net/http"
-	"strings"
 	"sync"
 	"time"
 
 	"gd_vehicle/common.in/cache"
 	"gd_vehicle/common.in/config"
+	"gd_vehicle/thirdparty"
+
+	"go.uber.org/zap"
 
 	"github.com/astaxie/beego/orm"
+	"github.com/tidwall/gjson"
 
 	"github.com/afex/hystrix-go/hystrix"
 )
@@ -158,25 +159,36 @@ type TextMsg struct {
 }
 
 func SendToDingTalk(content string) {
-	textMsg := TextMsg{}
-	textMsg.MsgType = "text"
-	textMsg.Text = Text{}
-	textMsg.Text.Content = content
-	webHook := config.Conf.ThirdPart.DingTalkWebhook
-	contentByte, _ := json.Marshal(textMsg)
-	req, err := http.NewRequest("POST", webHook, strings.NewReader(string(contentByte)))
-	client := &http.Client{}
-	req.Header.Set("Content-Type", "application/json")
-	resp, err := client.Do(req)
-	defer resp.Body.Close()
+	body := map[string]interface{}{
+		"msgtype": "text",
+		"text":    map[string]interface{}{"content": content},
+	}
+	bytes, _ := json.Marshal(body)
+
+	h := thirdparty.HttpRequestWithHeadCommon{
+		Method:  "POST",
+		Url:     config.Conf.ThirdPart.RobotUrl,
+		Body:    bytes,
+		TimeOut: 10 * time.Second,
+	}
+	bytes, err := h.Request()
 	if err != nil {
-		fmt.Println("send error:", err)
+		l.Error("func",
+			zap.String("call", "RobotMsg"),
+			zap.String("params", content),
+			zap.String("error", err.Error()))
 		return
 	}
-
-	fmt.Println(resp.StatusCode)
-	body, _ := ioutil.ReadAll(resp.Body)
-	fmt.Println(string(body))
+	errcode := gjson.GetBytes(bytes, "errcode").Int()
+	errmsg := gjson.GetBytes(bytes, "errmsg").String()
+	if errcode != 0 {
+		l.Error("func",
+			zap.String("call", "RobotMsg"),
+			zap.String("params", content),
+			zap.String("error", errmsg))
+		return
+	}
+	return
 }
 
 func ReloadHystrix() {