package signature_request import ( "encoding/json" "git.bvbej.com/bvbej/base-golang/pkg/aes" "git.bvbej.com/bvbej/base-golang/pkg/hmac" "git.bvbej.com/bvbej/base-golang/pkg/httpclient" "git.bvbej.com/bvbej/base-golang/pkg/time_parse" "github.com/tidwall/gjson" "net/url" "strconv" "time" ) type DingTalkTextAlert struct { MsgType string `json:"msgtype"` Text struct { Content string `json:"content"` } `json:"text"` } func HttpPostByAes(url, productID, key, vi string, params url.Values) (body []byte, err error) { var signatureDatetime = time_parse.CSTLayoutString() unix, _ := time_parse.CSTLayoutStringToUnix(signatureDatetime) params.Set("t", strconv.FormatInt(unix, 10)) var sign string sign, err = aes.New(key, vi).Encrypt(params.Encode()) if err != nil { return body, err } body, err = httpclient.PostForm( url, params, httpclient.WithTTL(time.Second*5), httpclient.WithHeader("Signature", sign), httpclient.WithHeader("Signature-Datetime", signatureDatetime), httpclient.WithHeader("Product-ID", productID), httpclient.WithOnFailedRetry(3, time.Second*1, func(body []byte) (shouldRetry bool) { return len(body) == 0 }, ), ) return body, err } func DingTalkAlert(accessToken, secret string, body json.RawMessage) bool { params := url.Values{} timestamp := strconv.FormatInt(time_parse.GetMicroTime(), 10) signData := timestamp + "\n" + secret sign := url.QueryEscape(hmac.New(secret).Sha256ToBase64String(signData)) params.Set("access_token", accessToken) params.Set("sign", sign) params.Set("timestamp", timestamp) response, err := httpclient.PostJSON( "https://oapi.dingtalk.com/robot/send?"+params.Encode(), body, httpclient.WithTTL(time.Second*5), httpclient.WithHeader("Content-Type", "application/json;charset=utf-8"), httpclient.WithOnFailedRetry(3, time.Second*1, func(body []byte) (shouldRetry bool) { return len(body) == 0 }, ), ) if err != nil { return false } return gjson.Get(string(response), "errcode").Int() == 0 } func DingTalkAlertText(accessToken, secret string, text string) bool { params := url.Values{} timestamp := strconv.FormatInt(time_parse.GetMicroTime(), 10) signData := timestamp + "\n" + secret sign := url.QueryEscape(hmac.New(secret).Sha256ToBase64String(signData)) params.Set("access_token", accessToken) params.Set("sign", sign) params.Set("timestamp", timestamp) var content DingTalkTextAlert content.MsgType = "text" content.Text.Content = text body, _ := json.Marshal(content) response, err := httpclient.PostJSON( "https://oapi.dingtalk.com/robot/send?"+params.Encode(), body, httpclient.WithTTL(time.Second*5), httpclient.WithHeader("Content-Type", "application/json;charset=utf-8"), httpclient.WithOnFailedRetry(3, time.Second*1, func(body []byte) (shouldRetry bool) { return len(body) == 0 }, ), ) if err != nil { return false } return gjson.Get(string(response), "errcode").Int() == 0 }