base_lib.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package services
  2. import (
  3. "bytes"
  4. "eta/eta_mini_api/utils"
  5. "io"
  6. "net/http"
  7. "time"
  8. )
  9. func HttpGet(url string) (body []byte, err error) {
  10. client := &http.Client{}
  11. req, err := http.NewRequest("GET", url, nil)
  12. if err != nil {
  13. return
  14. }
  15. nonce := utils.GetRandStringNoSpecialChar(16)
  16. timestamp := time.Now().Format(utils.FormatDateTimeUnSpace)
  17. signature := utils.GetSign(nonce, timestamp, utils.ETA_MINI_APPID, utils.ETA_MINI_APP_SECRET)
  18. //增加header选项
  19. req.Header.Add("Nonce", nonce)
  20. req.Header.Add("Timestamp", timestamp)
  21. req.Header.Add("Appid", utils.ETA_MINI_APPID)
  22. req.Header.Add("Signature", signature)
  23. req.Header.Set("Content-Type", "application/json")
  24. response, err := client.Do(req)
  25. if err != nil {
  26. return
  27. }
  28. defer response.Body.Close()
  29. body, err = io.ReadAll(response.Body)
  30. if err != nil {
  31. return
  32. }
  33. utils.FileLog.Info("result:" + string(body))
  34. return
  35. }
  36. func HttpPost(url string, postBody []byte) (body []byte, err error) {
  37. client := &http.Client{}
  38. req, err := http.NewRequest("POST", url, bytes.NewReader(postBody))
  39. if err != nil {
  40. return
  41. }
  42. nonce := utils.GetRandStringNoSpecialChar(16)
  43. timestamp := time.Now().Format(utils.FormatDateTimeUnSpace)
  44. signature := utils.GetSign(nonce, timestamp, utils.ETA_MINI_APPID, utils.ETA_MINI_APP_SECRET)
  45. //增加header选项
  46. req.Header.Add("Nonce", nonce)
  47. req.Header.Add("Timestamp", timestamp)
  48. req.Header.Add("Appid", utils.ETA_MINI_APPID)
  49. req.Header.Add("Signature", signature)
  50. req.Header.Set("Content-Type", "application/json")
  51. response, err := client.Do(req)
  52. if err != nil {
  53. return
  54. }
  55. defer response.Body.Close()
  56. body, err = io.ReadAll(response.Body)
  57. if err != nil {
  58. return
  59. }
  60. utils.FileLog.Info("result:" + string(body))
  61. return
  62. }