http.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package signature_request
  2. import (
  3. "encoding/json"
  4. "github.com/tidwall/gjson"
  5. "git.bvbej.com/bvbej/base-golang/pkg/aes"
  6. "git.bvbej.com/bvbej/base-golang/pkg/hmac"
  7. "git.bvbej.com/bvbej/base-golang/pkg/httpclient"
  8. "git.bvbej.com/bvbej/base-golang/pkg/time_parse"
  9. "net/url"
  10. "strconv"
  11. "time"
  12. )
  13. type DingTalkTextAlert struct {
  14. MsgType string `json:"msgtype"`
  15. Text struct {
  16. Content string `json:"content"`
  17. } `json:"text"`
  18. }
  19. func HttpPostByAes(url, productID, key, vi string, params url.Values) (body []byte, err error) {
  20. t := time.Now()
  21. params.Set("t", strconv.FormatInt(t.Unix(), 10))
  22. var sign string
  23. sign, err = aes.New(key, vi).Encrypt(params.Encode())
  24. if err != nil {
  25. return body, err
  26. }
  27. body, err = httpclient.PostForm(
  28. url,
  29. params,
  30. httpclient.WithTTL(time.Second*5),
  31. httpclient.WithHeader("Signature", sign),
  32. httpclient.WithHeader("Signature-Date", t.Format(time_parse.CSTLayout)),
  33. httpclient.WithHeader("Product-ID", productID),
  34. httpclient.WithOnFailedRetry(3, time.Second*1,
  35. func(body []byte) (shouldRetry bool) {
  36. return len(body) == 0
  37. },
  38. ),
  39. )
  40. return body, err
  41. }
  42. func DingTalkAlert(accessToken, secret string, body json.RawMessage) bool {
  43. params := url.Values{}
  44. timestamp := strconv.FormatInt(time_parse.GetMicroTime(), 10)
  45. signData := timestamp + "\n" + secret
  46. sign := url.QueryEscape(hmac.New(secret).Sha256ToBase64String(signData))
  47. params.Set("access_token", accessToken)
  48. params.Set("sign", sign)
  49. params.Set("timestamp", timestamp)
  50. response, err := httpclient.PostJSON(
  51. "https://oapi.dingtalk.com/robot/send?"+params.Encode(),
  52. body,
  53. httpclient.WithTTL(time.Second*5),
  54. httpclient.WithHeader("Content-Type", "application/json;charset=utf-8"),
  55. httpclient.WithOnFailedRetry(3, time.Second*1,
  56. func(body []byte) (shouldRetry bool) {
  57. return len(body) == 0
  58. },
  59. ),
  60. )
  61. if err != nil {
  62. return false
  63. }
  64. return gjson.Get(string(response), "errcode").Int() == 0
  65. }
  66. func DingTalkAlertText(accessToken, secret string, text string) bool {
  67. params := url.Values{}
  68. timestamp := strconv.FormatInt(time_parse.GetMicroTime(), 10)
  69. signData := timestamp + "\n" + secret
  70. sign := url.QueryEscape(hmac.New(secret).Sha256ToBase64String(signData))
  71. params.Set("access_token", accessToken)
  72. params.Set("sign", sign)
  73. params.Set("timestamp", timestamp)
  74. var content DingTalkTextAlert
  75. content.MsgType = "text"
  76. content.Text.Content = text
  77. body, _ := json.Marshal(content)
  78. response, err := httpclient.PostJSON(
  79. "https://oapi.dingtalk.com/robot/send?"+params.Encode(),
  80. body,
  81. httpclient.WithTTL(time.Second*5),
  82. httpclient.WithHeader("Content-Type", "application/json;charset=utf-8"),
  83. httpclient.WithOnFailedRetry(3, time.Second*1,
  84. func(body []byte) (shouldRetry bool) {
  85. return len(body) == 0
  86. },
  87. ),
  88. )
  89. if err != nil {
  90. return false
  91. }
  92. return gjson.Get(string(response), "errcode").Int() == 0
  93. }