123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- 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).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 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).Encrypt(params.Encode())
- 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
- }
|