eta_bridge.go 5.6 KB

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