http.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. netUrl "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 netUrl.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 HttpPostByAesWithTimeout(url, productID, key, vi string, params netUrl.Values, timeout time.Duration) (body []byte, err error) {
  44. var signatureDatetime = time_parse.CSTLayoutString()
  45. unix, _ := time_parse.CSTLayoutStringToUnix(signatureDatetime)
  46. params.Set("t", strconv.FormatInt(unix, 10))
  47. var sign string
  48. sign, err = aes.New(key, vi).Encrypt(params.Encode())
  49. if err != nil {
  50. return body, err
  51. }
  52. body, err = httpclient.PostForm(
  53. url,
  54. params,
  55. httpclient.WithTTL(timeout),
  56. httpclient.WithHeader("Signature", sign),
  57. httpclient.WithHeader("Signature-Datetime", signatureDatetime),
  58. httpclient.WithHeader("Product-ID", productID),
  59. httpclient.WithOnFailedRetry(3, time.Second*1,
  60. func(body []byte) (shouldRetry bool) {
  61. return len(body) == 0
  62. },
  63. ),
  64. )
  65. return body, err
  66. }
  67. func DingTalkAlert(accessToken, secret string, body json.RawMessage) bool {
  68. params := netUrl.Values{}
  69. timestamp := strconv.FormatInt(time_parse.GetMilliTimestamp(), 10)
  70. signData := timestamp + "\n" + secret
  71. sign := netUrl.QueryEscape(hmac.New(secret).Sha256ToBase64String(signData))
  72. params.Set("access_token", accessToken)
  73. params.Set("sign", sign)
  74. params.Set("timestamp", timestamp)
  75. response, err := httpclient.PostJSON(
  76. "https://oapi.dingtalk.com/robot/send?"+params.Encode(),
  77. body,
  78. httpclient.WithTTL(time.Second*5),
  79. httpclient.WithHeader("Content-Type", "application/json;charset=utf-8"),
  80. httpclient.WithOnFailedRetry(3, time.Second*1,
  81. func(body []byte) (shouldRetry bool) {
  82. return len(body) == 0
  83. },
  84. ),
  85. )
  86. if err != nil {
  87. return false
  88. }
  89. return gjson.Get(string(response), "errcode").Int() == 0
  90. }
  91. func DingTalkAlertText(accessToken, secret string, text string) bool {
  92. params := netUrl.Values{}
  93. timestamp := strconv.FormatInt(time_parse.GetMilliTimestamp(), 10)
  94. signData := timestamp + "\n" + secret
  95. sign := netUrl.QueryEscape(hmac.New(secret).Sha256ToBase64String(signData))
  96. params.Set("access_token", accessToken)
  97. params.Set("sign", sign)
  98. params.Set("timestamp", timestamp)
  99. var content DingTalkTextAlert
  100. content.MsgType = "text"
  101. content.Text.Content = text
  102. body, _ := json.Marshal(content)
  103. response, err := httpclient.PostJSON(
  104. "https://oapi.dingtalk.com/robot/send?"+params.Encode(),
  105. body,
  106. httpclient.WithTTL(time.Second*5),
  107. httpclient.WithHeader("Content-Type", "application/json;charset=utf-8"),
  108. httpclient.WithOnFailedRetry(3, time.Second*1,
  109. func(body []byte) (shouldRetry bool) {
  110. return len(body) == 0
  111. },
  112. ),
  113. )
  114. if err != nil {
  115. return false
  116. }
  117. return gjson.Get(string(response), "errcode").Int() == 0
  118. }