dw_msg.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package third
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models"
  6. "eta/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. //增加header选项
  22. req.Header.Add("Nonce", nonce)
  23. req.Header.Add("Timestamp", timestamp)
  24. req.Header.Add("Appid", utils.ETA_MINI_APPID)
  25. req.Header.Add("Signature", signature)
  26. req.Header.Set("Content-Type", "application/json")
  27. response, err := client.Do(req)
  28. if err != nil {
  29. return
  30. }
  31. defer response.Body.Close()
  32. body, err = io.ReadAll(response.Body)
  33. if err != nil {
  34. return
  35. }
  36. return
  37. }
  38. func DwSendTemplatedMsg(reportId int) error {
  39. url := fmt.Sprintf("%swechat/send_template_msg?ReportId=%d", utils.ETAMiniBridgeUrl, reportId)
  40. body, err := HttpGet(url)
  41. if err != nil {
  42. return err
  43. }
  44. result := new(models.BaseResponse)
  45. if err := json.Unmarshal(body, &result); err != nil {
  46. return err
  47. }
  48. if result.Ret != 200 {
  49. err = errors.New(string(body))
  50. return err
  51. }
  52. return nil
  53. }