eta_bridge.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package eta_bridge
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_data_init/utils"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "strings"
  10. )
  11. // BaseEtaBridgeDataResp 基础返回参数
  12. type BaseEtaBridgeDataResp struct {
  13. Code int `json:"code" description:"状态码"`
  14. Msg string `json:"msg" description:"提示信息"`
  15. //Data GetCrmTokenData `json:"data" description:"返回数据"`
  16. }
  17. // HttpEtaBridgePost
  18. // @Description: eta-bridge的post请求
  19. // @author: Roc
  20. // @datetime 2024-02-28 16:55:02
  21. // @param urlStr string
  22. // @param postData string
  23. // @param param interface{}
  24. // @return bResult []byte
  25. // @return err error
  26. // @return errMsg string
  27. func HttpEtaBridgePost(urlStr string, param interface{}) (bResult []byte, err error, errMsg string) {
  28. if utils.EtaBridgeUrl == "" {
  29. errMsg = `未配置第三方的桥接服务地址`
  30. err = errors.New(errMsg)
  31. return
  32. }
  33. data, e := json.Marshal(param)
  34. if e != nil {
  35. err = fmt.Errorf("data json marshal err: %s", e.Error())
  36. return
  37. }
  38. postUrl := utils.EtaBridgeUrl + "/api" + urlStr
  39. body := io.NopCloser(strings.NewReader(string(data)))
  40. client := &http.Client{}
  41. req, e := http.NewRequest("POST", postUrl, body)
  42. if e != nil {
  43. err = fmt.Errorf("http create request err: %s", e.Error())
  44. return
  45. }
  46. contentType := "application/json;charset=utf-8"
  47. req.Header.Set("Content-Type", contentType)
  48. checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
  49. req.Header.Set("Authorization", checkToken)
  50. resp, e := client.Do(req)
  51. if e != nil {
  52. err = fmt.Errorf("http client do err: %s", e.Error())
  53. return
  54. }
  55. defer func() {
  56. _ = resp.Body.Close()
  57. }()
  58. bResult, e = io.ReadAll(resp.Body)
  59. if e != nil {
  60. err = fmt.Errorf("resp body read err: %s", e.Error())
  61. return
  62. }
  63. if len(bResult) == 0 {
  64. err = fmt.Errorf("resp body is empty")
  65. return
  66. }
  67. // 生产环境解密, 注意有个坑前后的双引号
  68. if utils.RunMode == "release" {
  69. str := string(bResult)
  70. str = strings.Trim(str, `"`)
  71. bResult = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
  72. }
  73. result := new(BaseEtaBridgeDataResp)
  74. if e = json.Unmarshal(bResult, &result); e != nil {
  75. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(bResult))
  76. utils.FileLog.Info("桥接服务post请求失败:\n" + string(bResult))
  77. return
  78. }
  79. if result.Code != 200 {
  80. errMsg = result.Msg
  81. err = fmt.Errorf("result: %s", string(bResult))
  82. return
  83. }
  84. return
  85. }
  86. // HttpEtaBridgeGet
  87. // @Description: eta-bridge的get请求
  88. // @author: Roc
  89. // @datetime 2024-02-28 16:55:02
  90. // @param urlStr string
  91. // @param postData string
  92. // @return bResult []byte
  93. // @return err error
  94. // @return errMsg string
  95. func HttpEtaBridgeGet(urlStr string) (bResult []byte, err error, errMsg string) {
  96. if utils.EtaBridgeUrl == "" {
  97. errMsg = `未配置第三方的桥接服务地址`
  98. err = errors.New(errMsg)
  99. return
  100. }
  101. postUrl := utils.EtaBridgeUrl + "/api" + urlStr
  102. client := &http.Client{}
  103. req, e := http.NewRequest("GET", postUrl, nil)
  104. if e != nil {
  105. err = fmt.Errorf("http create request err: %s", e.Error())
  106. return
  107. }
  108. contentType := "application/json;charset=utf-8"
  109. req.Header.Set("Content-Type", contentType)
  110. checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
  111. req.Header.Set("Authorization", checkToken)
  112. resp, e := client.Do(req)
  113. if e != nil {
  114. err = fmt.Errorf("http client do err: %s", e.Error())
  115. return
  116. }
  117. defer func() {
  118. _ = resp.Body.Close()
  119. }()
  120. bResult, e = io.ReadAll(resp.Body)
  121. if e != nil {
  122. err = fmt.Errorf("resp body read err: %s", e.Error())
  123. return
  124. }
  125. if len(bResult) == 0 {
  126. err = fmt.Errorf("resp body is empty")
  127. return
  128. }
  129. // 生产环境解密, 注意有个坑前后的双引号
  130. if utils.RunMode == "release" {
  131. str := string(bResult)
  132. str = strings.Trim(str, `"`)
  133. bResult = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
  134. }
  135. result := new(BaseEtaBridgeDataResp)
  136. if e = json.Unmarshal(bResult, &result); e != nil {
  137. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(bResult))
  138. utils.FileLog.Info("桥接服务get请求失败:\n" + string(bResult))
  139. return
  140. }
  141. if result.Code != 200 {
  142. errMsg = result.Msg
  143. err = fmt.Errorf("result: %s", string(bResult))
  144. return
  145. }
  146. return
  147. }