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" netUrl "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 netUrl.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).EncryptCBC(params.Encode(), false) if err != nil { return body, err } body, err = httpclient.PostForm( url, params, httpclient.WithTTL(time.Second*30), httpclient.WithHeader("Signature", sign), httpclient.WithHeader("Signature-Datetime", signatureDatetime), httpclient.WithHeader("Product-ID", productID), httpclient.WithOnFailedRetry(1, time.Millisecond*200, func(body []byte) (shouldRetry bool) { return len(body) == 0 }, ), ) return body, err } func HttpPostByAesWithTimeout(url, productID, key, vi string, params netUrl.Values, timeout time.Duration) (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).EncryptCBC(params.Encode(), false) if err != nil { return body, err } body, err = httpclient.PostForm( url, params, httpclient.WithTTL(timeout), 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 := netUrl.Values{} timestamp := strconv.FormatInt(time_parse.GetMilliTimestamp(), 10) signData := timestamp + "\n" + secret sign := netUrl.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 := netUrl.Values{} timestamp := strconv.FormatInt(time_parse.GetMilliTimestamp(), 10) signData := timestamp + "\n" + secret sign := netUrl.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 }