dw_msg.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package third
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_api/models"
  6. "eta_gn/eta_api/utils"
  7. "fmt"
  8. "io"
  9. "net/http"
  10. "time"
  11. )
  12. func HttpGet(url string) (body []byte, err error) {
  13. client := &http.Client{}
  14. req, err := http.NewRequest("GET", url, nil)
  15. if err != nil {
  16. return
  17. }
  18. nonce := utils.GetRandStringNoSpecialChar(16)
  19. timestamp := time.Now().Format(utils.FormatDateTimeUnSpace)
  20. signature := utils.GetSign(nonce, timestamp, utils.ETA_MINI_APPID, utils.ETA_MINI_APP_SECRET)
  21. req.Header.Add("Nonce", nonce)
  22. req.Header.Add("Timestamp", timestamp)
  23. req.Header.Add("Appid", utils.ETA_MINI_APPID)
  24. req.Header.Add("Signature", signature)
  25. req.Header.Set("Content-Type", "application/json")
  26. response, err := client.Do(req)
  27. if err != nil {
  28. return
  29. }
  30. defer response.Body.Close()
  31. body, err = io.ReadAll(response.Body)
  32. if err != nil {
  33. return
  34. }
  35. return
  36. }
  37. func DwSendTemplatedMsg(reportId int) error {
  38. url := fmt.Sprintf("%swechat/send_template_msg?ReportId=%d", utils.ETAMiniBridgeUrl, reportId)
  39. body, err := HttpGet(url)
  40. if err != nil {
  41. return err
  42. }
  43. result := new(models.BaseResponse)
  44. if err := json.Unmarshal(body, &result); err != nil {
  45. return err
  46. }
  47. if result.Ret != 200 {
  48. err = errors.New(string(body))
  49. return err
  50. }
  51. return nil
  52. }