http.go 2.9 KB

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