瀏覽代碼

use robot

jason 1 年之前
父節點
當前提交
d88ef4049f
共有 2 個文件被更改,包括 32 次插入19 次删除
  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() {