base_future_good_lib.go 2.0 KB

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