http.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. escape := netUrl.QueryEscape(params.Encode())
  25. sign, err = aes.New(key, vi).Encrypt(escape)
  26. if err != nil {
  27. return body, err
  28. }
  29. body, err = httpclient.PostForm(
  30. url,
  31. params,
  32. httpclient.WithTTL(time.Second*5),
  33. httpclient.WithHeader("Signature", sign),
  34. httpclient.WithHeader("Signature-Datetime", signatureDatetime),
  35. httpclient.WithHeader("Product-ID", productID),
  36. httpclient.WithOnFailedRetry(3, time.Second*1,
  37. func(body []byte) (shouldRetry bool) {
  38. return len(body) == 0
  39. },
  40. ),
  41. )
  42. return body, err
  43. }
  44. func DingTalkAlert(accessToken, secret string, body json.RawMessage) bool {
  45. params := netUrl.Values{}
  46. timestamp := strconv.FormatInt(time_parse.GetMilliTimestamp(), 10)
  47. signData := timestamp + "\n" + secret
  48. sign := netUrl.QueryEscape(hmac.New(secret).Sha256ToBase64String(signData))
  49. params.Set("access_token", accessToken)
  50. params.Set("sign", sign)
  51. params.Set("timestamp", timestamp)
  52. response, err := httpclient.PostJSON(
  53. "https://oapi.dingtalk.com/robot/send?"+params.Encode(),
  54. body,
  55. httpclient.WithTTL(time.Second*5),
  56. httpclient.WithHeader("Content-Type", "application/json;charset=utf-8"),
  57. httpclient.WithOnFailedRetry(3, time.Second*1,
  58. func(body []byte) (shouldRetry bool) {
  59. return len(body) == 0
  60. },
  61. ),
  62. )
  63. if err != nil {
  64. return false
  65. }
  66. return gjson.Get(string(response), "errcode").Int() == 0
  67. }
  68. func DingTalkAlertText(accessToken, secret string, text string) bool {
  69. params := netUrl.Values{}
  70. timestamp := strconv.FormatInt(time_parse.GetMilliTimestamp(), 10)
  71. signData := timestamp + "\n" + secret
  72. sign := netUrl.QueryEscape(hmac.New(secret).Sha256ToBase64String(signData))
  73. params.Set("access_token", accessToken)
  74. params.Set("sign", sign)
  75. params.Set("timestamp", timestamp)
  76. var content DingTalkTextAlert
  77. content.MsgType = "text"
  78. content.Text.Content = 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. }