eta_bridge.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package eta_bridge
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_task/utils"
  6. "fmt"
  7. "github.com/rdlucklib/rdluck_tools/paging"
  8. "io"
  9. "net/http"
  10. "strings"
  11. )
  12. // BaseEtaBridgeDataResp 基础返回参数
  13. type BaseEtaBridgeDataResp struct {
  14. Code int `json:"code" description:"状态码"`
  15. Msg string `json:"msg" description:"提示信息"`
  16. //Data GetCrmTokenData `json:"data" description:"返回数据"`
  17. }
  18. // HttpEtaBridgePost
  19. // @Description: eta-bridge的post请求
  20. // @author: Roc
  21. // @datetime 2024-02-28 16:55:02
  22. // @param urlStr string
  23. // @param postData string
  24. // @param param interface{}
  25. // @return bResult []byte
  26. // @return err error
  27. // @return errMsg string
  28. func HttpEtaBridgePost(urlStr string, param interface{}) (bResult []byte, err error, errMsg string) {
  29. if utils.EtaBridgeUrl == "" {
  30. errMsg = `未配置第三方的桥接服务地址`
  31. err = errors.New(errMsg)
  32. return
  33. }
  34. data, e := json.Marshal(param)
  35. if e != nil {
  36. err = fmt.Errorf("data json marshal err: %s", e.Error())
  37. return
  38. }
  39. postUrl := utils.EtaBridgeUrl + "/api" + urlStr
  40. body := io.NopCloser(strings.NewReader(string(data)))
  41. client := &http.Client{}
  42. req, e := http.NewRequest("POST", postUrl, body)
  43. if e != nil {
  44. err = fmt.Errorf("http create request err: %s", e.Error())
  45. return
  46. }
  47. contentType := "application/json;charset=utf-8"
  48. req.Header.Set("Content-Type", contentType)
  49. checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
  50. req.Header.Set("Authorization", checkToken)
  51. resp, e := client.Do(req)
  52. if e != nil {
  53. err = fmt.Errorf("http client do err: %s", e.Error())
  54. return
  55. }
  56. defer func() {
  57. _ = resp.Body.Close()
  58. }()
  59. bResult, e = io.ReadAll(resp.Body)
  60. if e != nil {
  61. err = fmt.Errorf("resp body read err: %s", e.Error())
  62. return
  63. }
  64. if len(bResult) == 0 {
  65. err = fmt.Errorf("resp body is empty")
  66. return
  67. }
  68. // 生产环境解密, 注意有个坑前后的双引号
  69. if utils.RunMode == "release" {
  70. str := string(bResult)
  71. str = strings.Trim(str, `"`)
  72. bResult = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
  73. }
  74. result := new(BaseEtaBridgeDataResp)
  75. if e = json.Unmarshal(bResult, &result); e != nil {
  76. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(bResult))
  77. utils.FileLog.Info("桥接服务post请求失败:\n" + string(bResult))
  78. return
  79. }
  80. if result.Code != 200 {
  81. errMsg = result.Msg
  82. err = fmt.Errorf("result: %s", string(bResult))
  83. return
  84. }
  85. return
  86. }
  87. // HttpEtaBridgeGet
  88. // @Description: eta-bridge的get请求
  89. // @author: Roc
  90. // @datetime 2024-02-28 16:55:02
  91. // @param urlStr string
  92. // @param postData string
  93. // @return bResult []byte
  94. // @return err error
  95. // @return errMsg string
  96. func HttpEtaBridgeGet(urlStr string) (bResult []byte, err error, errMsg string) {
  97. if utils.EtaBridgeUrl == "" {
  98. errMsg = `未配置第三方的桥接服务地址`
  99. err = errors.New(errMsg)
  100. return
  101. }
  102. getUrl := utils.EtaBridgeUrl + "/api" + urlStr
  103. client := &http.Client{}
  104. req, e := http.NewRequest("GET", getUrl, nil)
  105. if e != nil {
  106. err = fmt.Errorf("http create request err: %s", e.Error())
  107. return
  108. }
  109. contentType := "application/json;charset=utf-8"
  110. req.Header.Set("Content-Type", contentType)
  111. checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
  112. req.Header.Set("Authorization", checkToken)
  113. resp, e := client.Do(req)
  114. if e != nil {
  115. err = fmt.Errorf("http client do err: %s", e.Error())
  116. return
  117. }
  118. defer func() {
  119. _ = resp.Body.Close()
  120. }()
  121. bResult, e = io.ReadAll(resp.Body)
  122. if e != nil {
  123. err = fmt.Errorf("resp body read err: %s", e.Error())
  124. return
  125. }
  126. if len(bResult) == 0 {
  127. err = fmt.Errorf("resp body is empty")
  128. return
  129. }
  130. // 生产环境解密, 注意有个坑前后的双引号
  131. if utils.RunMode == "release" {
  132. str := string(bResult)
  133. str = strings.Trim(str, `"`)
  134. bResult = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
  135. }
  136. result := new(BaseEtaBridgeDataResp)
  137. if e = json.Unmarshal(bResult, &result); e != nil {
  138. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(bResult))
  139. utils.FileLog.Info("桥接服务get请求失败,url", getUrl, ":\n", string(bResult))
  140. return
  141. }
  142. if result.Code != 200 {
  143. errMsg = result.Msg
  144. err = fmt.Errorf("result: %s", string(bResult))
  145. return
  146. }
  147. return
  148. }
  149. // EtaBridgeDataRespAndBusinessData 基础返回参数
  150. type EtaBridgeDataRespAndBusinessData struct {
  151. Code int `json:"code" description:"状态码"`
  152. Msg string `json:"msg" description:"提示信息"`
  153. Data GetBusinessDataResp `json:"data" description:"返回数据"`
  154. }
  155. type GetBusinessDataResp struct {
  156. List []PushBusinessIndex
  157. Paging *paging.PagingItem `description:"分页数据"`
  158. }
  159. // PushBusinessIndex
  160. // @Description: 添加外部指标(商家)请求
  161. type PushBusinessIndex struct {
  162. IndexCode string `description:"指标编码"`
  163. IndexName string `description:"指标名称"`
  164. Unit string `description:"单位"`
  165. Frequency string `description:"频度"`
  166. SourceName string `description:"数据来源名称"`
  167. Remark string `description:"备注字段"`
  168. DetailDataCreateTime string `description:"明细数据入库时间"`
  169. DataList []AddBusinessData `description:"指标数据"`
  170. }
  171. // AddBusinessData
  172. // @Description: 外部指标(商家系统)数据
  173. type AddBusinessData struct {
  174. Value float64 `description:"值"`
  175. Date string `description:"日期"`
  176. }