12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package services
- import (
- "encoding/json"
- "eta_gn/eta_index_lib/models"
- "eta_gn/eta_index_lib/services/alarm_msg"
- "eta_gn/eta_index_lib/utils"
- "fmt"
- "io"
- "net/http"
- "strings"
- )
- var (
- BridgeApiGnIndexDataUrl = "/api/index_data/gn/edb/data/list" // 获取指标数据API
- BridgeApiGnNewIndexUrl = "/api/index_data/gn/new_index" // 获取增量指标API
- BridgeApiGnMenuListUrl = "/api/index_data/gn/menu_list" // 获取指标目录API
- )
- // GetGnIndexDataListFromBridge
- // @Description: 从桥接服务获取指标数据
- // @param param
- // @return indexDataList
- // @return err
- func GetGnIndexDataListFromBridge(param models.BridgeGnIndexDataParams) (indexDataList []models.BridgeGnIndexAndData, err error) {
- defer func() {
- if err != nil {
- b, _ := json.Marshal(param)
- tips := fmt.Sprintf("桥接服务-获取国能指标数据失败, err: %s, params: %s", err.Error(), string(b))
- utils.FileLog.Info(tips)
- go alarm_msg.SendAlarmMsg(tips, 3)
- }
- }()
- url := fmt.Sprint(utils.EtaBridgeUrl, BridgeApiGnIndexDataUrl)
- data, e := json.Marshal(param)
- if e != nil {
- err = fmt.Errorf("data json marshal err: %s", e.Error())
- return
- }
- body := io.NopCloser(strings.NewReader(string(data)))
- client := &http.Client{}
- req, e := http.NewRequest("POST", url, body)
- if e != nil {
- err = fmt.Errorf("http create request err: %s", e.Error())
- return
- }
- checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
- contentType := "application/json;charset=utf-8"
- req.Header.Set("Content-Type", contentType)
- req.Header.Set("Authorization", checkToken)
- resp, e := client.Do(req)
- if e != nil {
- err = fmt.Errorf("http client do err: %s", e.Error())
- return
- }
- defer func() {
- _ = resp.Body.Close()
- }()
- b, e := io.ReadAll(resp.Body)
- if e != nil {
- err = fmt.Errorf("resp body read err: %s", e.Error())
- return
- }
- if len(b) == 0 {
- err = fmt.Errorf("resp body is empty")
- return
- }
- fmt.Println(string(b))
- // 生产环境解密
- if utils.RunMode == "release" {
- str := string(b)
- str = strings.Trim(str, `"`)
- b = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
- }
- result := new(models.BridgeGnResultIndexData)
- if e = json.Unmarshal(b, &result); e != nil {
- err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
- return
- }
- if result.Code != 200 {
- err = fmt.Errorf("result: %s", string(b))
- return
- }
- indexDataList = result.Data
- return
- }
|