base_from_gn.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_index_lib/models"
  5. "eta_gn/eta_index_lib/services/alarm_msg"
  6. "eta_gn/eta_index_lib/utils"
  7. "fmt"
  8. "io"
  9. "net/http"
  10. "strings"
  11. )
  12. var (
  13. BridgeApiGnIndexDataUrl = "/api/index_data/gn/edb/data/list" // 获取指标数据API
  14. BridgeApiGnNewIndexUrl = "/api/index_data/gn/new_index" // 获取增量指标API
  15. BridgeApiGnMenuListUrl = "/api/index_data/gn/menu_list" // 获取指标目录API
  16. )
  17. // GetGnIndexDataListFromBridge
  18. // @Description: 从桥接服务获取指标数据
  19. // @param param
  20. // @return indexDataList
  21. // @return err
  22. func GetGnIndexDataListFromBridge(param models.BridgeGnIndexDataParams) (indexDataList []models.BridgeGnIndexAndData, err error) {
  23. defer func() {
  24. if err != nil {
  25. b, _ := json.Marshal(param)
  26. tips := fmt.Sprintf("桥接服务-获取国能指标数据失败, err: %s, params: %s", err.Error(), string(b))
  27. utils.FileLog.Info(tips)
  28. go alarm_msg.SendAlarmMsg(tips, 3)
  29. }
  30. }()
  31. url := fmt.Sprint(utils.EtaBridgeUrl, BridgeApiGnIndexDataUrl)
  32. data, e := json.Marshal(param)
  33. if e != nil {
  34. err = fmt.Errorf("data json marshal err: %s", e.Error())
  35. return
  36. }
  37. body := io.NopCloser(strings.NewReader(string(data)))
  38. client := &http.Client{}
  39. req, e := http.NewRequest("POST", url, body)
  40. if e != nil {
  41. err = fmt.Errorf("http create request err: %s", e.Error())
  42. return
  43. }
  44. checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
  45. contentType := "application/json;charset=utf-8"
  46. req.Header.Set("Content-Type", contentType)
  47. req.Header.Set("Authorization", checkToken)
  48. resp, e := client.Do(req)
  49. if e != nil {
  50. err = fmt.Errorf("http client do err: %s", e.Error())
  51. return
  52. }
  53. defer func() {
  54. _ = resp.Body.Close()
  55. }()
  56. b, e := io.ReadAll(resp.Body)
  57. if e != nil {
  58. err = fmt.Errorf("resp body read err: %s", e.Error())
  59. return
  60. }
  61. if len(b) == 0 {
  62. err = fmt.Errorf("resp body is empty")
  63. return
  64. }
  65. fmt.Println(string(b))
  66. // 生产环境解密
  67. if utils.RunMode == "release" {
  68. str := string(b)
  69. str = strings.Trim(str, `"`)
  70. b = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
  71. }
  72. result := new(models.BridgeGnResultIndexData)
  73. if e = json.Unmarshal(b, &result); e != nil {
  74. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  75. return
  76. }
  77. if result.Code != 200 {
  78. err = fmt.Errorf("result: %s", string(b))
  79. return
  80. }
  81. indexDataList = result.Data
  82. return
  83. }