notify.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package service
  2. import (
  3. "bytes"
  4. "crypto/hmac"
  5. "crypto/sha256"
  6. "encoding/base64"
  7. "encoding/json"
  8. "fmt"
  9. "github.com/flipped-aurora/gin-vue-admin/server/plugin/notify/global"
  10. "io/ioutil"
  11. "net/http"
  12. "net/url"
  13. "time"
  14. )
  15. type NotifyService struct {
  16. }
  17. //@author: [Espoir](https://github.com/nightsimon)
  18. //@function: NotifyController
  19. //@description: 钉钉通知测试
  20. //@return: err error
  21. func (e *NotifyService) SendTextMessage(content string) error {
  22. msg := map[string]interface{}{
  23. "msgtype": "text",
  24. "text": map[string]string{
  25. "content": content,
  26. },
  27. //"at": map[string]interface{}{
  28. // "atMobiles": atMobiles,
  29. // "isAtAll": isAtAll,
  30. //},
  31. }
  32. return SendMessage(msg)
  33. }
  34. func SendMessage(msg interface{}) error {
  35. body := bytes.NewBuffer(nil)
  36. err := json.NewEncoder(body).Encode(msg)
  37. if err != nil {
  38. return fmt.Errorf("msg json failed, msg: %v, err: %v", msg, err.Error())
  39. }
  40. value := url.Values{}
  41. value.Set("access_token", global.GlobalConfig_.Token)
  42. if global.GlobalConfig_.Secret != "" {
  43. t := time.Now().UnixNano() / 1e6
  44. value.Set("timestamp", fmt.Sprintf("%d", t))
  45. value.Set("sign", sign(t, global.GlobalConfig_.Secret))
  46. }
  47. request, err := http.NewRequest(http.MethodPost, global.GlobalConfig_.Url, body)
  48. if err != nil {
  49. return fmt.Errorf("error request: %v", err.Error())
  50. }
  51. request.URL.RawQuery = value.Encode()
  52. request.Header.Add("Content-Type", "application/json")
  53. res, err := (&http.Client{}).Do(request)
  54. if err != nil {
  55. return fmt.Errorf("send dingTalk message failed, error: %v", err.Error())
  56. }
  57. defer func() { _ = res.Body.Close() }()
  58. result, err := ioutil.ReadAll(res.Body)
  59. if res.StatusCode != 200 {
  60. return fmt.Errorf("send dingTalk message failed, %s", httpError(request, res, result, "http code is not 200"))
  61. }
  62. if err != nil {
  63. return fmt.Errorf("send dingTalk message failed, %s", httpError(request, res, result, err.Error()))
  64. }
  65. type response struct {
  66. ErrCode int `json:"errcode"`
  67. }
  68. var ret response
  69. if err := json.Unmarshal(result, &ret); err != nil {
  70. return fmt.Errorf("send dingTalk message failed, %s", httpError(request, res, result, err.Error()))
  71. }
  72. if ret.ErrCode != 0 {
  73. return fmt.Errorf("send dingTalk message failed, %s", httpError(request, res, result, "errcode is not 0"))
  74. }
  75. return nil
  76. }
  77. func httpError(request *http.Request, response *http.Response, body []byte, error string) string {
  78. return fmt.Sprintf(
  79. "http request failure, error: %s, status code: %d, %s %s, body:\n%s",
  80. error,
  81. response.StatusCode,
  82. request.Method,
  83. request.URL.String(),
  84. string(body),
  85. )
  86. }
  87. func sign(t int64, secret string) string {
  88. strToHash := fmt.Sprintf("%d\n%s", t, secret)
  89. hmac256 := hmac.New(sha256.New, []byte(secret))
  90. hmac256.Write([]byte(strToHash))
  91. data := hmac256.Sum(nil)
  92. return base64.StdEncoding.EncodeToString(data)
  93. }