package dingding import ( "bytes" "context" "encoding/json" "fmt" "io/ioutil" "net/http" dingMod "com.snow.auto_monitor/app/models/dinglist" "github.com/qit-team/snow-core/log/logger" ) var token string = "" func AccessToken(client_key string, client_secret string) { targetUrl := "http://121.199.38.107:8000/oauth/v1/accesstoken" data := map[string]interface{}{ "client_key": client_key, "client_secret": client_secret, } jsonData, err := json.Marshal(data) if err != nil { logger.Error(context.TODO(), "dingding", err) return } req, err := http.NewRequest("POST", targetUrl, bytes.NewBuffer(jsonData)) if err != nil { logger.Error(context.TODO(), "dingding", err) return } // 设置请求头,指定内容类型为JSON req.Header.Set("Content-Type", "application/json") // 发送请求并获取响应 client := &http.Client{} resp, err := client.Do(req) if err != nil { logger.Error(context.TODO(), "dingding", err) return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { logger.Error(context.TODO(), "dingding", err) return } // fmt.Println("响应状态码:", resp.StatusCode) // fmt.Println("响应内容:", string(body)) if resp.StatusCode != 200 { logger.Error(context.TODO(), "dingding", "钉钉发送公告失败") logger.Error(context.TODO(), "dingding", string(body)) return } // 定义结构体用于解析JSON数据 type Data struct { AccessToken string `json:"accessToken"` AccessExpire int64 `json:"accessExpire"` } type Response struct { Code int `json:"code"` Msg string `json:"msg"` Data Data `json:"data"` } // 解析JSON数据 var res Response err = json.Unmarshal([]byte(body), &res) if err != nil { fmt.Println("解析JSON失败:", err) return } // 获取并打印accessToken accessToken := res.Data.AccessToken // fmt.Println("accessToken:", accessToken) token = accessToken } func DingSend(title string, content string, retry bool) (err error) { dlist, err := dingMod.GetInstance().Search(0, 0, "", "", "", 999, 0) if err != nil { logger.Error(context.TODO(), "dingding", err) return } deptid_list := []int64{} userid_list := []string{} for _, v := range dlist { deptid_list = append(deptid_list, v.DepartmentId) userid_list = append(userid_list, v.UserId) } targetUrl := "http://121.199.38.107:8000/msg/v1/dingtalk/blackboard/send" data := map[string]interface{}{ "base": map[string]string{ "server_index": "transfer_msg", "temp_index": "transferding", }, "blackboard_receiver": map[string]interface{}{ "deptid_list": deptid_list, "userid_list": userid_list, }, "title": title, "content": content, } jsonData, err := json.Marshal(data) if err != nil { logger.Error(context.TODO(), "dingding", err) return } req, err := http.NewRequest("POST", targetUrl, bytes.NewBuffer(jsonData)) if err != nil { logger.Error(context.TODO(), "dingding", err) return } // 设置请求头,指定内容类型为JSON req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", token) // 发送请求并获取响应 client := &http.Client{} resp, err := client.Do(req) if err != nil { logger.Error(context.TODO(), "dingding", err) return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { logger.Error(context.TODO(), "dingding", err) return } // fmt.Println("响应状态码:", resp.StatusCode) // fmt.Println("响应内容:", string(body)) if resp.StatusCode != 200 { logger.Error(context.TODO(), "dingding", "钉钉发送公告失败") logger.Error(context.TODO(), "dingding", string(body)) return } // 定义结构体用于解析JSON数据 type Data struct { Code int64 `json:"code"` Msg string `json:"msg"` } // 解析JSON数据 var res Data err = json.Unmarshal([]byte(body), &res) if err != nil { fmt.Println("解析JSON失败:", err) return } if res.Code == 2 { AccessToken("transfer123", "transfer321") if retry { DingSend(title, content, false) } } return } func init() { fmt.Println("初始化钉钉接口") AccessToken("transfer123", "transfer321") }