package services import ( "encoding/json" "errors" "eta/eta_mini_crm_ht/utils" httpClient "eta/eta_mini_crm_ht/utils/http" "fmt" "io" ) const ( GetIndexInfoUrl = "/htApi/v1/index/getIndexInfo" ) type IndexInfo struct { IndexCode string `json:"indexCode"` IndexName string `json:"indexName"` Frequency string `json:"frequency"` SourceName string `json:"sourceName"` LatestDataDate string `json:"latestDataDate"` LastPushTime string `json:"lastPushTime"` Remark string `json:"remark"` Unit string `json:"unit"` DataList []interface{} `json:"dataList"` } type HTResponse struct { Code string `json:"code"` Success bool `json:"success"` Message string `json:"message"` Data json.RawMessage `json:"data"` } func GetIndexInfo(indexCode string) (indexInfo *IndexInfo, err error) { client := httpClient.DefaultClient() if utils.HT_ADDRESS == "" { err = errors.New("未配置海通钢联地址") return } url := fmt.Sprintf("%s%s?indexCode=%s", utils.HT_ADDRESS, GetIndexInfoUrl, indexCode) resp, err := client.Get(url) if err != nil { utils.FileLog.Error("获取钢联指标信息失败", err.Error()) return } defer func(Body io.ReadCloser) { closeErr := Body.Close() if closeErr != nil { utils.FileLog.Error("关闭Response失败:%v", closeErr) } }(resp.Body) body, _ := io.ReadAll(resp.Body) var htRes HTResponse err = json.Unmarshal(body, &htRes) if err != nil { utils.FileLog.Error("解析应答信息失败:%v", err) return } if htRes.Success { err = json.Unmarshal(htRes.Data, &indexInfo) } else { err = errors.New(htRes.Message) } return }