base_edb_lib.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package edb_data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "hongze/hongze_open_api/models/response/edb"
  7. "hongze/hongze_open_api/utils"
  8. "io/ioutil"
  9. "net/http"
  10. "strings"
  11. )
  12. // EdbDetail 指标详情
  13. func EdbDetail(uniqueCode string) (edbInfoDetail *edb.BaseEdbInfoDetailResp, err error, msg string) {
  14. param := make(map[string]interface{})
  15. param["UniqueCode"] = uniqueCode
  16. urlStr := `open/edb/detail`
  17. resp, err := postRefreshEdbData(param, urlStr)
  18. if err != nil {
  19. return
  20. }
  21. if resp.Ret != 200 {
  22. msg = resp.Msg
  23. err = errors.New(resp.ErrMsg)
  24. return
  25. }
  26. dataBytes, err := json.Marshal(resp.Data)
  27. if err != nil {
  28. return
  29. }
  30. err = json.Unmarshal(dataBytes, &edbInfoDetail)
  31. if err != nil {
  32. return
  33. }
  34. return
  35. }
  36. // postRefreshEdbData 刷新指标数据
  37. func postRefreshEdbData(param map[string]interface{}, urlStr string) (resp *edb.BaseResponse, err error) {
  38. postUrl := utils.EDB_LIB_URL + urlStr
  39. postData, err := json.Marshal(param)
  40. if err != nil {
  41. return
  42. }
  43. result, err := HttpPost(postUrl, string(postData), "application/json")
  44. if err != nil {
  45. return
  46. }
  47. utils.FileLog.Info("postRefreshEdbData:" + postUrl + ";" + string(postData) + ";result:" + string(result))
  48. err = json.Unmarshal(result, &resp)
  49. if err != nil {
  50. return
  51. }
  52. return resp, nil
  53. }
  54. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  55. body := ioutil.NopCloser(strings.NewReader(postData))
  56. client := &http.Client{}
  57. req, err := http.NewRequest("POST", url, body)
  58. if err != nil {
  59. return nil, err
  60. }
  61. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  62. if len(params) > 0 && params[0] != "" {
  63. contentType = params[0]
  64. }
  65. req.Header.Set("Content-Type", contentType)
  66. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  67. resp, err := client.Do(req)
  68. defer resp.Body.Close()
  69. b, err := ioutil.ReadAll(resp.Body)
  70. fmt.Println("HttpPost:" + string(b))
  71. return b, err
  72. }