http.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package tool
  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. func HttpPostByAes(url, productID, key, vi string, params netUrl.Values) (body []byte, err error) {
  14. var signatureDatetime = time_parse.CSTLayoutString()
  15. unix, _ := time_parse.CSTLayoutStringToUnix(signatureDatetime)
  16. params.Set("t", strconv.FormatInt(unix, 10))
  17. var sign string
  18. sign, err = aes.New(key, vi).EncryptCBC(params.Encode(), false)
  19. if err != nil {
  20. return body, err
  21. }
  22. body, err = httpclient.PostForm(
  23. url,
  24. params,
  25. httpclient.WithTTL(time.Second*30),
  26. httpclient.WithHeader("Signature", sign),
  27. httpclient.WithHeader("Signature-Datetime", signatureDatetime),
  28. httpclient.WithHeader("Product-ID", productID),
  29. httpclient.WithOnFailedRetry(1, time.Millisecond*200,
  30. func(body []byte) (shouldRetry bool) {
  31. return len(body) == 0
  32. },
  33. ),
  34. )
  35. return body, err
  36. }
  37. func HttpPostByAesWithTimeout(url, productID, key, vi string, params netUrl.Values, timeout time.Duration) (body []byte, err error) {
  38. var signatureDatetime = time_parse.CSTLayoutString()
  39. unix, _ := time_parse.CSTLayoutStringToUnix(signatureDatetime)
  40. params.Set("t", strconv.FormatInt(unix, 10))
  41. var sign string
  42. sign, err = aes.New(key, vi).EncryptCBC(params.Encode(), false)
  43. if err != nil {
  44. return body, err
  45. }
  46. body, err = httpclient.PostForm(
  47. url,
  48. params,
  49. httpclient.WithTTL(timeout),
  50. httpclient.WithHeader("Signature", sign),
  51. httpclient.WithHeader("Signature-Datetime", signatureDatetime),
  52. httpclient.WithHeader("Product-ID", productID),
  53. httpclient.WithOnFailedRetry(3, time.Second*1,
  54. func(body []byte) (shouldRetry bool) {
  55. return len(body) == 0
  56. },
  57. ),
  58. )
  59. return body, err
  60. }
  61. func DingTalkAlertMarkdown(accessToken, secret, title, text string) bool {
  62. params := netUrl.Values{}
  63. timestamp := strconv.FormatInt(time_parse.GetMilliTimestamp(), 10)
  64. signData := timestamp + "\n" + secret
  65. sign := netUrl.QueryEscape(hmac.New(secret).Sha256ToBase64String(signData))
  66. params.Set("access_token", accessToken)
  67. params.Set("sign", sign)
  68. params.Set("timestamp", timestamp)
  69. var content struct {
  70. MsgType string `json:"msgtype"`
  71. Markdown struct {
  72. Title string `json:"title"`
  73. Text string `json:"text"`
  74. } `json:"markdown"`
  75. }
  76. content.MsgType = "markdown"
  77. content.Markdown.Title = title
  78. content.Markdown.Text = text
  79. body, _ := json.Marshal(content)
  80. response, err := httpclient.PostJSON(
  81. "https://oapi.dingtalk.com/robot/send?"+params.Encode(),
  82. body,
  83. httpclient.WithTTL(time.Second*5),
  84. httpclient.WithHeader("Content-Type", "application/json;charset=utf-8"),
  85. httpclient.WithOnFailedRetry(3, time.Second*1,
  86. func(body []byte) (shouldRetry bool) {
  87. return len(body) == 0
  88. },
  89. ),
  90. )
  91. if err != nil {
  92. return false
  93. }
  94. return gjson.Get(string(response), "errcode").Int() == 0
  95. }
  96. func DingTalkAlertText(accessToken, secret string, text string) bool {
  97. params := netUrl.Values{}
  98. timestamp := strconv.FormatInt(time_parse.GetMilliTimestamp(), 10)
  99. signData := timestamp + "\n" + secret
  100. sign := netUrl.QueryEscape(hmac.New(secret).Sha256ToBase64String(signData))
  101. params.Set("access_token", accessToken)
  102. params.Set("sign", sign)
  103. params.Set("timestamp", timestamp)
  104. var content struct {
  105. MsgType string `json:"msgtype"`
  106. Text struct {
  107. Content string `json:"content"`
  108. } `json:"text"`
  109. }
  110. content.MsgType = "text"
  111. content.Text.Content = text
  112. body, _ := json.Marshal(content)
  113. response, err := httpclient.PostJSON(
  114. "https://oapi.dingtalk.com/robot/send?"+params.Encode(),
  115. body,
  116. httpclient.WithTTL(time.Second*5),
  117. httpclient.WithHeader("Content-Type", "application/json;charset=utf-8"),
  118. httpclient.WithOnFailedRetry(3, time.Second*1,
  119. func(body []byte) (shouldRetry bool) {
  120. return len(body) == 0
  121. },
  122. ),
  123. )
  124. if err != nil {
  125. return false
  126. }
  127. return gjson.Get(string(response), "errcode").Int() == 0
  128. }