1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- package signature_request
- import (
- "encoding/json"
- "github.com/tidwall/gjson"
- "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"
- "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) {
- t := time.Now()
- params.Set("t", strconv.FormatInt(t.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-Date", t.Format(time_parse.CSTLayout)),
- 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
- }
|