base_future_good_lib.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package future_good
  2. import (
  3. "encoding/json"
  4. "eta/eta_task/models"
  5. "eta/eta_task/utils"
  6. "io/ioutil"
  7. "net/http"
  8. "strings"
  9. )
  10. // AddEdbData 添加指标数据
  11. func AddEdbData(futureGoodEdbCode string) (resp *models.BaseResponse, err error) {
  12. param := make(map[string]interface{})
  13. param["EdbCode"] = futureGoodEdbCode
  14. urlStr := `future_good/add`
  15. resp, err = postRefreshEdbData(param, urlStr)
  16. return
  17. }
  18. // RefreshEdbData 刷新指标数据
  19. func RefreshEdbData(futureGoodEdbInfoId int, futureGoodEdbCode, startDate string) (resp *models.BaseResponse, err error) {
  20. param := make(map[string]interface{})
  21. param["FutureGoodEdbCode"] = futureGoodEdbCode
  22. param["FutureGoodEdbInfoId"] = futureGoodEdbInfoId
  23. param["StartDate"] = startDate
  24. urlStr := `future_good/refresh`
  25. resp, err = postRefreshEdbData(param, urlStr)
  26. return
  27. }
  28. // postRefreshEdbData 刷新指标数据
  29. func postRefreshEdbData(param map[string]interface{}, urlStr string) (resp *models.BaseResponse, err error) {
  30. postUrl := utils.EDB_LIB_URL + urlStr
  31. postData, err := json.Marshal(param)
  32. if err != nil {
  33. return
  34. }
  35. result, err := HttpPost(postUrl, string(postData), "application/json")
  36. if err != nil {
  37. return
  38. }
  39. utils.FileLog.Info("postRefreshEdbData:" + postUrl + ";" + string(postData) + ";result:" + string(result))
  40. err = json.Unmarshal(result, &resp)
  41. if err != nil {
  42. return
  43. }
  44. return resp, nil
  45. }
  46. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  47. body := ioutil.NopCloser(strings.NewReader(postData))
  48. client := &http.Client{}
  49. req, err := http.NewRequest("POST", url, body)
  50. if err != nil {
  51. return nil, err
  52. }
  53. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  54. if len(params) > 0 && params[0] != "" {
  55. contentType = params[0]
  56. }
  57. req.Header.Set("Content-Type", contentType)
  58. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  59. resp, err := client.Do(req)
  60. defer resp.Body.Close()
  61. b, err := ioutil.ReadAll(resp.Body)
  62. //fmt.Println("HttpPost:" + string(b))
  63. return b, err
  64. }